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,string
import arcpy.mapping
from arcpy import env
env.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 mxd
i get an error when the layers are in Group layer:
>>>
Project.mxd
----Removed lyr----
Traceback (most recent call last):
File "C:\yaron\shonot\software\gis\tools\YARON_SCRIPTS\SelectLayerByLocation2.py", line 22, in <module>
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.
>>>