簡體   English   中英

如何將 csv 中的一列轉換為兩個單獨的文件:Python 中的一個 shape 文件和一個 osm 文件?

[英]How to convert convert one column in csv to two separate files: a shape file and an osm file in Python?

我有一個包含出租車行程的.csv文件,其中一列稱為trip_coordinates ,存儲為字符串,例如,一個行程坐標如下所示(存儲為字符串:):

[[40.7457407, -73.9781134], [40.7464087, -73.9797169], [40.7457353, -73.9801966], [40.7463887, -73.9817513], [40.7508351, -73.9785736], [40.7509627, -73.9785244], [40.7521935, -73.9776193], [40.7546355, -73.9757004], [40.7539937, -73.9741902], [40.753367, -73.974648], [40.754351, -73.9769749], [40.7547351, -73.9778672], [40.7554134, -73.9794895], [40.7547828, -73.9799429], [40.7451552, -73.9826672], [40.7457757, -73.9822189], [40.7463887, -73.9817513], [40.7508351, -73.9785736], [40.7509627, -73.9785244], [40.7521935, -73.9776193], [40.7546355, -73.9757004], [40.7552761, -73.9752669], [40.755903, -73.9748081], [40.756526, -73.974356], [40.7565994, -73.9745281], [40.7572359, -73.9760484], [40.7578582, -73.975593], [40.7584878, -73.9751336], [40.7591136, -73.9746825], [40.7597325, -73.974231], [40.7603711, -73.9737664], [40.7609986, -73.9733102]]

使用這些坐標,我能夠創建一個 LINESTRING 並將其存儲回原始.csv文件中名為route_linestring的列中,方法如下:

def convert_to_lineString(batch):
   batch_trips = pd.read_csv('batch.csv')

   for index, row in batch_trips.iterrows():
     if row['selected_distance'] != -100:
         temp = row['trip_route'].split(',')
         pnts_array = []
         for item in range(0,len(temp)):
            if item % 2 == 0:
                # string manipulation to extract points
                x = temp[item].replace('[','')
                y = temp[item+1].replace(']','')
                pnt = Point(float(x), float(y))
                pnts_array.append(pnt)
         line = LineString(pnts_array)
         print('line:', line)             
         batch_trips.at[index, 'route_linestring'] = line

   batch_trips.to_csv('batch.csv')

convert_to_lineString(1, 1)

上面的數組或坐標現在看起來像這樣:

LINESTRING (40.7457407 -73.9781134, 40.7464087 -73.9797169, 40.7457353 -73.9801966, 40.7463887 -73.9817513, 40.7508351 -73.9785736, 40.7509627 -73.9785244, 40.7521935 -73.9776193, 40.7546355 -73.9757004, 40.7539937 -73.9741902, 40.753367 -73.974648, 40.754351 -73.9769749, 40.7547351 -73.9778672, 40.7554134 -73.9794895, 40.7547828 -73.9799429, 40.7451552 -73.9826672, 40.7457757 -73.9822189, 40.7463887 -73.9817513, 40.7508351 -73.9785736, 40.7509627 -73.9785244, 40.7521935 -73.9776193, 40.7546355 -73.9757004, 40.7552761 -73.9752669, 40.755903 -73.9748081, 40.756526 -73.974356, 40.7565994 -73.9745281, 40.7572359 -73.9760484, 40.7578582 -73.975593, 40.7584878 -73.9751336, 40.7591136 -73.9746825, 40.7597325 -73.974231, 40.7603711 -73.9737664, 40.7609986 -73.9733102)

我需要幫助將列route_linestring保存在單獨的形狀文件以及單獨的 .osm 文件中嗎?

我會通過將 csv 讀取為geopandas.GeoDataFrame並結合使用json.loadsshapely.LineString將字符串坐標轉換為幾何來解決這個問題。 然后您可以使用.to_file將地理數據框另存為 shapefile。 最后,我將使用ogr2osm從新創建的 shapefile 創建 osm 文件。

例子.csv:

label,trip_route
feature 1,"[[40.7457407, -73.9781134], [40.7464087, -73.9797169], [40.7457353, -73.9801966], [40.7463887, -73.9817513], [40.7508351, -73.9785736], [40.7509627, -73.9785244], [40.7521935, -73.9776193], [40.7546355, -73.9757004], [40.7539937, -73.9741902], [40.753367, -73.974648], [40.754351, -73.9769749], [40.7547351, -73.9778672], [40.7554134, -73.9794895], [40.7547828, -73.9799429], [40.7451552, -73.9826672], [40.7457757, -73.9822189], [40.7463887, -73.9817513], [40.7508351, -73.9785736], [40.7509627, -73.9785244], [40.7521935, -73.9776193], [40.7546355, -73.9757004], [40.7552761, -73.9752669], [40.755903, -73.9748081], [40.756526, -73.974356], [40.7565994, -73.9745281], [40.7572359, -73.9760484], [40.7578582, -73.975593], [40.7584878, -73.9751336], [40.7591136, -73.9746825], [40.7597325, -73.974231], [40.7603711, -73.9737664], [40.7609986, -73.9733102]]"

代碼:

import json

import geopandas as gpd
import ogr2osm
from shapely import LineString

# Load csv as GeoDataFrame
df = gpd.read_file('example.csv')

# Convert coordinate string to geometry
df.geometry = df.trip_route.apply(lambda x: LineString(json.loads(x)))

# Export to shapefile
df.to_file('example.shp')

# Use ogr2osm to convert shapefile to osm file
translation_object = ogr2osm.TranslationBase()
datasource = ogr2osm.OgrDatasource(translation_object)
datasource.open_datasource('example.shp')
osmdata = ogr2osm.OsmData(translation_object)
osmdata.process(datasource)
datawriter = ogr2osm.OsmDataWriter('example.osm')
osmdata.output(datawriter)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM