1

I am trying to solve a problem, I am pasting part of the code:

from arcpy import env
arcpy.env.overwriteOutput = True

spatialRef = arcpy.SpatialReference("WGS 1984")
polylineFC = arcpy.CreateFeatureclass_management( r"H:\GIS5044\GIS5044_Nov 06\GIS5044\homework\hw#3\ ", "RhinoParsing.shp", "POLYLINE", "", "", "", spatialRef)
fieldList=["Name"]
arcpy.AddField_management(polylineFC,fieldList[0],"Text","#","#",8)
point = arcpy.Point()
array = arcpy.Array()
featureList = []
for rhino in nameCord_Dict:
    CordList= [nameCord_Dict[rhino]]
    print CordList
    print len(CordList)
    for feature in CordList:
            for cordpair in feature:
                point.X = cordpair[0]
                point.Y = cordpair[1]
                array.add(point)
    pointPolyline = arcpy.Polyline(array, spatialRef)
    with arcpy.da.InsertCursor(polylineFC,("SHAPE@")) as iCursor:
        array.removeAll()
    iCursor.insertRow((pointPolyline,))

Here I am trying to make one polyline for each [rino] of the dictionary "nameCord_Dict". but when I run the code it creates one line feature instead of 5 as there are five keys in the dictionary. I am not understanding where should i do to end polyline once all the coordinate related to one element of "nameCord_Dict" is finish and start a new line for a new element.

This is the polyline i got:

  • 1
    "This is the polyline i got:" seems that you forgot to add something inportant to your question. – SS_Rebelious Nov 23 '14 at 16:47
  • Thank you and sorry for asking a confusing question. Yes, there is only one polyline object. but i want to make polyline for each element. I am not if it is clear now. please let me know.. – Mac Nov 23 '14 at 16:51
2

I believe your code placement for the cursor and indentation is incorrect to get all of the lines you want. You should only need to create the insert cursor once before reading your dictionary. Also I do not see the da InsertCursor help recommend using the with syntax (although if you did use the with syntax do it with this layout and indent the entire for loop one additional level, since the insertRow method must always fall at least one or more indent levels inside the insert cursor creation block for the with syntax). So try:

iCursor = arcpy.da.InsertCursor(polylineFC,("SHAPE@"))
for rhino in nameCord_Dict:
    CordList= [nameCord_Dict[rhino]]
    print CordList
    print len(CordList)
    for feature in CordList:
        for cordpair in feature:
            point.X = cordpair[0]
            point.Y = cordpair[1]
            array.add(point)
    pointPolyline = arcpy.Polyline(array, spatialRef)
    iCursor.insertRow((pointPolyline,))
    del array
    array = arcpy.Array()
    del pointPolyline
del iCursor
del array
  • Thank you for the help. I am actually very new in writing code and therefore i could not figure out where is the problem. I tried with the code you provided and it actually connected all the polylines which were separate before. i am not sure where i am making the mistake but now it is just one polyline created using all the coordinates of the entire dictionary. do you have any suggestion? thanks. – Mac Nov 23 '14 at 18:08
  • Change the array.removelAll() method to del array and recreate it with array = arcpy.Array() , then add a new line at that indent level with del pointPolyline to kill the polyline at the end of each rhino set. I have edited my post to add that line. – Richard Fairhurst Nov 24 '14 at 3:58
  • Also, are you sure it is only creating one polyline record? I don't see how that is possible. Is it actually creating 5 separate records and extending the line each time to create the final line with all the points? If it doing that then my revisions should fix it. Since I don't know how the nameCord_Dict was populated with points in your code I cannot say what is going on without that part of your code. – Richard Fairhurst Nov 24 '14 at 4:09
  • Are you somehow destroying the feature class in a loop? The arcpy.CreateFeatureclass_management should only occur once outside of any loop. Be sure to paste all of the code that you have written before this section, especially if the indent level of the code I have written is different in your code. – Richard Fairhurst Nov 24 '14 at 4:19
  • Thank you so much. Now it is producing five lines. Could you please suggest how can I make each line different color. Now all the lines are same. – Mac Nov 24 '14 at 7:21

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.