I previously asked Updating attribute information about this script last week. I have amended it and am confused.

I need to write the ADMINAREA value to the empty LOCALITY fields from TWB_Suburbs to TWB_Property respectively. This script took about 3.5 hours to run and the result did not update the LOCALITY field in the TWB_Property feature class. Can anyone please help?

# Import modules
import arcpy
import os

#Set workspace geodatabase
arcpy.env.workspace = "C:\Users\Harry\Documents\Uni\Semester 1 2016\GIS3407 GIS Programming and Visualisation\Assignment 2\Assignment2.gdb"

#Start an editing session
edit = arcpy.da.Editor("C:\Users\Harry\Documents\Uni\Semester 1 2016\GIS3407 GIS Programming and Visualisation\Assignment 2\Assignment2.gdb")
edit.startEditing(False, False)

# Create a list of suburbs
# Create an empty list
localityList = []

#Create the search cursor
rows = arcpy.SearchCursor("TWB_Suburbs")

#Move to the first row
row = rows.next()

#Append the list with the selected locality name
while row:
    #Move to the next row
    row = rows.next()

print(str(len(localityList)) + " <-- # of suburbs")

del row, rows
# Make a layer of all of the properties with no locality value
# Create query
nullQuery = '"LOCALITY" = \' \''

# Create layer
nullLayer = arcpy.MakeFeatureLayer_management("TWB_Property", "TWB_Property_Layer", nullQuery)
field = "LOCALITY"
fc = "TWB_Property_Layer"

counter = 0

# Select by location
indexCounter = 0
print localityList
for counter in range(0,len(localityList)):
    localityQuery = '"ADMINAREA" = ' + '\''+ localityList[indexCounter] + '\''
    localityLayer = arcpy.MakeFeatureLayer_management("TWB_Suburbs", "TWB_Sububs_Layer", localityQuery)
    arcpy.SelectLayerByLocation_management("TWB_Property_Layer", "WITHIN", "TWB_Sububs_Layer")

    parcelList = []
    sRows = arcpy.SearchCursor("TWB_Property_Layer")
    sRow = sRows.next()

    while sRow:
            sRow = sRows.next()
            with arcpy.da.UpdateCursor(fc, field) as cursor:
                for uRow in cursor:
                    if uRow[0] != "str(localityList[indexCounter]":
                        uRow[0] = str(localityList[indexCounter])
    print ("There are " + str(len(parcelList)) + " empty locality fields in " + localityList[indexCounter])
    counter += 1
    indexCounter += 1

del uRow


The output in PyScripter was:

20 <-- # of suburbs


There are 1340 empty locality fields in GLENVALE

There are 332 empty locality fields in CRANLEY

There are 1398 empty locality fields in ROCKVILLE

There are 137 empty locality fields in MOUNT KYNOCH

There are 1367 empty locality fields in HARLAXTON

There are 1796 empty locality fields in MOUNT LOFTY

There are 839 empty locality fields in DRAYTON

There are 3608 empty locality fields in WILSONTON

There are 1865 empty locality fields in NORTH TOOWOOMBA

There are 4320 empty locality fields in NEWTOWN

There are 2629 empty locality fields in EAST TOOWOOMBA

There are 109 empty locality fields in REDWOOD

There are 283 empty locality fields in PRINCE HENRY HEIGHTS

There are 3371 empty locality fields in HARRISTOWN

There are 2637 empty locality fields in SOUTH TOOWOOMBA

There are 2448 empty locality fields in CENTENARY HEIGHTS

There are 3421 empty locality fields in RANGEVILLE

There are 2331 empty locality fields in KEARNEYS SPRING

There are 2445 empty locality fields in MIDDLE RIDGE

There are 2379 empty locality fields in TOOWOOMBA CITY


Have you tried using Spatial Join rather than doing the repeated Select By Location operations? It also has a WITHIN option. Then a cursor should read the Spatial Join result to a dictionary for the matching and the update would take under 10 minutes. Furthermore you own code should replace all of the old style cursors with da cursors, which run 10 times faster.

Anyway, just to fix your code the problem is in the condition within the while loop that runs the update cursor. You have if uRow[0] != "str(localityList[indexCounter]": which should be if uRow[0] != str(localityList[indexCounter]):. Your indentation is also wrong. See revised code section below.

    while sRow:
        with arcpy.da.UpdateCursor(fc, field) as cursor:
            for uRow in cursor:
                if uRow[0] != str(localityList[indexCounter]):
                    uRow[0] = str(localityList[indexCounter])
        sRow = sRows.next()

Although I think the problem goes deeper. It appears that both the searchCursor and the updateCursor are dealing with the "TWB_Property_Layer" which you must not do. If that is what you intended, then using the searchCursor and updateCursor together is both unnecessary and against the help recommendations and you must get rid of the searchCursor entirely and only use an updateCursor. You are not doing anything with the parcelList list in your code, so get rid of it, and the iteration of the counters can be handled within the updateCursor for loop.

  • Thank you for the quick response. I am running the script before I go to work and will check it later in the day. I had a feeling that it might have been writing to the layer and not the feature class. – Hazzamataz May 16 '16 at 20:46

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.