I need to generate a polygon FC using some records previously flagged as 'NUMBER_GT_NUMBER_END = 1'

These records need to be loaded into a new schema of the same FC in order to include duplicates that will be used later for other processes. If you run the code as I am giving it here, it will print out the number of records per OBJECTID. The issue is that I am able to insert only one for each case. Once added a record into the new FC schema, I want to update the SITUS_STREET_NUMBER field with the value stored in x + 1. I have been going back and forth with other approaches like using Append, Copy, among others, but the process is really slow in comparison to the one achieved so far using the da.cursors.


OBJECTID = 28964 ---> 3 records




This means that the above polygon geometry with OBJECTID = 28964 needs to be inserted to the new FC three times and that the SITUS_STREET_NUMBER field needs to be updated with the listed values.


import arcpy, re

arcpy.env.workspace = r'YOUR DIRECTOTY PATH HERE\locator_address_ranges.gdb'
arcpy.env.overwriteOutput = True

# Set a variable for the workspace
workspace = arcpy.env.workspace

featureClass = workspace + '\\' + 'ParcelsTax_Formated'
featureClassCopy = workspace + '\\' + 'ParcelsTax_Formated_new'
number_Field = "SITUS_STREET_NUMBER"
number_end_Field = "SITUS_STREET_NUMBER_END"
oid = "OBJECTID"
number_Field2 = "SITUS_STREET_NUMBER2"  # Long Integer
number_end_Field2 = "SITUS_STREET_NUMBER_END2"  # Long Integer
countAddrPnts = "Points"  # Long Integer
full_addr = "SITUS_FULL_ADDRESS"
greater_than = "NUMBER_GT_NUMBER_END"  # Value = 1 --> SITUS_STREET_NUMBER2 is > than SITUS_STREET_NUMBER_END2 - Short Integer
fieldsToUpdate = (number_Field, full_addr)

dsc = arcpy.Describe(featureClass)
fields = dsc.fields

# List all field names except the OID field

fieldnames = [field.name for field in fields]
print "Updated List : ", fieldnames

## STEP 1
# Insert rows for scenario where SITUS_STREET_NUMBER2 is > than SITUS_STREET_NUMBER_END2
arcpy.MakeFeatureLayer_management(featureClass, "countAddrPnts_lyr")
arcpy.SelectLayerByAttribute_management("countAddrPnts_lyr", "NEW_SELECTION", 'NUMBER_GT_NUMBER_END = 1')

rows = arcpy.SearchCursor(featureClass, 'NUMBER_GT_NUMBER_END = 1')
row = rows.next()

while row:
    arcpy.SelectLayerByAttribute_management("countAddrPnts_lyr", "NEW_SELECTION",
                                            "OBJECTID = " + str(row.getValue(oid)))
    print "OBJECTID = " + str(row.getValue(oid)) + ' ---> ' + str(row.getValue(countAddrPnts) + 1) + ' records'

    with arcpy.da.SearchCursor(featureClass, fieldnames, 'NUMBER_GT_NUMBER_END = 1 AND OBJECTID = ' + str(row.getValue(oid))) as sCur:
            for x in xrange(int(row.getValue(number_end_Field2) - 1), int(row.getValue(number_Field2))):
                print x + 1
                with arcpy.da.InsertCursor(featureClassCopy, fieldnames) as iCur:
                    for row in sCur:
    row = rows.next()

GIS data (FileGDB) and python script here

  • 1
    Have you looked into a nested for loop to insert the row? for _ in range(num_of_records): cursor.insertRow(row) – Paul Jan 25 '17 at 20:26
  • Can you show me how to do it since I am not sure how to? – Ernesto CD Jan 25 '17 at 20:39
  • Please include a code snippet that shows how you are inserting a single record within your question body. – PolyGeo Jan 25 '17 at 20:40
  • Generally speaking, you don't want to nest your cursors (ie don't open a cursor inside a for / while loop). You've nested an insert cursor inside a for loop inside a search cursor inside another search cursor. (Plus Select Layer by attribute!). With that being said, I think you'd place my code inside the for row in Scur block. Hard to follow though. – Paul Jan 25 '17 at 21:57
  • You're referring to two cursor contents as 'row', seemingly simultaneously. You should likely also follow the line creating sCur with 'for row in sCur:' and then place the xrange loop within that and the insertrow line within that. You should also only create the insert cursor once, outside all of the loops (now you're creating many, many insert cursors). – phloem Jan 25 '17 at 22:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.