POST
|
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 more
10-18-2018
07:12 PM
|
3
|
0
|
1248
|
POST
|
Thank you for your suggestions. I already change what you said but it didn't work.
... View more
10-18-2018
04:31 PM
|
0
|
1
|
1248
|
POST
|
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!!!!.
... View more
10-18-2018
01:42 PM
|
0
|
4
|
1787
|
Title | Kudos | Posted |
---|---|---|
3 | 10-18-2018 07:12 PM |
Online Status |
Offline
|
Date Last Visited |
11-19-2023
10:33 PM
|