2016-08-19 77 views
-1

因此,对于我的示例,我有一个州公园的大型shapefile,其中有些是实际的公园,其他的只是路径。然而,没有列定义哪些是实际公园的列,我想选择那些是路径并删除它们。我有一个专栏的名称,通常在字符串中包含单词“trail”。然而,它并不总是在开始或结束。Arcpy,根据字符串的一部分选择要素

我只在基本水平上熟悉Python,虽然我可以通过手动选择我想要的,但我很好奇,看看它是否可以自动化。我一直在使用arcpy.Select_analysis并尝试在我的where_clause中使用“LIKE”,并且已经看到使用切片的示例,但尚未能获得工作解决方案。我也试过使用'is in'函数,但我不确定我是否正确地使用where_clause。在询问和搜索时,我可能没有足够的把握正确的用词。任何帮助表示赞赏。我一直在ArcMap 10.3中使用Python窗口。

目前我在:

arcpy.Select_analysis( “stateparks”, “notrails”, '' 踪迹 '是\ “SITE_NAME \”')

回答

0

虽然使用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 
+0

抱歉这么久,但这个伎俩!谢谢!起初没有任何行被删除,直到我意识到搜索字符串区分大小写。卫生署!感谢你的宝贵时间! – Koelker12

相关问题