0

I am relatively new to python so apologies if this is a basic mistake. I would like to make a python script to batch run the Spatial Join Tool. The Target shapefiles are in one folder and the join features are in another. Both folders contain 3 shapefiles with identical names. The output location is to a third folder. Script below:

import arcpy
import os


#Set environments

# 1. Target features. Split_Sample Points - SSPFworkspace and List variable
SSPF_1_workspace = r"D:\GIS\Python\Trackplot_py\Test_rev0_1\Split_Sample_Points"
arcpy.env.workspace = SSPF_1_workspace
SSPF_1_List = arcpy.ListFeatureClasses()
print (SSPF_1_List)

# 2. Join Features. Split point folder is SPF_2_workspace
SPF_2_workspace = r"D:\GIS\Python\Trackplot_py\Test_rev0_1\Split_Points"
arcpy.env.workspace = SPF_2_workspace
SPF_2_List = arcpy.ListFeatureClasses()
print (SPF_2_List)

# 3. Spatial Join Output folder.  SJ_Points is SJF_3_workspace
SJF_3_workspace = r"D:\GIS\Python\Trackplot_py\Test_rev0_1\SJ_Points"
arcpy.env.workspace = SJF_3_workspace
SJF_3_List = arcpy.ListFeatureClasses()


for TarF in SSPF_1_List:
    for JoinF in SPF_2_List:
        if TarF == JoinF:
            outputFSJ = os.path.join(SJF_3_workspace, "SJ_" + TarF)

            # Spatial join tool code

            arcpy.SpatialJoin_analysis(target_features= TarF,
                                       join_features= JoinF,
                                       out_feature_class= outputFSJ,
                                       join_operation="JOIN_ONE_TO_ONE",
                                       join_type="KEEP_ALL",
                                       field_mapping='',
                                       match_option="CLOSEST",
                                       search_radius="1",
                                       distance_field_name="SJ_Dist_m")
print (SJF_3_List)
print ("Complete")

The result is:

C:\Python27\ArcGIS10.8\python.exe D:/GIS/python/Trackplots/Spatial_Join.py [u'SSS_I13152_PRC.shp', u'SSS_M13031_01_pt1_PRC.shp', u'SSS_X13426_PRC.shp'] [u'SSS_I13152_PRC.shp', u'SSS_M13031_01_pt1_PRC.shp', u'SSS_X13426_PRC.shp'] Traceback (most recent call last): File "D:/GIS/python/Trackplots/Spatial_Join.py", line 41, in distance_field_name="SJ_Dist_m") File "C:\Program Files (x86)\ArcGIS\Desktop10.8\ArcPy\arcpy\analysis.py", line 533, in SpatialJoin raise e arcgisscripting.ExecuteError: Failed to execute. Parameters are not valid. ERROR 000732: Target Features: Dataset SSS_I13152_PRC.shp does not exist or is not supported ERROR 000732: Join Features: Dataset SSS_I13152_PRC.shp does not exist or is not supported Failed to execute (SpatialJoin).

Process finished with exit code 1

As you can see the print lists appear to work specifying the shapefile list for both target and join features. Yet the error (000732) states that the shapefile does not exist or is supported. Any suggestions?

1

When you do

arcpy.env.workspace = SSPF_1_workspace
SSPF_1_List = arcpy.ListFeatureClasses()
SPF_2_workspace = r"D:\GIS\Python\Track

No items in SSPF_1_List will be found when spatial joining since you changed workspace after it was created and arcpy will only look for them in the last specified workspace.

Try:

import os
arcpy.env.workspace = SSPF_1_workspace
SSPF_1_List = [os.path.join(arcpy.env.workspace, feature) for feature in arcpy.ListFeatureClasses()]

Then the feature classes will be found since the path is complete (path+feature class/filename).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.