在ArcPy中开发地理处理工具

时间:2015-08-06 18:02:08

标签: python gis arcpy

我试图在地理处理工具中制作可在ArcMap中重复使用的工具,但我收到了以下错误。这看起来像是我格式化where_query变量的方式的一个问题,但我似乎无法做到正确。

Traceback (most recent call last):
  File "C:\ArcPy\mean_center_drift.py", line 19, in <module>
    arcpy.Select_analysis(in_feature, year_out_name, where_query)
  File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\analysis.py", line 84, in Select
    raise e
ExecuteError: ERROR 000358: Invalid expression 1
Failed to execute (Select).

以下是当前形式的代码。

import os
import arcpy

in_feature  = arcpy.GetParameterAsText(0)
out_features  = arcpy.GetParameterAsText(1)
origin_year = arcpy.GetParameterAsText(2)
field_name = arcpy.GetParameterAsText(3)

for x in range(10, 140, 10):
    year_range = int(origin_year) + x
    where_query = 'field_name' <= "{0}.format(year_range)"

    year_out_name = os.path.join(out_features, "Years_{0}".format(x))
    mean_out_name = os.path.join(out_features, "Mean_{0}".format(x))

    arcpy.Select_analysis(in_feature, year_out_name, where_query)

    arcpy.MeanCenter_stats(year_out_name, mean_out_name, "#", "#", "#")

非常感谢您提供的任何帮助!

2 个答案:

答案 0 :(得分:1)

>>> where_query = 'field_name' <= "{0}.format(year_range)" >>> where_query True >>> year_range = 2010 >>> where_query = "'field_name' <= {0}".format(year_range) >>> where_query "'field_name' <= 2010" >>> 构造错误。

arcpy

我不熟悉field_name如果不需要print周围的单引号,请删除它们。

如果你没有使用带语法高亮的编辑器,你应该考虑它。

{{1}}语句/函数是检查可疑变量的快捷方法。

答案 1 :(得分:0)

Someone in the GIS StackExchange能够帮助我使用以下解决方案。

import os
import arcpy

in_feature  = arcpy.GetParameterAsText(0)
out_features  = arcpy.GetParameterAsText(1)
origin_year = arcpy.GetParameterAsText(2)
field_name = arcpy.GetParameterAsText(3)

path = arcpy.Describe(in_feature).path
new_field_name = arcpy.AddFieldDelimiters(path, field_name)

for x in range(10, 140, 10):
    year_range = int(origin_year) + x
    where_query = """{0} <= {1}""".format(new_field_name, year_range)

    year_out_name = os.path.join(out_features, "Years_{0}".format(x))
    mean_out_name = os.path.join(out_features, "Mean_{0}".format(x))

    arcpy.Select_analysis(in_feature, year_out_name, where_query)

    arcpy.MeanCenter_stats(year_out_name, mean_out_name, "#", "#", "#")
相关问题