1

I am using ArcMap 10.1 and python to create cursor that starts at the one and only row of a polyline shapefile, some code runs to create a new feature based on the position of the exisiting feature, then adds this new feature as a second row. I then want the same code to run again on the newly created feature to then create a third...and so on.

However, the cursor stops working after the first run and the code completes, I guessing because the initial cursor doesn't take into account the newly created feature?

However I need this to run until a certain number of rows/features are created.

Is anyone able to help with this please?

Its as if I need to refresh the cursor or something, but I struggling to find a solution.

def addPolyline(cursor, array, sr):
    polyline = arcpy.Polyline(array, sr)
    cursor.insertRow((polyline,))
    array.removeAll()

import arcpy
import os
import math

infc = ...path...
shapefile = ...path...

mxd = arcpy.mapping.MapDocument("CURRENT")

df = arcpy.mapping.ListDataFrames(mxd,"*")[0]

spatialRef = arcpy.Describe(infc).spatialReference

desc = arcpy.Describe(infc)
shapefieldname = desc.ShapeFieldName

rows = arcpy.SearchCursor(infc)

with arcpy.da.InsertCursor(shapefile, ("SHAPE@",)) as cursor:
    polylineArray = arcpy.Array() 

    for row in rows:

            < SOME CODE TO CREATE NEW FEATURE >  

            newStartPoint = arcpy.Point(newStartX, newStartY)
            newEndPoint = arcpy.Point(newEndX, newEndY)

            polylineArray.add(newStartPoint)
            polylineArray.add(newEndPoint)

            addPolyline(cursor, polylineArray, spatialRef)
  • 1
    I'd simply use list of lists. First element [Shape, field1value, etc]. While True: if len(bigList)==maxLength break. Find last element in the list. Process it, add result to bigList. When done dump all but first row in the list to fc using da.InsertCursor. If memory limitations, start with InsertCursor. Insert required number of records - dummies. Do updateCursor, keeping in memory previous row only – FelixIP Feb 5 '15 at 21:04
  • 1
    I think you already have the answer, the cursor gets a list of the features on creation so new features aren't referenced, and also features removed still are referenced by the cursor. You can either take the suggestion by @FelixIP or close the insert cursor to flush pending writes and instantiate the search cursor each time you want to add a bunch of new features... although not mentioned in your code are you actually inserting the feature (is that what addPolyline does)? – Michael Stimson Feb 5 '15 at 22:59
0

As commented by @MichaelMiles-Stmson:

I think you already have the answer, the cursor gets a list of the features on creation so new features aren't referenced, and also features removed still are referenced by the cursor. You can either take the suggestion by @FelixIP or close the insert cursor to flush pending writes and instantiate the search cursor each time you want to add a bunch of new features.

The suggestion by @FelixIP was:

I'd simply use list of lists. First element [Shape, field1value, etc]. While True: if len(bigList)==maxLength break. Find last element in the list. Process it, add result to bigList. When done dump all but first row in the list to fc using da.InsertCursor. If memory limitations, start with InsertCursor. Insert required number of records - dummies. Do updateCursor, keeping in memory previous row only

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.