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
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!!!!.
Solved! Go to Solution.
Here's the answer for anyone if they ever have to do something similar:
#Import arcpy module
import arcpy
#Checking if Spatial Analysis Extension is available
arcpy.CheckExtension('Spatial')
#Taking Spatial Analysis to work with it
arcpy.CheckOutExtension('Spatial')
#Setting the environment workspace
arcpy.env.workspace = 'C:\Lab_5\Lab_5\AAB_gdb\AAB.gdb'
#List of the files we are going to work with
defor_years = arcpy.ListFeatureClasses("def*")
for i in defor_years:
print i
# Setting the output for the new files
output = 'C:\Lab_5\Lab_5\scratch_gdb\scratch.gdb\spatjoin'
# Setting target features
target_features = 'C:\Lab_5\Lab_5\AAB_gdb\AAB.gdb\commune'
# Creating a for loop to iterate over files
for i in range(0,len(defor_years)):
year = defor_years[i]
# Since there are going to be several files as output,it's necessary to create a different name for each
name = output + str(year)
# Prints te name of the new files or outputs
print "name =" + name
# Executing the spatial join using as join features the files that were been iterated
# and as output_feature class the name established above for each new file
#The option KEEP COMMON maintains only the records that were common between the target
# and join features
arcpy.SpatialJoin_analysis(target_features, year ,name,'','KEEP_COMMON')
You need to use the syntax highlighter to post code; no one can tell where your for loops end since the indentations are omitted.
Look at what the error is telling you. It may stem from you not properly seting your workspace. I suggest using:
arcpy.env.workspace = r'C:\path\to\your.gdb'
Otherwis you need to escape each of the back slashes with a back slash:
arcpy.env.workspace = 'C:\\path\\to\\your.gdb'
Thank you for your suggestions. I already change what you said but it didn't work.
/blogs/dan_patterson/2016/08/14/script-formatting
Spatial Join—Help | ArcGIS Desktop
spatial join requires two 'layers', so if you aren't running the code in the interactive window or have *.lyr (*.lyrx) files,
it won't work.
you will have to make feature layers before you do the spatial join.
Here's the answer for anyone if they ever have to do something similar:
#Import arcpy module
import arcpy
#Checking if Spatial Analysis Extension is available
arcpy.CheckExtension('Spatial')
#Taking Spatial Analysis to work with it
arcpy.CheckOutExtension('Spatial')
#Setting the environment workspace
arcpy.env.workspace = 'C:\Lab_5\Lab_5\AAB_gdb\AAB.gdb'
#List of the files we are going to work with
defor_years = arcpy.ListFeatureClasses("def*")
for i in defor_years:
print i
# Setting the output for the new files
output = 'C:\Lab_5\Lab_5\scratch_gdb\scratch.gdb\spatjoin'
# Setting target features
target_features = 'C:\Lab_5\Lab_5\AAB_gdb\AAB.gdb\commune'
# Creating a for loop to iterate over files
for i in range(0,len(defor_years)):
year = defor_years[i]
# Since there are going to be several files as output,it's necessary to create a different name for each
name = output + str(year)
# Prints te name of the new files or outputs
print "name =" + name
# Executing the spatial join using as join features the files that were been iterated
# and as output_feature class the name established above for each new file
#The option KEEP COMMON maintains only the records that were common between the target
# and join features
arcpy.SpatialJoin_analysis(target_features, year ,name,'','KEEP_COMMON')