1

I'm using ArcMap 10.2 and want a python script to loop through a list to make new feature classes, using the list items as attribute values in a selection before copying the features to a new file.

The script makes the first feature class fine but then copies all of the features for the next two iterations of the loop.

import arcpy

arcpy.env.workspace = r"C:\RS_Data\SHP"
arcpy.env.overwriteOutput = True

amenities = ['school', 'hospital', 'place_of_worship']
points = r"C:\RS_Data\SHP\OSMpoints.shp"
arcpy.MakeFeatureLayer_management(points, "OSM_Lyr")

with arcpy.da.SearchCursor("OSM_Lyr","amenity") as cursor:
    for amenity in amenities:
        query3 = '"amenity" = ' + "'" + amenity + "'"
        arcpy.SelectLayerByAttribute_management("OSM_Lyr", "CLEAR_SELECTION")
        for row in cursor:
            if row[0] == amenity:
                arcpy.SelectLayerByAttribute_management("OSM_Lyr", "ADD_TO_SELECTION",query3)
        arcpy.CopyFeatures_management("OSM_Lyr", amenity)
        print query3

Here is the print statement I get for the query after each loop:

"amenity" = 'school'
"amenity" = 'hospital'
"amenity" = 'place_of_worship'

It seems the selection is working the first time but not for the next two iterations. If I take the CLEAR_SELECTION line out it makes three identical feature classes using the same attribute value. How can I get the loop to make a selection on the next value in the list and copy the features to a new file?

3
  • 2
    Instead of using MakeFeatureLayer_management, SelectLayerByAttribute_management, CopyFeatures_management and an arcpy.da.SearchCursor I think you should investigate the Select_analysis tool which should bring this down to about 5 lines of code. Just iterate through your list of values and feed them to Select_analysis.
    – PolyGeo
    Commented Jul 13, 2016 at 0:48
  • 1
    Or the where clause on the search cursor... Not that I can see why you're using a cursor here. To me it seems like you should loop through your amenities (like you are) and Select like PolyGeo said, MakeFeatureLayer with where_clause or SelectLayerByAttribute and copy features - no need for a cursor here at all. Commented Jul 13, 2016 at 0:53
  • 2
    and no need for MakeFeatureLayer or SelectLayerByAttribute or CopyFeatures either - Select_analysis() replaces all that.
    – PolyGeo
    Commented Jul 13, 2016 at 1:15

1 Answer 1

0

Using Select as @PolyGeo and @Michael Miles-Stimson say will indeed simplify your code:

import arcpy

arcpy.env.workspace = r"C:\RS_Data\SHP"
arcpy.env.overwriteOutput = True

amenities = ['school', 'hospital', 'place_of_worship']
points = r"C:\RS_Data\SHP\OSMpoints.shp"

for amenity in amenities:
    query3 = '"amenity" = ' + "'" + amenity + "'"
    arcpy.Select_analysis(points, amenity, query3)

For the rest your code with cursor and layers looks right, you should maybe replace "ADD_TO_SELECTION" with "NEW_SELECTION" in the second SelectLayerByAttribute (and the first SelectLayerByAttribute will then become unnecessary).

1
  • I used Michael's solution as this is for a class assignment and we haven't covered Select_analysis yet, although it seems to me that this is an improvement over SelectLayerByAttribute by obviating the MakeFeatureLayer step. Thanks!
    – geoJshaun
    Commented Jul 13, 2016 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.