I have a *.csv file that have a specific field that contain array coordinates representing a route. This is a example:
[(39.72412, -104.9995), (39.72412, -105.00091), (39.72417, -105.00157), (39.72422, -105.00159)]
How can I create with Python a polyline shapefile by this array point coordinate? Open Source or Esri solution is the same. I have try with this code using the help below, and i have add a fix to read specific field by csv:
with open(csvfile,'rb') as f:
reader = csv.DictReader(f)
for row in reader:
listrow=ast.literal_eval(row["route"]) #row will be a string representation of a list so it its converted to a list with ast.literal_eval
line=arcpy.Polyline(arcpy.Array([arcpy.Point(*coords) for coords in listrow]))
icur.insertRow((line,))
del icur
the code works fine, but i would read a second field of csv to see into shapefile. My csv have for each row the array coordinate and ID, how i can write also the ID in line shape?
-
1What have you tried? Take look at the InsertCursor and the examples at the bottom: pro.arcgis.com/en/pro-app/arcpy/data-access/…Bera– Bera2017年10月11日 12:52:48 +00:00Commented Oct 11, 2017 at 12:52
-
i have seen the insert cursor but i don't have a solution with this arrayAPPGIS– APPGIS2017年10月11日 12:56:09 +00:00Commented Oct 11, 2017 at 12:56
-
1Please Edit your question after you choose the desired software environment. Right now you're effectively asking several questions, one for each potential GIS package, which violates the "One question per Question" policy set forth in the Tour.Vince– Vince2017年10月11日 13:04:05 +00:00Commented Oct 11, 2017 at 13:04
1 Answer 1
The InsertCursor is the way to go:
InsertCursor establishes a write cursor on a feature class or table. InsertCursor can be used to add new rows.
CSV file (1 polyline will be created for each row):
[(39.72412, -104.9995), (39.72412, -105.00091), (39.72417, -105.00157), (39.72422, -105.00159)]
[(39.72412, -104.9995), (39.72412, -105.00091), (39.72417, -105.00157), (39.72422, -105.00159)]
[(39.72412, -104.9995), (39.72412, -105.00091), (39.72417, -105.00157), (39.72422, -105.00159)]
[(39.72412, -104.9995), (39.72412, -105.00091), (39.72417, -105.00157), (39.72422, -105.00159)]
Code:
import arcpy,ast,os
arcpy.env.overwriteOutput=1
#Change to match your data:
csvfile =r'C:\Test\Polylines.csv'
folder=r'C:\Test'
shapename=r'PolylineCSV.shp'
spatref_epsg=3006
arcpy.CreateFeatureclass_management(out_path=folder, out_name=shapename,geometry_type='POLYLINE',
spatial_reference=arcpy.SpatialReference(spatref_epsg))
icur=arcpy.da.InsertCursor(os.path.join(folder,shapename),'SHAPE@')
with open(csvfile,'rb') as f:
for row in f:
listrow=ast.literal_eval(row) #row will be a string representation of a list so it its converted to a list with ast.literal_eval
line=arcpy.Polyline(arcpy.Array([arcpy.Point(*coords) for coords in listrow]))
icur.insertRow((line,))
del icur
-
Hi have update my code. I have modife your help to read a specific field of csv but i need to read and add also the ID field of array coordinateAPPGIS– APPGIS2017年10月11日 16:08:32 +00:00Commented Oct 11, 2017 at 16:08
-
Im not going to answer that since you didnt include this in the original questionBera– Bera2017年10月11日 16:26:53 +00:00Commented Oct 11, 2017 at 16:26
-
I have edit my post. Whta do you mean? You mead to show the exampple cvs?APPGIS– APPGIS2017年10月11日 17:12:51 +00:00Commented Oct 11, 2017 at 17:12
-
I mean that you cant ask one question, accept my answer and then change the question. I Think you should post Another question on how to include the ID column.Bera– Bera2017年10月12日 05:05:22 +00:00Commented Oct 12, 2017 at 5:05
-
Ok, sorry. I have posted other question. Thanks for your helpAPPGIS– APPGIS2017年10月12日 08:42:16 +00:00Commented Oct 12, 2017 at 8:42