0

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?

  • 1
    What have you tried? Take look at the InsertCursor and the examples at the bottom: pro.arcgis.com/en/pro-app/arcpy/data-access/… – BERA Oct 11 '17 at 12:52
  • i have seen the insert cursor but i don't have a solution with this array – APPGIS Oct 11 '17 at 12:56
  • 1
    Please 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 Oct 11 '17 at 13:04
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 coordinate – APPGIS Oct 11 '17 at 16:08
  • Im not going to answer that since you didnt include this in the original question – BERA Oct 11 '17 at 16:26
  • I have edit my post. Whta do you mean? You mead to show the exampple cvs? – APPGIS Oct 11 '17 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 Oct 12 '17 at 5:05
  • Ok, sorry. I have posted other question. Thanks for your help – APPGIS Oct 12 '17 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.