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"
try:
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","#")
except:
# If an error occurred while running a tool, then print the messages.
print arcpy.GetMessages()
