Arcpy,select 基于部分字符串的特征
Arcpy, select features based on part of a string
所以对于我的例子,我有一个很大的州立公园 shapefile,其中一些是真正的公园,而另一些只是小径。但是,没有列定义哪些是小径与实际公园,我想 select 那些是小径并删除它们。我确实有一列用于每个功能的名称,通常在字符串的某处包含单词 "trail"。然而,它并不总是在开始或结束时。
我只在基础水平上熟悉 Python,虽然我可以手动完成 select 我想要的,但我很好奇它是否可以自动化。我一直在使用 arcpy.Select_analysis 并尝试在我的 where_clause 中使用 "LIKE" 并且看到了使用切片的示例,但未能获得有效的解决方案。我也尝试过使用 'is in' 函数,但我不确定我是否正确地使用了 where_clause。我可能只是不太了解在询问和搜索时要使用的正确术语。任何帮助表示赞赏。我一直在 ArcMap 10.3 中使用 Python Window。
目前我在:
arcpy.Select_analysis ("stateparks", "notrails", ''trail' is in \"SITE_NAME\"')
虽然使用 Select 工具是一个不错的选择,但 SQL 表达式的语法可能是一个挑战。考虑使用 Update Cursor 来解决这个问题。
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
所以对于我的例子,我有一个很大的州立公园 shapefile,其中一些是真正的公园,而另一些只是小径。但是,没有列定义哪些是小径与实际公园,我想 select 那些是小径并删除它们。我确实有一列用于每个功能的名称,通常在字符串的某处包含单词 "trail"。然而,它并不总是在开始或结束时。
我只在基础水平上熟悉 Python,虽然我可以手动完成 select 我想要的,但我很好奇它是否可以自动化。我一直在使用 arcpy.Select_analysis 并尝试在我的 where_clause 中使用 "LIKE" 并且看到了使用切片的示例,但未能获得有效的解决方案。我也尝试过使用 'is in' 函数,但我不确定我是否正确地使用了 where_clause。我可能只是不太了解在询问和搜索时要使用的正确术语。任何帮助表示赞赏。我一直在 ArcMap 10.3 中使用 Python Window。
目前我在:
arcpy.Select_analysis ("stateparks", "notrails", ''trail' is in \"SITE_NAME\"')
虽然使用 Select 工具是一个不错的选择,但 SQL 表达式的语法可能是一个挑战。考虑使用 Update Cursor 来解决这个问题。
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