Search code examples
pythonarcpy

Error recognizing parameters for a spatial join using ArcPy


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


Solution

  • 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.)