# 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."
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 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."
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.
gp.workspace = "C:\\LINZ_Topo_Data\\South_island\\"
gp.workspace = r"C:\LINZ_Topo_Data\South_island\"
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?
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.
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")
サインインしたメンバーは投稿、更新のフォローなどができます。初めてですか?無料アカウントを登録してください。
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.