In my script I intent to select by location some features(polygons) from feature class call "opt_area", then I want to select other features from the same feature class using Select by attribute, then copy the selected features to a new feature class, but is not working. What it does is copying the entire feature class, not the features selected by SelectByLocation and SelectByAttribute
try:
arcpy.MakeFeatureLayer_management(opt_areas, opt_areas_layer)
# Process: Selecting by Location; Use Selecting by Location to select features that are intersected by roads.
arcpy.SelectLayerByLocation_management(opt_areas_layer, "INTERSECT", roads, "", "NEW_SELECTION", "") print("Select by location completed")
# Process: Select by Attribute
arcpy.SelectLayerByAttribute_management(opt_areas_layer, "ADD_TO_SELECTION", where_clause="Shape_Area >= 40469")
print("Select by attribute completed")
except:
print(arcpy.GetMessages())
Solved! Go to Solution.
In your code, you first selected those features in your layer that are intersected by roads. Rather than adding to that selection, you want to remove features from the selection where the shape area does not meet your criteria. Then you can save the selected features.
try:
arcpy.MakeFeatureLayer_management(opt_areas, opt_areas_layer)
# Process: Selecting by Location; Use Selecting by Location to select features that are intersected by roads.
arcpy.SelectLayerByLocation_management(opt_areas_layer, "INTERSECT", roads, None, "NEW_SELECTION", None)
print("Select by location completed")
# Process: Select by Attribute
arcpy.SelectLayerByAttribute_management(opt_areas_layer, "REMOVE_FROM_SELECTION", where_clause="Shape_Area < 40469")
print("Select by attribute completed")
except:
print(arcpy.GetMessages())
That code looks like it should work. Are you using the Feature Class To Feature Class tool to copy the features? Maybe all of the features match one of those two conditions (intersect roads or Shape_Area >= 40469)?
Stephen M, thank you for your comment I believe the problem was that I had the wrong variable selected.
In your code, you first selected those features in your layer that are intersected by roads. Rather than adding to that selection, you want to remove features from the selection where the shape area does not meet your criteria. Then you can save the selected features.
try:
arcpy.MakeFeatureLayer_management(opt_areas, opt_areas_layer)
# Process: Selecting by Location; Use Selecting by Location to select features that are intersected by roads.
arcpy.SelectLayerByLocation_management(opt_areas_layer, "INTERSECT", roads, None, "NEW_SELECTION", None)
print("Select by location completed")
# Process: Select by Attribute
arcpy.SelectLayerByAttribute_management(opt_areas_layer, "REMOVE_FROM_SELECTION", where_clause="Shape_Area < 40469")
print("Select by attribute completed")
except:
print(arcpy.GetMessages())
Randy Burton I made some changes that you recommended and It worked,
thank you