-1

I'm trying to iterate a spatial join through a folder - then iterate a second spatial join through the outputs of the first.

This is my initial script:

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)

I keep receiving Error00732: Target Features: Dataset C does not exist or is not supported.

I'm sure this is a simple error, but none of the solutions that have previously been recommended to solve this error allow me to still output my results to their own folder.

Thanks in advance for any suggestions

beauxgeo
  • 3
  • 2

2 Answers2

0

You appear to be trying to loop through a given directory, performing the spatial join on (shapefiles?) contained therein.

However, this syntax is a problem:

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

In this case, the for loop is iterating over a string. So each time through the loop, it takes one character at a time: first C, then :, then \... and of course the arcpy function fails with this input, because it expects a file path, not a character. Hence the error: Target Features: Dataset C does not exist...


To instead loop through files in your input directory, you need a couple extra steps. Build a list of files, and then iterate through that list.

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

(Ref. this answer on GIS.SE.)

Community
  • 1
  • 1
Erica
  • 2,399
  • 5
  • 26
  • 34
  • Thank you for your response! I realize now this would have been better suited for the GIS Stack Exchange. – beauxgeo Apr 13 '17 at 17:08
  • It's a little hit or miss whether Python-specific questions fit well on GIS.SE or StackOverflow. In this case, your error was a Python issue, but the solution is an ArcPy tool :) – Erica Apr 13 '17 at 17:09
0

After working through some kinks, and thanks to the advice of @erica, I decided to abandon my original concept of a nested for loop, and approach more simply. I'm still working on a GUI that will create system arguments that can be assigned to the variables and then used as parameters for the spatial joins, but for now, this is the solution I've worked out.

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)

Setting multiple environments and using the actual paths feels clunky, but it works for me at this point.

beauxgeo
  • 3
  • 2