Select to view content in your preferred language

Spatial join for several features with a for loop

2359
4
Jump to solution
10-18-2018 01:42 PM
SilvanaCastillo
Emerging Contributor

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!!!!.

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
SilvanaCastillo
Emerging Contributor

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')‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

4 Replies
JoeBorgione
MVP Emeritus

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'
That should just about do it....
SilvanaCastillo
Emerging Contributor

Thank you for your suggestions. I already change what you said but it didn't work.

0 Kudos
DanPatterson_Retired
MVP Emeritus

/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.

Make Feature Layer—Data Management toolbox | ArcGIS Desktop 

SilvanaCastillo
Emerging Contributor

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')‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍