I'm trying to create a script tool to select all the features within a user-specified area of interest then run the Data Reviewer extension. I've encountered an issue trying to select features from a feature dataset. There are multiple feature datasets in my sde database, and I want to select all feature classes from one specific feature dataset. The code below runs up to the selected_aoi
variable.
Any suggestions on how to solve this?
import arcpy, os
arcpy.env.overwriteOutput = "true"
# User path to reviewer workspace
rev_workspace = arcpy.GetParameterAsText(0)
# RBJ file path
rbj_file = arcpy.GetParameterAsText(1)
# Open session
session_num = arcpy.GetParameterAsText(2)
# Select user AOI's using SQL
user_AOI = arcpy.GetParameterAsText(3)
# Path to production workspace
prod_workspace = r"X:\DataConnections\Utilities.sde"
# Path to the Water Distribution Feature Dataset
wat_fc_dataset = os.path.join(prod_workspace, "/Utilities.WaterDistribution")
# Select the AOI and Run Data Reviewer
try:
arcpy.AddMessage("Starting Process")
aoi_fc_select = os.path.join(prod_workspace +
"/Utilities.ModelCorrections/Utilities.WaterOperationalAreaCorrections")
arcpy.AddMessage("Selecting AOI")
arcpy.MakeFeatureLayer_management(aoi_fc_select, "selected_aoi_lyr", user_AOI)
arcpy.AddMessage("AOI Selected")
arcpy.AddMessage("Selecting features in AOI")
selected_aoi = arcpy.SelectLayerByLocation_management("selected_aoi_layer", "intersect", wat_fc_dataset)
arcpy.AddMessage("Features in AOI selected")
# Run the RBJ file
arcpy.AddMessage("Running Data Reviewer on selected features")
arcpy.ExecuteReviewerBatchJob_Reviewer(rev_workspace,session_num,rbj_file,prod_workspace, selected_aoi)
arcpy.AddMessage("Process Completed")
except:
arcpy.AddMessage("Failed to Execute")
1 Answer 1
There is a typo in the code when trying to select the layer.
Make Feature Layer code:
arcpy.MakeFeatureLayer_management(aoi_fc_select, "selected_aoi_lyr", user_AOI)
Selection code:
selected_aoi = arcpy.SelectLayerByLocation_management("selected_aoi_layer", "intersect", wat_fc_dataset)
The code made a layer called "selected_aoi_lyr" but it is trying to select from a non-existent "selected_aoi_layer" ("lyr" vs "layer").
This is a common accident to make when using MakeFeatureLayer. A great way of avoiding this is to assign new feature layers to variables instead; like so:
arcpy.AddMessage("Selecting AOI")
feature_layer = arcpy.MakeFeatureLayer_management(aoi_fc_select, "selected_aoi_lyr", user_AOI)
arcpy.AddMessage("AOI Selected")
arcpy.AddMessage("Selecting features in AOI")
selected_aoi = arcpy.SelectLayerByLocation_management(feature_layer , "intersect", wat_fc_dataset)
arcpy.AddMessage("Features in AOI selected")
That way instead of having to write the string name for the feature layer, it uses a variable instead. Most IDE's would then say if a typo was made as the variable wouldn't exist, but won't tell you if it is in a string.
i.e
IDE's will say this syntax looks ok
arcpy.MakeFeatureLayer_management(aoi_fc_select, "feature_layer", user_AOI)
arcpy.SelectLayerByLocation_management("feature_lyr", "intersect", wat_fc_dataset)
IDE's will say that feature_lyr doesn't exist
feature_layer = arcpy.MakeFeatureLayer_management(aoi_fc_select, "feature_layer", user_AOI)
arcpy.SelectLayerByLocation_management(feature_lyr, "intersect", wat_fc_dataset)
-
Thank you! Can't believe I overlooked that. How could I go about selecting all features in multiple feature classes by location?cb101– cb1012022年06月03日 13:56:05 +00:00Commented Jun 3, 2022 at 13:56
Explore related questions
See similar questions with these tags.