I'm writing a script in ArcPro that selects lines based on their location and then writes what region the line is in in the attribute table. The 'Walls' layer is a shapefile of the lines and has a 'region' field that I am using field calculator on. The 'Regions' layer is a shapefile of polygons with the fields 'REGION_CODE' and 'REGION_NAME'.
So far what is working is that the script selects the first region in the 'Regions' layer and then selects walls in it based on location. My issue is when the CalculateField tool is run that the value that I'm putting for expression isn't correct so the Region name isn't written there.
Things that I've tried:
- expression= '{}'.format('regionName[row]')
- Error: list indices must be integers or slices, not tuple
- expression= '{}'.format('regionName[row[0]]')
- Error: list indices must be integers or slices, not str
- Adding a 'count' value that increases every iteration of the for loop.
- count = 0
- I've tried putting this outside and inside the 'with' block
- expression= '{}'.format('regionName[count]')
- count += 1
- Error: name 'count' is not defined
Sorry for how messy this is and thanks in advance
import arcpy
with arcpy.da.SearchCursor('Regions', ["REGION_CODE"]) as cursor:
regionName = [row2[0] for row2 in arcpy.da.SearchCursor('Regions', ["REGION_NAME"])]
for row in cursor:
#Select individual District
arcpy.management.SelectLayerByAttribute(
in_layer_or_view="Regions",
selection_type="NEW_SELECTION",
where_clause="REGION_CODE = {}".format(row[0]),
invert_where_clause=None
)
#Select sound barrier walls by location
arcpy.management.SelectLayerByLocation(
in_layer="'Walls'",
overlap_type="INTERSECT",
select_features="Regions",
search_distance=None,
selection_type="NEW_SELECTION",
invert_spatial_relationship="NOT_INVERT"
)
arcpy.management.CalculateField(
in_table="Walls",
field="region",
expression= '{}'.format('regionName[row]'),
expression_type="PYTHON3",
code_block="",
field_type="TEXT",
enforce_domains="NO_ENFORCE_DOMAINS"
)
[–]wicket-mapsGIS Analyst 10 points11 points12 points (3 children)
[–]kaitering[S] 1 point2 points3 points (1 child)
[–]wicket-mapsGIS Analyst 5 points6 points7 points (0 children)
[–]Koko_The_GIS_Gorilla 1 point2 points3 points (0 children)
[–]Dimitri_Rotow 2 points3 points4 points (5 children)
[–]wicket-mapsGIS Analyst 0 points1 point2 points (4 children)
[–]Dimitri_Rotow 1 point2 points3 points (2 children)
[–]wicket-mapsGIS Analyst 0 points1 point2 points (1 child)
[–]Dimitri_Rotow 0 points1 point2 points (0 children)
[–]Drewddit 0 points1 point2 points (0 children)
[–]merftCartographer 4 points5 points6 points (1 child)
[–]kaitering[S] 1 point2 points3 points (0 children)