Search code examples
pythonstringselectarcpy

Arcpy, select features based on part of a string


So for my example, I have a large shapefile of state parks where some of them are actual parks and others are just trails. However there is no column defining which are trails vs actual parks, and I would like to select those that are trails and remove them. I DO have a column for the name of each feature, that usually contains the word "trail" somewhere in the string. It's not always at the beginning or end however.

I'm only familiar with Python at a basic level and while I could go through manually selecting the ones I want, I was curious to see if it could be automated. I've been using arcpy.Select_analysis and tried using "LIKE" in my where_clause and have seen examples using slicing, but have not been able to get a working solution. I've also tried using the 'is in' function but I'm not sure I'm using it right with the where_clause. I might just not have a good enough grasp of the proper terms to use when asking and searching. Any help is appreciated. I've been using the Python Window in ArcMap 10.3.

Currently I'm at:

arcpy.Select_analysis ("stateparks", "notrails", ''trail' is in \"SITE_NAME\"')


Solution

  • Although using the Select tool is a good choice, the syntax for the SQL expression can be a challenge. Consider using an Update Cursor to tackle this problem.

    import arcpy
    
    stateparks = r"C:\path\to\your\shapefile.shp"
    notrails = r"C:\path\to\your\shapefile_without_trails.shp"
    
    # Make a copy of your shapefile
    arcpy.CopyFeatures_management(stateparks, notrails)
    
    # Check if "trail" exists in the string--delete row if so
    with arcpy.da.UpdateCursor(notrails, "SITE_NAME") as cursor:
        for row in cursor:
            if "trails" in row[0]: # row[0] refers to the current row in the "SITE_NAME" field
                cursor.deleteRow() # Delete the row if condition is true