I am working on building polylines from an array of points I have created from a csv file and am completely lost in building the lines. My code works as follows: I have the point array working, I need help in creating the polylines from the point array. Please note I am also wanting to add the name into the new polyline feature.

# Reads a csv file to create points of locations
import arcpy
print "==============RESTART===================="

# Set up variables
spreadsheet = "C:\\Users\\Desktop\\RhinoObservations2.csv"    # input file with coordinates
ouputFC = Rhino_Path          # defines the name of the ouput feature class

# Open the CSV file and read the header line. "r" is read the file
observationsFile = open (spreadsheet, "r")
print observationsFile

headerLine = observationsFile.readline()  # reads all lines in CSV??
print headerLine

# Determine the indicies of the columns "X","Y","Rhino"
fieldList = headerLine.split(",")
print fieldList

xIndex = fieldList.index("X")
print xIndex

yIndex = fieldList.index("Y")
print yIndex

rhinoIndex = fieldList.index("Rhino")
print rhinoIndex

# Set up new dictionary where the Rhino information is stored
rhinoDictionary = {}

# Loop through the remaining line of the input file; for each line....
for line in observationsFile.readlines():
   print line

    # Determine the rhino name, X,Y coordintes from that line
    segmentedLine = line.split(",")

    rhino = segmentedLine[rhinoIndex]

    x = segmentedLine[xIndex]

    y = segmentedLine[yIndex]

    # Create an object of the the class Point (defined in the arcpy module) for the X Y coordinate
    coords = arcpy.Point(x,y)

    # If this rhino is not already in the dictionary
    if not rhino in rhinoDictionary:

        # Create a new object of class Array, add the object point, and then put array into dictionary
        # under the name of the Rhino
        coordArray = arcpy.Array()  #setting up an empty list to store points
        coordArray.add(coords)      #list that contains one point
        rhinoDictionary[rhino] = coordArray

    # Else

        # Get the array object from the dictionary for this Rhino and add the point to the array.
        coordArray = rhinoDictionary[rhino]

# Print out the information in the dictionary
for key in rhinoDictionary:
    print key
    print "This is the point array: " + str(rhinoDictionary[key])
    for p in rhinoDictionary[key]:
        print "(" + str(p.X) + ", " + str(p.Y) + ")"

    # Take the array and create polylines from it.

    print "========"
# Create polylines and add them to the feature class
  • Could you reformat that code, it's very hard to read. You seem to be getting the points into the array ok, I think you need to do arcpy.polyline(coordArray) to turn it into a polyline. See this example: resources.arcgis.com/en/help/main/10.1/index.html#//… – Michael Stimson Jul 15 '14 at 21:54
  • I'm not sure how I can edit the formatting in this website – user32928 Jul 15 '14 at 22:04
  • go to edit, select the block and hit the {} button. – Michael Stimson Jul 15 '14 at 22:05
  • ah, thank you. I looked on the ESRI resource link, however I am confused as to how to apply it to my code. Will I use the third code block since it shows a list of coordinates going into a polyline? – user32928 Jul 15 '14 at 22:10
  • 1
    Re: "Please note I am also wanting to add the name into the new polyline feature". Please separate these into two questions. Thanks. – Aaron Jul 15 '14 at 22:20

To turn your array into a polyline you need to use arcpy.polyline, but I suspect that what you're really having an issue with is inserting features into a feature class, for this I recommend an insert cursor - they come in two flavors arcpy and arcpy.da. I generally use the arcpy (old method) but am coming to like the arcpy.da method.

To use an insert cursor declare it first and then use it to put features into a feature class; you must remove it when done or it will leave locks so consider using with.

This is the basic workflow for inserting one object using your variables.

InsCur = arcpy.InsertCursor(outputFC) # Declare the cursor
Feat = InsCur.newRow()                # create a new row, ready to be filled

Shape = arcpy.Polyline(coordArray)
Feat.shape = Shape # can do this directly using = arcpy.Polyline(coordArray)
Feat.setValue("Name","this name") # set the value of field Name to 'this name'

InsCur.insertRow(Feat) # put the modified row into the data
del InsCur             # don't forget this or the feature class will be locked
  • I was under the impression that normal cursors do not support with. If you have access to the data access module, definitely use that as it's faster, handles exceptions better, and is usually less code. – Paul Jul 15 '14 at 22:30
  • Michael, this works. However, for some reason my data is not looping and is only writing the first polyline path it is reading from the text file. Any thoughts on why this may be? I have indented all of your code underneath "for key in rhinoDictionary" – user32928 Jul 15 '14 at 23:03
  • I'm not sure what you're trying to do with the dictionary. It would help if I could understand what's in the input file and the updated code. – Michael Stimson Jul 15 '14 at 23:35
  • @MichaelMiles-Stimson Could you post some code that utilizes the with statement and original cursors? with arcpy.SearchCursor("poly_small") as cursor: pass gives an attribute error, while adding the da version works. – Paul Jul 16 '14 at 12:46
  • You are right @Paul, the with statement appears to be incompatible with the non-da cursor. All of my old code that I've been updating from gp & gp9.3 use del on the row and cursor so I haven't changed them.. if it 'aint broke, don't fix it! Perhaps you could post some code showing how to do this using a with statement and an arcpy.da.InsertCursor. – Michael Stimson Jul 16 '14 at 22:10

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.