I have been carrying out some manual analysis on a polygon dataset, selecting all points from a feature layer within 5km and populating the selected records using field calculator. What I am now wanting to do is use some Python to automate this, as I have several features layers to do this for.
This is what I have been running:
arcpy.management.SelectLayerByLocation( in_layer="Waterbodies", overlap_type="WITHIN_A_DISTANCE", select_features="Offtaker_Utility", search_distance="5 Kilometers", selection_type="NEW_SELECTION", invert_spatial_relationship="NOT_INVERT") arcpy.management.CalculateField( in_table="Waterbodies", field="Utility_5km", expression='"Yes"', expression_type="PYTHON3", code_block="", field_type="TEXT", enforce_domains="NO_ENFORCE_DOMAINS")
I could just continue to do this manually or even just edit the script to change 'select_features' in selectbylocation, and the 'field' in calculatefield, but I want to automate this if possible. I am fairly new to ArcPy and haven't used it much in the last 2-3 years, so slowly learning my way again and trying to automate some tasks using it to get some real world and work workflows setup.
I decided to do this in an ArcGIS Pro notebook, and so far I have:
This is the code:
arcpy.env.workspace = r"my file path\Datscha_output.gdb" feature_classes = arcpy.ListFeatureClasses() filtered_fcs = [fc for fc in feature_classes if "Offtaker" in fc] for fc in filtered_fcs: print (fc) print (filtered_fcs) for fc in filtered_fcs: arcpy.management.SelectLayerByLocation( in_layer="Waterbodies_Definitive_Version", overlap_type="WITHIN_A_DISTANCE", select_features=fc, search_distance="5 Kilometers", selection_type="NEW_SELECTION", invert_spatial_relationship="NOT_INVERT" )
This all works, but I also need to incorporate another geoprocessing task of calculating a field. This is where I am a bit stuck. I think I need to include the calculate field function into the above loop, as I need to tell the script to select all points from Offtaker_a, then calculate the relevant field in polygon dataset, then loop onto the next point feature of Ottaker_b etc. etc.
The calculate field function has a parameter for what field to update, so I presume I must need to create a variable to look at each field in the table, or similar?
Solved! Go to Solution.
Dan, glad I could help!