Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Using ArcGIS 10 SP4 with all extensions...

I have a parent directory (J:\...\spatialjoin) containing two sub-directories of feature classes I want to spatially join.

Directory 1 (target features) contains 14 point fc's of mosquito trap locations dispersed around a particular county. Each fc represents one week of trap samples. Fields included: trapname, lat, lon, collection date, # mosquitoes, species.

Directory 2 (join features) contains 14 point fc's of rainfall amounts at 219 even-spaced points for a particular county. Each fc represents one week of rainfall. Fields included: point/stationID, lat, lon, weekly sum of rainfall at that point

I want to spatially join one week of rainfall to one week of mosquito locations and repeat until all 14 pairs of files have been joined.

Filename Example:

target directory: traps_2008_week01_7292008.shp, traps_2008_week02_852008.shp, ..., traps_2008_week14_10282008.shp

join directory: week_01.shp, week_02.shp, ..., week_14.shp

The code below lists multiple workspaces and prints names features in each sub-directory. How can I modify it to input target and join features into the tool? Thanks

import arcpy, os
from arcpy import env

# Allow overwrites
arcpy.env.overwriteOutput = True

dir = r'J:\Spring2012\gis_project\spatialjoin'
arcpy.env.workspace = dir

outWorkspace = r"J:\Spring2012\gis_project\spatialjoinoutput"


    targetFeatures = r'J:\Spring2012\gis_project\spatialjoin\collectionweek2008'
    joinFeatures = r'J:\Spring2012\gis_project\spatialjoin\weeklysum2008'
    outfc = os.path.join(outWorkspace)

    def fcs_in_workspace(workspace):
      arcpy.env.workspace = workspace
      for fc in arcpy.ListFeatureClasses():
          yield os.path.join(workspace, fc)
      for ws in arcpy.ListWorkspaces():
          for fc in fcs_in_workspace(os.path.join(workspace, ws)):
              yield fc

    for fc in fcs_in_workspace(dir):
        arcpy.SpatialJoin_analysis(targetFeatures+".shp", joinFeatures+".shp", outfc+".shp", "#", "#", "#", "CLOSEST", "2.392 Kilometers","#")

    # If an error occurred while running a tool, then print the messages.
    print arcpy.GetMessages()
share|improve this question

closed as unclear what you're asking by PolyGeo, BradHards, Jason Scheirer, whuber Oct 9 '13 at 0:24

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

What trouble are you having? Is it an error, are you getting data you don't expect, something else? More details on what is going wrong and what result you desire would help troubleshoot. – Lou Apr 26 '12 at 17:53
I'm getting "Parameters are not valid" output message. ERROR 000732: Target Features: Dataset J:\Spring2012\gis_project\spatialjoin\collectionweek2008.shp does not exist or is not supported ERROR 000732: Join Features: Dataset J:\Spring2012\gis_project\spatialjoin\weeklysum2008.shp does not exist or is not supported – Brach Apr 26 '12 at 18:11
aren't you missing the .shp on the filenames? – Dan Patterson Apr 26 '12 at 18:51
Yes, I see the problem. The targetFeatures and joinFeatures variables are set to directories (containing shapefiles), but these input variables are passing the directory name not the shapefiles they contain. – Brach Apr 26 '12 at 19:01
What do you want this code to do? Can you expand on what you would like to see, for example, what literals should appear in the SpatialJoin_analysis call? Tell us more about the shapefiles, their names and locations and how they relate. It's really hard to help without more information, but for one thing I noticed your iterator over fcs_in_workspace(dir) never actually uses the item on each loop. That is, fc doesn't appear in your call. More information would help. – Lou Apr 27 '12 at 17:17