Clipping Feature Classes by Data View Extents with ArcPy?

المشرف العام

Administrator
طاقم الإدارة
Have a fully functional script about 90% of the time. It is that 10% that is really bothering me and I cannot seem to deduce a solution.

Script will take the user defined dataframe view extents and clip all layers within extent to a geodatabase. That 10% failure rate occurs when clipping an extent that does not contain one of the features (gate_line, fence_line, or wall_line).

My thinking would be to limit input for clipping to only layers that are found in the view extents, this is where I logically fail.

The current script:

import arcpy, osfrom arcpy import env#overwrite permissionsarcpy.env.overwriteOutput = True#set map document to currentmxd = arcpy.mapping.MapDocument("CURRENT")path = mxd.filePathpath = path.replace(os.path.basename(path),"") arcpy.env.workspace = path#creates new file geoGDBarcpy.CreateFileGDB_management(path, "CurrentExtents.gdb")gdb = path + "CurrentExtents.gdb"arcpy.env.workspace = gdb#set dataframe extents as a polygondf = arcpy.mapping.ListDataFrames(mxd)[0]dfAsFeature = arcpy.Polygon(arcpy.Array([df.extent.lowerLeft, df.extent.lowerRight, df.extent.upperRight, df.extent.upperLeft]), df.spatialReference)lyrs = arcpy.mapping.ListLayers(mxd,"",df)exportLayers = []for lyr in lyrs: if lyr.isFeatureLayer == True: layerRplc = lyr.longName replace = [".", "\\"] result = arcpy.GetCount_management(lyr) count = int(result.getOutput(0))#Only include non-blank features if count != 0: for item in replace: layerRplc = layerRplc.replace(item, "_") outFC = os.path.join(gdb,layerRplc) exportLayers.append(outFC) if arcpy.Exists(outFC): arcpy.Delete_management(outFC) arcpy.AddMessage("Getting our clip on...." + outFC) clip = arcpy.Clip_analysis(lyr, dfAsFeature, outFC)#removes empty fc's from gdb clipRes = arcpy.GetCount_management(clip) clipCount = int(clipRes.getOutput(0)) if clipCount == 0: arcpy.Delete_management(clip) del replace del lyrdel mxddel dfScript is run in desktop from catalog with a saved mxd directory as the defined workspace.

Just started coding recently!



أكثر...
 
أعلى