使用 ArcPy 识别空间连接参数时出错

Error recognizing parameters for a spatial join using ArcPy

我正在尝试通过文件夹迭代空间连接 - 然后通过第一个空间连接的输出迭代第二个空间连接。

这是我的初始脚本:

import arcpy, os, sys, glob

'''This script loops a spatial join through all the feature classes
in the input folder, then performs a second spatial join on the output 
files'''


#set local variables

input = "C:\Users\Ryck\Test\test_Input"
boundary = "C:\Users\Ryck\Test\area_Input\boundary_Test.shp"
admin = "C:\Users\Ryck\Test\area_Input\admi_Boundary_Test.shp"
outloc = "C:\Users\Ryck\Test\join_02"

#overwrite any files with the same name
arcpy.env.overwriteOutput = True

#perform spatial joins

for fc in input:
    outfile = outloc + fc
    join1 = [arcpy.SpatialJoin_analysis(fc,boundary,outfile) for fc in 
            input]

    for fc in join1:
        arcpy.SpatialJoin_analysis(fc,admin,outfile)

我一直收到错误 00732:目标特征:数据集 C 不存在或不受支持。

我确定这是一个简单的错误,但是 none 以前推荐的解决此错误的解决方案允许我仍然将我的结果输出到他们自己的文件夹。

提前感谢您的任何建议

您似乎在尝试遍历给定目录,对其中包含的(形状文件?)执行空间连接。

但是,这个语法有问题:

input = "C:\Users\Ryck\Test\test_Input"
for fc in input:
    # do things to fc

在这种情况下,for 循环是 iterating over a string。所以每次通过循环,它一次接受一个字符:首先是 C,然后是 :,然后是 \... 当然,arcpy 函数会因这个输入而失败,因为它需要一个文件路径,而不是一个字符。因此出现错误:目标特征:数据集 C 不存在...


要在您的输入目录中循环遍历 文件 ,您需要几个额外的步骤。 Build a list of files,然后遍历该列表。

arcpy.env.workspace = input            # sets "workspace" to input directory, for next tool
shp_list = arcpy.ListFiles("*.shp")    # list of all shapefiles in workspace
for fc in shp_list:
    # do things to fc

(参考 GIS.SE 上的 this answer。)

在解决了一些问题之后,感谢@erica 的建议,我决定放弃我最初的嵌套 for 循环概念,并采用更简单的方法。我仍在开发一个 GUI,它将创建可以分配给变量的系统参数,然后用作空间连接的参数,但现在,这是我已经制定的解决方案。

import arcpy

input = "C:\Users\Ryck\Test\test_Input\"
boundary = "C:\Users\Ryck\Test\area_Input\boundary_Test.shp"
outloc = "C:\Users\ryck\Test\join_01"
admin = "C:\Users\Ryck\Test\area_Input\admin_boundary_Test.shp"
outloc1 = "C:\Users\Ryck\Test\join_02"

arcpy.env.workspace = input
arcpy.env.overwriteOutput = True

shp_list = arcpy.ListFeatureClasses()

print shp_list

for fc in shp_list:
    join1 = 
arcpy.SpatialJoin_analysis(fc,boundary,"C:\Users\ryck\Test\join_01\" + 
                           fc)

arcpy.env.workspace = outloc

fc_list = arcpy.ListFeatureClasses()

print fc_list

for fc in fc_list:
    arcpy.SpatialJoin_analysis(fc,admin,"C:\Users\ryck\Test\join_02\" + 
                               fc)

设置多个环境并使用实际路径感觉很笨拙,但在这一点上对我有用。