AnsweredAssumed Answered

Please help with update cursor. So close I can taste it.

Question asked by HKlein on May 16, 2016
Latest reply on May 17, 2016 by curtvprice

Hello,

I need some help with this script I am developing. I need the update cursor to overwrite the empty LOCALITY values in TWB_Property with the ADMINAREA from TWB_Suburbs that I have turned into a list. I know the code is a little messy and I also can not figure out how to display it all using this forum. Any help will be appreciated. Thank you.

 

code begins below.

 

# 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:
    localityList.append(str(row.ADMINAREA))
    #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
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:
            parcelList.append(sRow)
            sRow = sRows.next()
    print ("There are " + str(len(parcelList)) + " empty locality fields in " + localityList[indexCounter])
    numberParcel = int(len(parcelList))
    with arcpy.da.UpdateCursor(fc, field) as cursor:
            for uRow in cursor:
                if uRow[0] == '':
                    uRow[0] = str(localityList[indexCounter])
                    cursor.updateRow(uRow)

    arcpy.Delete_management("TWB_Sububs_Layer")
    counter += 1
    indexCounter += 1

del uRow

arcpy.Delete_management("TWB_Property_Layer")
arcpy.Delete_management("TWB_Sububs_Layer")

Outcomes