Select to view content in your preferred language

Spatial join between one target feature and several join features ?

934
4
Jump to solution
05-31-2012 02:00 AM
HeidiSevestre
Emerging Contributor
Dear all with Python experience,

In advance, please excuse the little experience I have dealing with Python, as I am trying to write my very first script!

My situation is that I would like to perform spatial join between one single "target feature" and several "join features". The aim is to create as many output files as I have join features.
In addition (but I have absolutely no idea how) I would like each output file to keep the file name from the join feature file.

So far, my script looks like this:


import arcpy
import os

# Create the geoprocessor object
try:
     # 9.2 and beyond   
     import arcgisscripting
     gp = arcgisscripting.create()
     print "\nImporting geoprocessor for 9.2 and beyond..."   
except:   
     # 9.1 and before   
     import win32com.client
     gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")
     print "\nImporting geoprocessor for 9.1 and before..."

targetFeatures = "G:/PhD/ERA 40/WorkPython/target/"
joinFeatures = "G:/PhD/ERA 40/WorkPython/join/"
outfc = "G:/PhD/ERA 40/WorkPython/Output"

# Read all the shapefiles in folder 'joinFeatures'
gp.SpatialJoin(targetFeatures, joinFeatures, outfc, "JOIN_ONE_TO_ONE", "KEEP_ALL")

print "\nDone."

Without surprises this script doesn't perform well, and I am not able to get anything out of it.

My target feature file is "Alaska_20111229_geog_glaciers.shp"
My join feature files are called "09011981small.shp", the first 8 digits changing for every single file.

Any help/improvement/advice would be very much appreciated. Thank you!
Kind regards,

Heidi
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MarcinGasior
Frequent Contributor
This code may be a good start for you (I assume you're using ArcGIS 10):
try:     import arcpy, os     arcpy.env.workspace = "G:/PhD/ERA 40/WorkPython/join"     targetFeatures = "G:/PhD/ERA 40/WorkPython/target/Alaska_20111229_geog_glaciers.shp"     outDirectory = "G:/PhD/ERA 40/WorkPython/Output"      #take all shapefiles from .../join folder which name contains "small" at the end     fcList = arcpy.ListFeatureClasses("*small")      #for each "small" feature class perform spatial join     for featureClass in fcList:         outFeatureClass = os.path.join(outDirectory, featureClass)         arcpy.SpatialJoin_analysis(targetFeatures, featureClass, outFeatureClass)  except:     print arcpy.GetMessages()


To preserve indentation select your code and click Wrap
 tags around selected text

View solution in original post

0 Kudos
4 Replies
HeidiSevestre
Emerging Contributor
(and sorry about the indentations that have mysteriously disappeared from my script)
0 Kudos
MarcinGasior
Frequent Contributor
This code may be a good start for you (I assume you're using ArcGIS 10):
try:     import arcpy, os     arcpy.env.workspace = "G:/PhD/ERA 40/WorkPython/join"     targetFeatures = "G:/PhD/ERA 40/WorkPython/target/Alaska_20111229_geog_glaciers.shp"     outDirectory = "G:/PhD/ERA 40/WorkPython/Output"      #take all shapefiles from .../join folder which name contains "small" at the end     fcList = arcpy.ListFeatureClasses("*small")      #for each "small" feature class perform spatial join     for featureClass in fcList:         outFeatureClass = os.path.join(outDirectory, featureClass)         arcpy.SpatialJoin_analysis(targetFeatures, featureClass, outFeatureClass)  except:     print arcpy.GetMessages()


To preserve indentation select your code and click Wrap
 tags around selected text
0 Kudos
HeidiSevestre
Emerging Contributor
Thank you for this quick and instructive reply.
I have just tried your script, and I get:

Parsing error <type 'exceptions.SyntaxError'>: invalid syntax (line 12)
which is the "except:"

Should there be an indentation before except?
Thanks again for your help.

Heidi
0 Kudos
HeidiSevestre
Emerging Contributor
Thank you very very much! The script works perfectly fine, and is very simple and neat.

Greetings from Norway

Heidi 🙂
0 Kudos