#Import modules and create the geoprocessor objectimport arcgisscripting, os gp=arcgisscripting.create()# Set the workspace gp.workspace = ("C:\SAM_GIS\SAM_GIS\BIODIVERSITY\Mammals")# Start a blank list for Polygon files polyList = [] # Start a blank list for Point files pointList = [] #Start a blank list for the Line files lineList = []# For each subdirectory for dir in os.listdir(gp.workspace): if os.path.isdir(gp.workspace+"/"+dir): # Get a list of the files in each directory files = os.listdir(gp.workspace+"/"+dir) # For each file in a given directory for file in files: # Get only files that end with .shp if (file.endswith(".shp")): print file # Describe feature class desc = gp.Describe(gp.workspace+"/"+dir+"/"+file) type = desc.ShapeType #print dir+"/"+file + " type is: " + type if type == "Polygon": print dir+"/"+file + " is Polygon" polyPath = polyList.append(dir+"/"+file) if type == "Point": print dir+"/"+file + " is Point" pointPath = pointList.append(dir+"/"+file) if type == "Line": print dir+"/"+file + " is Line" linePath = lineList.append(dir+"/"+file) # Hard-code the output merged shapefile names polyshapefile = "Mergedpoly.shp" pointshapefile = "Mergedpoint.shp" lineshapefile = "Mergedline.shp" # Given a list of shapefiles, separate each by a ";" # and put quotes around the whole thing def polyshpList(polyPath): return '"%s"' % ';'.join(polyList) def pointshpList(pointPath): return '"%s"' % ';'.join(pointList) def lineshpList(linePath): return'"%s"' % ";".join(lineList)# Set the variable to the newly formatted list of shapefiles polymergedlist = polyshpList(polyPath) pointmergedlist = pointshpList(pointPath) linemerged list = lineshpList(linePath# Polygons try: print "\nMerging " + polymergedlist + " to get " + polyshapefile + "...\n" gp.merge_management(polymergedlist, polyshapefile) gp.clip_analysis("Mergedpoly.shp", "C:\SAM_GIS\SAM_GIS\BIODIVERSITY\CANADAOUTLINEMAP.shp", gp.workspace, ".000001") print gp.getMessages() except: print gp.getMessages() print "\n *** ERROR: Shapefiles (Polygon) failed to merge *** \n"# Points try: print "\nMerging " + pointmergedlist + " to get " + pointshapefile + "...\n" gp.merge_management(pointmergedlist, pointshapefile) gp.clip_analysis("Mergedpoint.shp", "C:\SAM_GIS\SAM_GIS\BIODIVERSITY\CANADAOUTLINEMAP.shp", gp.workspace, ".000001") print gp.getMessages() except: print gp.getMessages() print "\n *** ERROR: Shapefiles (Point) failed to merge *** \n" # Lines try: print "\nMerging " + linemergedlist + " to get " + lineshapefile + "...\n" gp.merge_management(linemergedlist, lineshapefile) gp.clip_analysis("Mergedline.shp", "C:\SAM_GIS\SAM_GIS\BIODIVERSITY\CANADAOUTLINEMAP.shp", gp.workspace, ".000001") print gp.getMessages() except: print gp.getMessages() print "\n *** ERROR: Shapefiles (Line) failed to merge *** \n" print "\nDone."
import collections import os import arcpy shape_files = collections.defaultdict(list) ##root = arcpy.GetParameterAsText(0) ##dest_path = arcpy.GetParameterAsText(1) root = 'c:/geodata' dest_path = 'c:/tmp' for r, d, f in os.walk(root): shapes = filter(lambda x: x.lower().endswith('.shp'), f) for shape_file in shapes: shape_files[shape_file].append(os.path.join(r, shape_file)) for file_name, file_list in shape_files.iteritems(): ## arcpy.Merge_management(file_list, os.path.join(dest_path, file_name)) print '%s: %r' % (file_name, file_list)
import os, arcpy rootDir = r"D:\csny490\temp" meregString = "" shpDict = {} for dirPath, dirNames, fileNames in os.walk(rootDir, topdown = False): for fileName in fileNames: if fileName.endswith(".shp"): if fileName not in shpDict: shpDict[fileName] = [dirPath] else: shpDict[fileName].append(dirPath) for shp in shpDict: for path in shpDict[shp]: mergeString = mergeString + path + "\\" + shp + ";" arcpy.Merge_management(mergeString[:-1], r"D:\csny490\trythis.gdb\merge_output")
BTW: Does anyone know of a Python IDE (or a method in PythonWin) that lets you execute blocks of code all at once (copy and paste a bunch of code into an "Interactive Window" and then execute it)???? I found you can do this in the Python Window in ArcMap, but as of yet I can't find a way to do it in PythonWin.
That would be nice, wouldn't it. I never heard of anything like that. I spend more time commenting and uncommenting code and then running it. Either that or copying the code block into a new "temp" script and running as a new script. Neither option is great.
thanks Kristya Saldy iam now getting an error on Line 4:'Parsing error <type 'exceptions.SyntaxError'>: invalid syntax (line 4)'i take this to be ' gp.workspace = ("C:\LINZ_Topo_Data\South_island\")' where all my subfolder are stored...where you get the same problem?
gp.workspace = "C:\\LINZ_Topo_Data\\South_island\\"
gp.workspace = r"C:\LINZ_Topo_Data\South_island\"
Eric, I don't see an option to "execute code from the clipboard" when I right click in the interactive window. See attached .jpg...What version of PythonWin are you using? I have v2.5 (build 210). P.S. Yes I realize this topic was hijacked... Sorry.
# This script will merge all point shapefiles in subfolders# of the workspace specified below# Import modules and create the geoprocessor objecttry: # 9.2 and beyond import arcgisscripting, collections, arcpy, sys, os gp = arcgisscripting.create()except: # 9.1 and before import win32com.client, collections, arcpy, sys, os gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")# Set versiongp.SetProduct("ArcEditor")# Set the workspacegp.workspace = ("W:\GPS\Trimble_Jobs\Trimble_Geomatics_Office\Projects")# Start a blank list for Polygon files# polyList = []# Start a blank list for Point filespointList = []# For each subdirectoryfor dir in os.listdir(gp.workspace): if os.path.isdir(gp.workspace+"/"+dir): # Get a list of the files in each directory files = os.listdir(gp.workspace+"/"+dir) # For each file in a given directory for file in files: # Get only files that end with .shp if (file.endswith(".shp")): print file # Describe feature class desc = gp.Describe(gp.workspace+"/"+dir+"/"+file) type = desc.ShapeType #print dir+"/"+file + " type is: " + type #if type == "Polygon": # print dir+"/"+file + " is Polygon" # polyPath = polyList.append(dir+"/"+file) if type == "Point": print dir+"/"+file + " is Point" pointPath = pointList.append(dir+"/"+file) # Hard-code the output merged shapefile names # polyshapefile = "Mergedpoly.shp" pointshapefile = "Mergedpoint.shp" # Given a list of shapefiles, separate each by a ";" # and put quotes around the whole thing # def polyshpList(polyPath): # return '"%s"' % ';'.join(polyList) def pointshpList(pointPath): return '"%s"' % ';'.join(pointList)# Set the variable to the newly formatted list of shapefiles # polymergedlist = polyshpList(polyPath)pointmergedlist = pointshpList(pointPath)# Polygons# try: # print "\nMerging " + polymergedlist + " to get " + polyshapefile + "...\n" # gp.merge_management(polymergedlist, polyshapefile) #print gp.getMessages() #except: # print gp.getMessages() # print "\n *** ERROR: Shapefiles (Polygon) failed to merge *** \n"# Pointstry: print "\nMerging " + pointmergedlist + " to get " + pointshapefile + "...\n" gp.merge_management(pointmergedlist, pointshapefile) print gp.getMessages() except: print gp.getMessages() print "\n *** ERROR: Shapefiles (Point) failed to merge *** \n" print "\nDone."
if type == "Point": print dir+"/"+file + " is Point" pointPath = pointList.append(dir+"/"+file)
# This script will merge all point shapefiles in subfolders # of the workspace specified below # Import modules and create the geoprocessor object try: # 9.2 and beyond import arcgisscripting, collections, arcpy, sys, os gp = arcgisscripting.create() except: # 9.1 and before import win32com.client, collections, arcpy, sys, os gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1") # Set version gp.SetProduct("ArcEditor") # Set the workspace gp.workspace = ("C:/Trimble Geomatics Office/Projects") # Start a blank list for Polygon files # polyList = [] # Start a blank list for Point files pointList = [] # For each subdirectory for dir in os.listdir(gp.workspace): if os.path.isdir(gp.workspace+"/"+dir+"/Export/"): # Get a list of the files in each directory files = os.listdir(gp.workspace+"/"+dir+"/Export/") # For each file in a given directory for file in files: # Get only files that end with .shp if (file.endswith(".shp")): print file # Describe feature class desc = gp.Describe(gp.workspace+"/"+dir+"/"+"/Export/"+file) type = desc.ShapeType #print dir+"/"+file + " type is: " + type #if type == "Polygon": # print dir+"/"+file + " is Polygon" # polyPath = polyList.append(dir+"/"+file) if type == "Point": print dir+"/"+file + " is Point" pointPath = pointList.append(dir+"/"+"/Export/"+file) # Hard-code the output merged shapefile names # polyshapefile = "Mergedpoly.shp" pointshapefile = "Mergedpoint2.shp" # Given a list of shapefiles, separate each by a ";" # and put quotes around the whole thing # def polyshpList(polyPath): # return '"%s"' % ';'.join(polyList) def pointshpList(pointPath): return '"%s"' % ';'.join(pointList) # Set the variable to the newly formatted list of shapefiles # polymergedlist = polyshpList(polyPath) pointmergedlist = pointshpList(pointPath) # Polygons # try: # print "\nMerging " + polymergedlist + " to get " + polyshapefile + "...\n" # gp.merge_management(polymergedlist, polyshapefile) #print gp.getMessages() #except: # print gp.getMessages() # print "\n *** ERROR: Shapefiles (Polygon) failed to merge *** \n" # Points try: print "\nMerging " + pointmergedlist + " to get " + pointshapefile + "...\n" gp.merge_management(pointmergedlist, pointshapefile) print gp.getMessages() except: print gp.getMessages() print "\n *** ERROR: Shapefiles (Point) failed to merge *** \n" print "\nDone."
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.