1

I am attempting to do the final project for the open source Penn state class https://www.e-education.psu.edu/geog485/node/144. The task is too parse a csv of rhino coordinates and create a polyline. I have written code that I believe should work but nothing gets created in the feature class

import csv
import arcpy
arcpy.env.overwriteOutput = True
file = 'C:\RhinoObservations.csv'
geodb = "C:\rhinotracks.gdb"
earthq = arcpy.CreateFeatureclass_management(geodb,"Rhino","POLYLINE",'','',"ENABLED",4326)
arcpy.AddField_management(earthq,"NAME","TEXT")
rhino = {}
with open(file, 'rb') as csvv:
    reader = csv.DictReader(csvv)
    for row in reader:
        if row['Rhino'] not in rhino:
            rhino[row['Rhino']] = []
        rhino[row['Rhino']].append((float(row['X']),float(row['Y'])))
with arcpy.da.InsertCursor(earthq,("NAME","SHAPE@XY")) as cur:
    for k,v in rhino.items():
        print k,v
        array = arcpy.Array()
        for coords in v:
            array.add(arcpy.Point(float(coords[0]),float(coords[1])))
        line = arcpy.Polyline(array,4326)
        row2 = (k,line)
        cur.insertRow(row2)

the script throws no error and create a feature class of the rhinos names but there are no polylines

enter image description here

I am using this post as a reference Creating arrays and polylines from dictionary using ArcPy?

when I print k,v the rhinos name has the correct XY pairs stored in their correct tuples. I do not know why there is no polyline showing up this is the output for the Rhino Meredith when I print k,v. Tuple in a list

Meredith [(26.97808, -19.11016), (26.97768, -19.11153), (26.97551, -19.11269), (26.97657, -19.11343), (26.97607, -19.11448), (26.97434, -19.11438), (26.97311, -19.11301), (26.97378, -19.11269), (26.97412, -19.11175), (26.97479, -19.11085)]
  • 1
    Can you include a copy/paste of the printed XY pairs? – Midavalo Oct 14 '16 at 0:26
  • There was another similar question here recently - reason for no lines being produced was that the XY were pretty much right on top of each other, which looks like might be the issue for you. Do a search for similar recent questions – Midavalo Oct 14 '16 at 0:35
  • i dont think that would be the issue...they give this assignment for a class and show the output picture – ziggy Oct 14 '16 at 0:37
  • 2
    Why SHAPE@XY, what is happening when you use SHAPE@ in InsertCursor? – FelixIP Oct 14 '16 at 0:37
2

Have a look at this page: http://pro.arcgis.com/en/pro-app/arcpy/classes/polyline.htm#C_GUID-FC2C3085-2C89-4CDB-985B-DFE78118B603

This has a code sample for creating a polyline.

Try this instead (changed the polyline creation and SHAPE field):

import csv
import arcpy
arcpy.env.overwriteOutput = True
file = 'C:\RhinoObservations.csv'
geodb = "C:\rhinotracks.gdb"
earthq = arcpy.CreateFeatureclass_management(geodb,"Rhino","POLYLINE",'','',"ENABLED",4326)
arcpy.AddField_management(earthq,"NAME","TEXT")
rhino = {}
with open(file, 'rb') as csvv:
    reader = csv.DictReader(csvv)
    for row in reader:
        if row['Rhino'] not in rhino:
            rhino[row['Rhino']] = []
        rhino[row['Rhino']].append((row['X'],row['Y']))
with arcpy.da.InsertCursor(earthq,("NAME","SHAPE@")) as cur:
    for k,v in rhino.items():
        print k,v
        line = arcpy.Polyline(arcpy.Array([arcpy.Point(*coords) for coords in v]))
        row2 = (k,line)
        cur.insertRow(row2)

You were using the SHAPE@XY token. This is the token for the feature's centroid. For the feature's geometry, you need to use the SHAPE@ token.

  • weird I was initially using the SHAPE@ token and it wasnt working but it just worked – ziggy Oct 14 '16 at 0:41
  • were you testing in the python window? It stores everything you do in memory, so when you get your code right, make sure to restart arc to clear everything from memory and retest. – jbalk Oct 14 '16 at 1:01
  • Nope standalone script. I probably had another error or something – ziggy Oct 14 '16 at 1:14

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.