1

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)

Solution:

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)

  • You should post your solution as an answer to the question, not as part of the question – nmtoken Apr 24 '18 at 6:09
  • t's probably bad form to ask questions when this is solved, but here goes.... Is it possible to do this without the for loop? In R, I can split a csv (with split) to a list called x, and then use lapply to write all the names in x to individual files. Can I do something similar in python? – damo Apr 24 '18 at 15:09
1

To get the list of shapefiles:

featureclasses = arcpy.ListFeatureClasses() #gets the feature class names from the workspace you set earlier

To iterate the feature classes:

for fc in featureclasses:
    name = fc.replace(".shp","namejoin.shp") #create a name for the output
    arcpy.SpatialJoin_analysis(fc, join_file, name) #same join for each

There are other ways you might change the name. I assumed that you wanted to output to the workspace.

  • Thank you. I've just checked it and it worked.Initially this bit arcpy.SpatialJoin_analysis(fc, join_file, name) #same join for each gave points on the map. I switched it to arcpy.SpatialJoin_analysis( join_file, fc,name) #same join for each and it worked. – damo Apr 23 '18 at 14:11
  • Sorry: quick question. This works once. When I change the shpworkspace to a new location, and the join_file to a new file, i'm then faced with TypeError: 'NoneType' object is not iterable. Does something need to be deleted/reset/break point inserted? – damo Apr 23 '18 at 15:04
0

The answer!

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)

  • It's probably bad form to ask questions when this is solved, but here goes.... Is it possible to do this without the for loop? In R, I can split a csv (with split) to a list called x, and then use lapply to write all the names in x to individual files. Can I do something similar in python? – damo Apr 24 '18 at 11:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.