i work with arcview 10.3 on 20 mxd's (each mxd has 25 layers) and i try to do select by location (with arcpy) and want to see a layers that are visible in the current map extent only. All Other layers that do not get in the current map extent i want to remove them from the data frame. I try this code:
import arcpy,os,sys,stringimport arcpy.mappingfrom arcpy import envenv.overwriteOutput = True env.workspace = r"C:\Project"for mxdname in arcpy.ListFiles('*.mxd'): print mxdname print '----Removed lyr----' mxd = arcpy.mapping.MapDocument(r"C:\Project\\" + mxdname) df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] for lyr in arcpy.mapping.ListLayers(mxd, "" ,df): arcpy.MakeFeatureLayer_management(lyr.dataSource, "lyr") dfAsFeature = arcpy.Polygon(arcpy.Array([df.extent.lowerLeft, df.extent.lowerRight, df.extent.upperRight, df.extent.upperLeft]), df.spatialReference) arcpy.SelectLayerByLocation_management("lyr", "INTERSECT", dfAsFeature) if int(arcpy.GetCount_management("lyr").getOutput(0)) == 0: arcpy.mapping.RemoveLayer(df, lyr) print lyr mxd.save()del mxdi get 2 errors:1 when the layers are in Group layer i get an error:
>>> Project.mxd----Removed lyr----Traceback (most recent call last): File "C:\yaron\shonot\software\gis\tools\YARON_SCRIPTS\SelectLayerByLocation2.py", line 22, in arcpy.MakeFeatureLayer_management(lyr.dataSource, "lyr") File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\arcobjects\_base.py", line 78, in _get (attr_name, self.__class__.__name__))NameError: The attribute 'dataSource' is not supported on this instance of Layer.>>> 2 the second error- when i have a raster in the mxd i get this error:
>>> Project.mxd----Removed lyr----total VCR300Traceback (most recent call last): File "C:\yaron\shonot\software\gis\tools\YARON_SCRIPTS\SelectLayerByLocation2.py", line 22, in arcpy.MakeFeatureLayer_management(lyr.dataSource, "lyr") File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\management.py", line 6509, in MakeFeatureLayer raise eExecuteError: Failed to execute. Parameters are not valid.ERROR 000732: Input Features: Dataset C:\Project\layers\logo1.jpg does not exist or is not supportedWARNING 000725: Output Layer: Dataset lyr already exists.Failed to execute (MakeFeatureLayer).>>>
أكثر...
import arcpy,os,sys,stringimport arcpy.mappingfrom arcpy import envenv.overwriteOutput = True env.workspace = r"C:\Project"for mxdname in arcpy.ListFiles('*.mxd'): print mxdname print '----Removed lyr----' mxd = arcpy.mapping.MapDocument(r"C:\Project\\" + mxdname) df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] for lyr in arcpy.mapping.ListLayers(mxd, "" ,df): arcpy.MakeFeatureLayer_management(lyr.dataSource, "lyr") dfAsFeature = arcpy.Polygon(arcpy.Array([df.extent.lowerLeft, df.extent.lowerRight, df.extent.upperRight, df.extent.upperLeft]), df.spatialReference) arcpy.SelectLayerByLocation_management("lyr", "INTERSECT", dfAsFeature) if int(arcpy.GetCount_management("lyr").getOutput(0)) == 0: arcpy.mapping.RemoveLayer(df, lyr) print lyr mxd.save()del mxdi get 2 errors:1 when the layers are in Group layer i get an error:
>>> Project.mxd----Removed lyr----Traceback (most recent call last): File "C:\yaron\shonot\software\gis\tools\YARON_SCRIPTS\SelectLayerByLocation2.py", line 22, in arcpy.MakeFeatureLayer_management(lyr.dataSource, "lyr") File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\arcobjects\_base.py", line 78, in _get (attr_name, self.__class__.__name__))NameError: The attribute 'dataSource' is not supported on this instance of Layer.>>> 2 the second error- when i have a raster in the mxd i get this error:
>>> Project.mxd----Removed lyr----total VCR300Traceback (most recent call last): File "C:\yaron\shonot\software\gis\tools\YARON_SCRIPTS\SelectLayerByLocation2.py", line 22, in arcpy.MakeFeatureLayer_management(lyr.dataSource, "lyr") File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\management.py", line 6509, in MakeFeatureLayer raise eExecuteError: Failed to execute. Parameters are not valid.ERROR 000732: Input Features: Dataset C:\Project\layers\logo1.jpg does not exist or is not supportedWARNING 000725: Output Layer: Dataset lyr already exists.Failed to execute (MakeFeatureLayer).>>>
أكثر...