I'm pretty sure this is simple. I'm used to just doing a few maps at a time, and manually joining them doesn't take too much time. However, I've now managed to get myself into the situation where I have 20 infections I'm interested in. And 12 geographic areas. I'd like to create choropleth maps for each infection in each area. I don't think I can click manually through these.
I know that ArcGIS recommends this:
import arcpy target_features = "C:/data/usa.gdb/states" join_features = "C:/data/usa.gdb/cities" out_feature_class = "C:/data/usa.gdb/states_cities" arcpy.SpatialJoin_analysis(target_features, join_features, out_feature_class)
Is the standard "How to do spatial joins" I've got that to work as a one off.
What I'd like to is iterate through the shapefiles in a file location (for each infection), join them to a shapefile (for the geographic area), save it as a new shapefile in a new location with a name like infection_area.shp
import arcpy,os shpworkspace = r"S:\somestuff\maps\CSV Source file" arcpy.env.workspace = shpworkspace arcpy.env.overwriteOutput = True shplist = arcpy.ListFiles("*.shp") join_file = r"S:\somestuff\maps\areaname.shp" pleth = r"S:\somestuff\maps\shpfile_areaname.shp" #join shapefiles for shpfile in shplist arcpy.SpatialJoin_analysis(target_file, join_file, pleth)
I'm having a complete blank (as I'm new to the world of loops and python) on how to say "for each shpfile in shplist, do a spatial join to the join file, write it as pleth".
I've got a feeling I need to use os and split shpfile to create a name and location for the joined shapefile to be saved as.
I tried to build this in a model iterator but that nearly broke me.
I'm almost certain this has been replicated as a question, but how do I write a for loop that will do what I need?
(join a file in location a, to another in location b, write the output as partfile1name_partfile2name.shp to location b, repeat for all shapefiles in location a)
import arcpy,os #sets the workspace shpworkspace = r"S:\somestuff\maps\CSV Source file" arcpy.env.workspace = shpworkspace arcpy.env.overwriteOutput = True featureclasses = arcpy.ListFeatureClasses() #gets the feature class names from the workspace you set earlier #the name of the template map join_file = r"S:\somestuff\maps\areaname.shp" # Join polygons to points # This joins all .shp files in the workspace to the defined point file for fc in featureclasses: name = fc.replace(".shp","_ward.shp") #create a name for the output arcpy.SpatialJoin_analysis(join_file,fc, name) #same join for each
I had to change the order to (join_file, fc,name)