使用Where语句中的变量在for循环中按属性选择

时间:2014-01-15 21:13:58

标签: python gis arcpy

我见过几个与此技术相关的类似帖子,但我仍然遇到问题。以下脚本用于从一个shapefile中选择位于第二个多边形shapefile边界内的所有点,然后编辑选定到其中一个多边形字段中的点数。我无法使where子句语句正常运行,尽管从错误消息看它是正确的。两个shapefile都位于文件地理数据库和要素层中,为了使用选择函数而为两者创建。

arcpy.MakeFeatureLayer_management("treatment_maps",'maps')
arcpy.MakeFeatureLayer_management("points_2014",'points')
cursor=arcpy.UpdateCursor("treatment_maps")

for row in cursor:
    map=row.getValue('map_name')
    where='"map_name"' + '=' + '\'' + str(map) + '\''
    arcpy.SelectLayerByAttribute_management("maps","NEW_SELECTION",where)
    arcpy.SelectLayerByLocation_management("points","COMPLETELY_WITHIN","maps")
    count=arcpy.GetCount_management("points")
    row.setValue('DI_Count', count)
    cursor.updateRow(row)

错误讯息:

Executing: SelectLayerByAttribute maps NEW_SELECTION "map_name"='Airport'
Start Time: Wed Jan 15 13:00:56 2014
ERROR 000358: Invalid expression
Cannot acquire a lock.
Cannot acquire a lock.
Failed to execute (SelectLayerByAttribute).
Failed at Wed Jan 15 13:00:56 2014 (Elapsed Time: 0.00 seconds)

treatment_maps要素类包含位于每个多边形内的点的多边形和计数(DI_Count),需要更新这些点。我希望让这个脚本正常工作,因为我有大约100个多边形要更新。感谢您的帮助或建议!

更新: 在对每一行进行试验后,我发现了按属性选择'其中'语句正常运行但是当光标设置在同一对象上时,属性选择功能不起作用。我仍然需要一个解决方案。

1 个答案:

答案 0 :(得分:0)

我不确定。但ArcGIS帮助示例中的字段表达式是:[map_name],而不是“map_name”。 尝试将线路6替换为:
where='[map_name]' + '=' + '\'' + str(map) + '\''
祝你好运!