0

I have different month (JANUARY, FEBRUARY, MARCH.....DECEMBER) folder, each folder hold point shapefile for different month. I want to consider all month folder in a single program and output pass into as on different month folder.

I have written a code but its taking only single month at a time. How to put in my code different month workspace and output save into as same month folder?

I have shown below my code. This code taking too long time for merging the monthly point shapefile. Is there any function available for process the program?

import arcpy, os
out = r'D:\SWAT-WEATHER-DATA2\MARCH' 
arcpy.env.workspace = r'D:\SWAT-WEATHER-DATA2\MARCH' 
shplist = arcpy.ListFeatureClasses('*.shp')
arcpy.Merge_management(shplist, os.path.join(out, 'Merged_003.shp'))
print 'done' 
nmtoken
13.6k5 gold badges39 silver badges91 bronze badges
asked Aug 20, 2016 at 9:26

1 Answer 1

2

You can step through all your folders and files using arcpy.da.walk() and merge the listed files for each of the folders.

This will get all shapefiles in each folder (if that folder is the name of a month) and merge them together into a new file stored in the same folder. It will get the number of the month (i.e. January = 001, March = 003 etc.) and the output file will be based on this number e.g. all shapefiles in the folder OCTOBER will be merged into a new shapefile called Merged_010.shp

import arcpy, os, calendar
topWorkspace = r'D:\SWAT-WEATHER-DATA2' 
# Get dict of months and month-number (i.e. January = 001, March = 003 etc.)
months = {calendar.month_name[i].upper(): str(i).zfill(3) for i in range(1, 13)} 
# Get list of all folders that contain feature classes (shapefiles)
walk = arcpy.da.Walk(topWorkspace, topdown=True, datatype="FeatureClass") 
for folderPath, folderNames, fileNames in walk: 
 baseName = os.path.basename(folderPath).upper() 
 if baseName.upper() in months: # Test that subfolder is a month name
 monthNumber = months[baseName] # Get month-number for use in output filename
 arcpy.env.workspace = folderPath 
 shapeFiles = [x for x in fileNames if os.path.splitext(x)[1].lower() == '.shp'] 
 # Merge all shapefiles in folder into new shapefile with name like Merged_001.shp (for January)
 arcpy.Merge_management(shapeFiles, "Merged_{}.shp".format(monthNumber)) 

I have modified the script a little to get around the arcpy.da.Walk() not working. This instead uses arcpy.ListWorkspaces() to find all the folders, and then arcpy.ListFeatureClasses() to find all the shapefiles in each folder.

import arcpy, os, calendar
topWorkspace = r'D:\SWAT-WEATHER-DATA2' 
arcpy.env.workspace = topWorkspace
# Get dict of months and month-number (i.e. January = 001, March = 003 etc.)
months = {calendar.month_name[i].upper(): str(i).zfill(3) for i in range(1, 13)} # Get dict of months and month number (i.e. January = 001, March = 003 etc.)
# Step through list of all folders
for folderPath in arcpy.ListWorkspaces():
 baseName = os.path.basename(folderPath).upper()
 if baseName in months: # Test that subfolder is a month name
 monthNumber = months[baseName] # Get month-number for use in output filename
 arcpy.env.workspace = folderPath
 shapeFiles = arcpy.ListFeatureClasses('*.shp')
 # Merge all shapefiles in folder into new shapefile with name like Merged_001.shp (for January)
 arcpy.Merge_management(shapeFiles, "Merged_{}.shp".format(monthNumber)) # Merge shapefiles in folder into new shapefile with name Merged_001.shp (for January)
answered Aug 20, 2016 at 20:56
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.