Select to view content in your preferred language

Iterate through features and select by location

2718
11
03-05-2020 03:34 PM
NataliaGutierrez1
Regular Contributor

Hello,

I have a layer with Zoning Overlays. I have a layer with parcels. I need to iterate through zoning Overlay, select the parcels falling inside, add a new field to the parcels Feature class and add some text to the selected fields, then repeat for the next zoning overlay. The loop works, insofar but it stops with a particular error that is not specific enough. 

The code runs until it has to assign the values to the second field. 

Please see below for more details.

Here's my code:

Import arcpy

# Set two geoprocessing environments
arcpy.env.workspace = r"D:\APRX, MXDS\Geo_Engine_Zoning_Project\Austin_Geo_Engine.gdb"
arcpy.env.overwriteOutput = True

# List of fields in Merged_Ovelays FC
fields = ["OBJECTID", "zoning_ove"]

# Convert austin_parcels feature class to feature layer
arcpy.MakeFeatureLayer_management("austin_parcels", "austin_parcels_layer")

# Convert Merged_Overlays feature class to feature layer
arcpy.MakeFeatureLayer_management("Merged_Overlays", "Merged_Overlays_Layer")

# arcpy.da.SearchCursor(in_table, field_names, {where_clause}, {spatial_reference}, {explode_to_points}, {sql_clause})
with arcpy.da.SearchCursor("Merged_Overlays_Layer", fields) as cursor:
    for row in cursor:
        select = "OBJECTID = {}".format(row[0])
# SelectLayerByAttribute(in_layer_or_view, {selection_type}, {where_clause}, {invert_where_clause})
        arcpy.management.SelectLayerByAttribute("Merged_Overlays_Layer", "NEW_SELECTION", select)
# SelectLayerByLocation(in_layer, {overlap_type}, {select_features}, {search_distance}, {selection_type}, {invert_spatial_relationship})
        arcpy.management.SelectLayerByLocation("austin_parcels_layer", "HAVE_THEIR_CENTER_IN", "Merged_Overlays_Layer","","NEW_SELECTION")
# Syntax: AddField(in_table, field_name, field_type, {field_precision}, {field_scale}, {field_length}, {field_alias}, {field_is_nullable}, {field_is_required}, {field_domain})
        arcpy.management.AddField('austin_parcels_layer', "Overlay_{}".format(row[1]), "TEXT")
# CalculateField(in_table, field, expression, {expression_type}, {code_block}, {field_type})
        arcpy.management.CalculateField("austin_parcels_layer", "Overlay_{}".format(row[1]), '"Overlay_{}".format(row[1])')

I am getting the following error and I am not sure how to fix it:

Traceback (most recent call last):
File "<string>", line 24, in <module>
File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\management.py", line 4230, in CalculateField
raise e
File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\management.py", line 4227, in CalculateField
retval = convertArcObjectToPythonObject(gp.CalculateField_management(*gp_fixargs((in_table, field, expression, expression_type, code_block), True)))
File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\geoprocessing\_base.py", line 506, in <lambda>
return lambda *args: val(*gp_fixargs(args, True))
arcgisscripting.ExecuteError: ERROR 999999: Something unexpected caused the tool to fail. Contact Esri Technical Support (http://esriurl.com/support) to Report a Bug, and refer to the error help for potential solutions or workarounds.
Invalid pointer
Failed to execute (CalculateField).

Is it something with my line of code that is not working?

arcpy.management.CalculateField("austin_parcels_layer", "Overlay_{}".format(row[1]), '"Overlay_{}".format(row[1])')

My goal is to:

Copy the text from the selected feature below:

and put it into the newly created field. 

I would really appreciate your help,

Thank you so much!

Natalia

0 Kudos
11 Replies
NataliaGutierrez1
Regular Contributor

Thank you!

0 Kudos
JoeBorgione
MVP Emeritus

These guys are my go-to python compradres (along with Dan Patterson‌) and all of them have younger eyes and could read your code better than I could!

That should just about do it....
0 Kudos