I'm trying to update missing values in one .shp from another .shp. I'm using SelectLayerByLocation to identify the properties which lie within the boundaries of a suburb and then using a UpdateCursor to update the properties list with the suburbs. The code below doesn't iterate through the list correctly so all I get is the first value from the initial SearchCursor updating the properties list.

fc_P = propertyDataset
fc_S = suburbsDataset
query = '"LOCALITY" =\'\''
field_P = ["LOCALITY"]
field_S = ["ADMINAREA"]

arcpy.MakeFeatureLayer_management(fc_P,"Properties_layer", query)

arcpy.SelectLayerByLocation_management("Properties_Layer", "WITHIN", 

with arcpy.da.SearchCursor("Suburbs_layer",field_S) as Srow:
    for row in Srow:
        Suburb = Srow[0]

with arcpy.da.UpdateCursor("Properties_layer", field_P) as Ucursor:
    for Urow in Ucursor:
        Urow[0] = Suburb

del Ucursor
del Srow

suburbs.shp attribute table properties.shp attribute table

  • You are not iterating over localitylist in the code you present (for locality in localitylist:) – BERA Apr 22 at 7:53
  • i was using the localitylist to get a count of properties updated later in the code. would it be best to append the list during the search cursor and then use that within the update cursor? – Brett Apr 22 at 7:57
  • Are you trying to update properties that does not have a suburb (ADMINAREA?)? A screenshot showing the attribute tables and maybe the map would help in understanding your question. – BERA Apr 22 at 8:04
  • thats exactly what i'm trying to do. the locality in the properties dataset is named adminarea in the suburbs dataset. some properties dont have localities/adminareas listed. – Brett Apr 22 at 8:20
  • when i run the code it grabs the result from the row 0 in the property .shp which is toowoomba city and uses that that to fill in the blanks. – Brett Apr 22 at 8:29

I think you should try using spatial join instead. That way you dont need to iterate Select Layer By Location and multiple cursors. The output can be written to in_memory so that it not saved to disk and the output is used to create a dictionary of objectids and their suburb.

Try code below, I have no data to try it on so you might get some errors:

import arcpy

propfc = 'Properties'
subfc = 'Suburbs'
propfc_suburbfield = 'LOCALITY'
subfc_suburbfield = 'ADMINAREA'

#Create feature layer of properties with LOCALITY shorter than 2 charachters
sql = "CHAR_LENGTH({0})<2".format(arcpy.AddFieldDelimiters(propfc, propfc_suburbfield))
arcpy.MakeFeatureLayer_management(in_features=propfc, out_layer='proplyr', where_clause=sql)

#Spatial join suburbs to properties layer
tempspatjoin = r'in_memory\spatjoin'
arcpy.SpatialJoin_analysis(target_features='proplyr', join_features=subfc, out_feature_class=tempspatjoin, join_type='KEEP_COMMON',\

#Create a dictionary of property objectids and what suburb they are in
updatedictionary = {k:v for k,v in arcpy.da.SearchCursor(tempspatjoin,['TARGET_FID',subfc_suburbfield])}

#Update proplyr with suburbs from dictionary
with arcpy.da.UpdateCursor('proplyr',['OID@',propfc_suburbfield]) as cursor:
    for row in cursor:
        if row[0] in updatedictionary: #If this rows oid is in dictionary
            row[1] = updatedictionary[row[0]] #Update with suburb

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.