Unable to copy 2 different selected features from the same dataset to a new feature class:

580
4
Jump to solution
12-11-2018 06:41 AM
GustavoColmenares
New Contributor

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())

0 Kudos
1 Solution

Accepted Solutions
RandyBurton
MVP Alum

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())‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

4 Replies
StephenM
Occasional Contributor II

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)?

0 Kudos
GustavoColmenares
New Contributor

Stephen M, thank you for your comment  I believe the problem was that I had the wrong variable selected.

0 Kudos
RandyBurton
MVP Alum

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())‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
GustavoColmenares
New Contributor

Randy Burton I made some changes that you recommended and It worked,
thank you

0 Kudos