Write polyline vertices to csv

3981
4
04-10-2014 06:27 AM
TonyAppel1
New Contributor
I am looking for some code to write out each line feature in a feature class/shapefile to a csv file that would look something like this:

FID, X, Y

All help is appreciated.

thanks

Tony
Tags (2)
0 Kudos
4 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Tony,

You could perform the following steps:

1.  Convert the lines vertices to a point feature class using Feature Vertices to Points
2.  Add XY Coordinates to point feature class
3.  Export feature class to dbf file
4.  Rename output dbf file to csv using os.rename
0 Kudos
Zeke
by
Regular Contributor III
You could also use the python csv module to write the file out.
0 Kudos
MattEiben
Occasional Contributor
A search cursor like this could easily iterate through all the points and add them to a csv file for you.

The key here is to use the "explode to points" method in the search cursor

import csv
        
with arcpy.da.SearchCursor(featureClass, ["OID@","SHAPE@XY"],explode_to_points=True) as cursor:
    with open("outputFile.csv","wb") as csvfile:
        writer = csv.writer(csvfile)
        writer.writerow(["FID","LAT","LON"])
        for row in cursor:
            FID = row[0]
            lat = row[1][1]
            lon = row[1][0]
            writer.writerow([FID,lat,lon])


Hope this helps!

Matt
0 Kudos
MatthewDobson
Occasional Contributor
I am not sure, but will the 'Export Feature Attribute to ASCII' tool do the trick?

I found it under Spatial Statistics -> Utilities ....




Matthew
0 Kudos