3

I need to write a script that updates the "LOCALITY" field in a feature class if it is empty with the suburb it is in.

I have identified the number of properties with select by location and the below script will show that 1340 properties in the first index of the localityList will need to be updated.

PyScripter says that the script runs ok but when I check the attribute table in ArcMap the LOCALITY field has not been updated.

How can I implement the update tool correctly? After that is done all I will need to do is loop through the localityList to move through all of the suburbs.

# 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"

# 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)

# Select by location
indexCounter = 0
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 = []
rows = arcpy.SearchCursor("TWB_Property_Layer")
row = rows.next()

while row:
    parcelList.append(row.OBJECTID)
    row = rows.next()

print str(len(parcelList))

numberParcel = len(parcelList)

for parcel in range(0, numberParcel):
    rows = arcpy.UpdateCursor("TWB_Property", '"LOCALITY" = ' + "'" + localityList[indexCounter] + "'")
    row = rows.next()

row.setValue("LOCALITY", str(localityList[indexCounter]))
rows.next()

del row, rows

arcpy.Delete_management("TWB_Property_Layer")
arcpy.Delete_management("TWB_Sububs_Layer")
  • 3
    you've left out rows.updateRow(row) after you setValue. See Update Cursor - ArcGIS Desktop Help – Midavalo May 10 '16 at 1:23
  • 3
    You should also look into the Data Access Update Cursor (arcpy.da.UpdateCursor) as these are usually much better/faster/more efficient than the old Update Cursor (arcpy.UpdateCursor). See Data Access Update Cursor - ArcGIS Desktop Help – Midavalo May 10 '16 at 1:25
  • 4
    You've got a search and update cursor both rows and row, that's not good. It makes your code a little confusing and you risk overwriting the variables.. or (even worse) trying to store a row retrieved by a search cursor in a different feature class! try sRows/sRow uRows/uRow to separate the search and update cursors; also consider using the arcpy.da cursors, they're a bit more logical. – Michael Stimson May 10 '16 at 1:26
2

You need to include rows.updateRow(row) to save your changes to each row of your Update Cursor:

row.setValue("LOCALITY", str(localityList[indexCounter]))
rows.updateRow(row) # save the change to the row
rows.next()

However you should also take note of the comments above about using the arcpy.da.UpdateCursor and about how you've named both your SearchCursor and UpdateCursor to use rows and row.

  • Thank you. I keep getting an error about an invalid SQL statement. I think it is trying to pull all of the values from TWB_Suburbs into the one field of TWB_Property. That is a different issue though. – Hazzamataz May 10 '16 at 10:00

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.