arcpy insert cursor problem - adding multiple polygons

2998
4
Jump to solution
11-13-2014 01:01 PM
RobertWetmore
New Contributor

I am reading rows from a database with a field "Coords" that contains polygon coordinates in the format 40.090170,-83.007704|40.089878,-83.007724..... and inserting each row as a polygon.

 

The first feature is drawn correctly, the second feature is drawn but  is connected to and duplicates the first feature, and the third contains all three and so on.

 

The coordinates are cumulative (not intentionally).  I tried deleting variables between iterations, but that didn't help.  What am I missing?

Thanks

 

    import pypyodbc     import arcpy     GdbFeat = r"\\server\Arc_Server_data\publish.gdb\OUPS"     spatialRef = arcpy.Describe(GdbFeat).spatialReference     conn = pypyodbc.win_connect_mdb(r"C:\GIS\db\COWOUPSx.mdb")     cursor = conn.cursor()     cursor.execute("SELECT TicketDate, TicketNum, Coords FROM Tickets WHERE (((TicketDate) = #11/15/2014#))")     rows = cursor.fetchall()     cursor.close     del cursor     conn.close     del conn     if rows:                     polygonArray = arcpy.Array()             for row in rows:                                     Coords = row[6]                     coordinatePairList = Coords.split("|")                     for coordinatePair in coordinatePairList:                             coordinates = coordinatePair.split(",")                             currentPoint = arcpy.Point(coordinates[1],coordinates[0])                                                     polygonArray.add(currentPoint)                                                                                polygon = arcpy.Polygon(polygonArray, spatialRef)                                     polygonArray.removeAll                                     with arcpy.da.InsertCursor(GdbFeat, ("Shape@", "TicketDate", "TicketNum")) as Acursor:                             Acursor.insertRow((polygon, row[0], row[1]))                                                          del Coords                                 del polygon                     del coordinatePairList                     del coordinatePair                     del coordinates                     del currentPoint                                  del row             del rows
0 Kudos
1 Solution

Accepted Solutions
IanMurray
Frequent Contributor

polygonArray.removeAll

need to be

polygonArray.removeAll()

Your array is never emptying, so it contains each set of coordinates with each run through the for-loop

View solution in original post

0 Kudos
4 Replies
IanMurray
Frequent Contributor

polygonArray.removeAll

need to be

polygonArray.removeAll()

Your array is never emptying, so it contains each set of coordinates with each run through the for-loop

0 Kudos
RobertWetmore
New Contributor

That's what I thought - I was just missing the ()

Thank You

0 Kudos
RobertWetmore
New Contributor

Answered here

need to create a new array for each iteration

if rows:        

        for row in rows:

                polygonArray = arcpy.Array()

I thought polygonArray.removeAll would reset the array, but it did not.

0 Kudos
IanMurray
Frequent Contributor

Creating the array after the for row in rows would work too, since it creates a new array each time.  Your code was just missing the parantheses, otherwise it would have been fine as is.

0 Kudos