AnsweredAssumed Answered

Spatial join for several features with a for loop

Question asked by silvana9675 on Oct 18, 2018
Latest reply on Oct 18, 2018 by silvana9675

Hi,

 

I'm trying to do a spatial join between sevaral feature classes as join features and one feature class as the target feature. I have written a python script for this but I get an error when I execute it.

 

import arcpy
arcpy.CheckExtension('Spatial')
arcpy.CheckOutExtension('Spatial')

arcpy.env.workspace = 'C:\Lab_5\Lab_5\AAB_gdb\AAB.gdb'

#This is to check if these are the feature classes that I want to use

defor_years = arcpy.ListFeatureClasses("def*")

for i in defor_years:
print i

#This is to create the output name for each spatial join

output = 'C:\Lab_5\Lab_5\scratch_gdb\scratch.gdb\spatialjoin_'

for i in range(0,len(defor_years)):
   year = defor_years[i]
   name = output + str(year)
   print name
join_features = name

#Setting the variable for the target features
target_features = 'C:\Lab_5\Lab_5\AAB_gdb\AAB.gdb\commune'

arcpy.SpatialJoin_analysis(target_features, join_features)

When I execute it, I get this.

I guess it's not taking each output of the for loop, making that the final spatial join failed. Can anybody help me with this? Thank youuu!!!!.

Outcomes