1

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?

Bera
81.6k14 gold badges84 silver badges197 bronze badges
asked Oct 11, 2017 at 12:42
3
  • 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/… Commented Oct 11, 2017 at 12:52
  • i have seen the insert cursor but i don't have a solution with this array Commented Oct 11, 2017 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. Commented Oct 11, 2017 at 13:04

1 Answer 1

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
answered Oct 11, 2017 at 13:30
5
  • 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 Commented Oct 11, 2017 at 16:08
  • Im not going to answer that since you didnt include this in the original question Commented Oct 11, 2017 at 16:26
  • I have edit my post. Whta do you mean? You mead to show the exampple cvs? Commented 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. Commented Oct 12, 2017 at 5:05
  • Ok, sorry. I have posted other question. Thanks for your help Commented Oct 12, 2017 at 8:42

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.