1

I am trying to select address points from address feature class within a selected zip code from zipcode feature class and populate the address feature class field "ZipCode" with the "Zip_Code" field from the zipcode feature class that is selected. What I have so far doesn't produce an error, but continues to run and the ZipCode field is not being populated.

I am new to python.

Following is what I have so far.

#Set variables for feature classes
addresses = "Addresses"
zipcodes = "Zip_Codes"

#Perform Select by Location to create the zipcode data from ZipCodes to Addresses
#Create layer file
address_layer = "addr"
zipcode_layer = "zip"

if arcpy.Exists(zipcode_layer):
    arcpy.Delete_management(zipcode_layer)

arcpy.MakeFeatureLayer_management(zipcodes, zipcode_layer)
result = arcpy.GetCount_management(zipcode_layer)
print("There are {0} zip codes in the Zip_Codes feature class".format(result))

if arcpy.Exists(address_layer):
    arcpy.Delete_management(address_layer)

arcpy.MakeFeatureLayer_management(addresses, address_layer)

#Update zipcode field in Addresses feature class based on selectbylocation on Zip_Codes
#Initiate count to 1
count = 1
fieldListA = ["ZipCode"] #Zip code field from addresses variable
fieldListZ = ["Zip_Code"] #Zip code field from zipcodes variable
with arcpy.da.UpdateCursor(addresses, fieldListA) as upCursor:
    for row in upCursor:
        #Select all address points within the selected Zip_Code field within zipcode_layer
        arcpy.SelectLayerByLocation_management(address_layer, "WITHIN", zipcode_layer)
        result = arcpy.GetCount_management(address_layer)
        #print("There are {0} addresses".format(result))

        strZip = row[0]
        upCursor.updateRow(row)

        count = count + 1
1
  • 3
    Could you edit your post and remove this image, and instead copy and paste your code using code blocks? See gis.stackexchange.com/tour
    – GISHuman
    Nov 29, 2017 at 18:01

2 Answers 2

1

From your code it looks like you want to populate a field in the adress layer with the number of selected features and not the actual zip codes from zip_code layer.

You are not assigning a value to row, so upCursor.updateRow(row) will do nothing. An assignment is for example:

row[0] = 12345

If you want to fetch the zip code from overlapping zip code polygon you can do i with a cursor inside the cursor, se code below. I dont recommend this on large data sets since it will have to go through all polygons for each Point. You should use Spatial Join tool instead.

import arcpy

address = r'C:\database.gdb\adresses'
zipcodes = r'C:\database.gdb\zipcodes'

with arcpy.da.UpdateCursor(address, ['SHAPE@','zipfield_to_update']) as ucursor:
    for update_row in ucursor:
        with arcpy.da.SearchCursor(zipcodes, ['SHAPE@', 'zipfield_to_read']) as scursor:
            for search_row in scursor:
                if update_row[0].within(search_row[0]):
                    update_row[1] = search_row[1]
                    ucursor.updateRow(update_row)

You could also do this on selections, like you try in your code and it will propably be quicker.

6
  • I am new to python, so I am sure I have it built wrong. I do want to populate a field in the address layer with the actual zip code - not the number of selected features.
    – Sandy
    Nov 29, 2017 at 19:54
  • @Sandy: There is no need for a script, use Spatial Join: pro.arcgis.com/en/pro-app/tool-reference/analysis/…
    – BERA
    Nov 29, 2017 at 19:55
  • If you actually want to do it your way you would need a search cursor within the updatecursor to read the zip code of the selected features. But dont do that when there is spatial join
    – BERA
    Nov 29, 2017 at 20:20
  • I need to use this in a script that will be ran after hours.
    – Sandy
    Nov 29, 2017 at 20:21
  • You can use Spatial Join in a script
    – BERA
    Nov 29, 2017 at 20:36
0

In your code snippet below you create a row object named row on the second line, and then on the last line you update the cursor row using the current value of row which you have not modified in between. You have read the first item in row when you did strZip = row[0] but you have not written anything to it.

with arcpy.da.UpdateCursor(addresses, fieldListA) as upCursor:
    for row in upCursor:
        #Select all address points within the selected Zip_Code field within zipcode_layer
        arcpy.SelectLayerByLocation_management(address_layer, "WITHIN", zipcode_layer)
        result = arcpy.GetCount_management(address_layer)
        #print("There are {0} addresses".format(result))

        strZip = row[0]
        upCursor.updateRow(row)

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.