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]),
         arcpy.SelectLayerByLocation_management("lyr", "INTERSECT", dfAsFeature)

         if int(arcpy.GetCount_management("lyr").getOutput(0)) == 0:
              arcpy.mapping.RemoveLayer(df, lyr)
              print lyr

del mxd

i get an error when the layers are in Group layer:

----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.
  • What do you want it to do when you have a raster? Clip it with your polygon used in Select by location? Maybe you can just add a type check and filter out rasters before entering the Select by location process.
    – Martin
    Apr 16, 2015 at 10:00
  • 1
    Would you be able to edit your question to clarify what you are asking, please? Now, before any answers are offered is the ideal time to do so. Please try to focus your question down to just the more important of your 2(?) problems.
    – PolyGeo
    Apr 16, 2015 at 11:07
  • Martin,i only want to take care of the layers, raster will stay in the map
    – newGIS
    Dec 23, 2015 at 7:00

1 Answer 1


Ok, the first error is easy to avoid. It's failing because you are trying to find the datasource of a group layer, and group layers don't have data sources. Filter the group layers out by doing this:

for lyr in arcpy.mapping.ListLayers(mxd, "" ,df):
     if not lyr.supports("DATASOURCE"):
     #further code here for non-group layers

The supports() method is the best way to check for whether a given layer object has a certain property. Here's more info (scroll down to find the supports method).

Regarding the other issue, I assume that you are trying to avoid rasters (because you can't do a select by location on a raster layer). If this is the case, you can do a similar filter as above, using the isRasterLayer property:

for lyr in arcpy.mapping.ListLayers(mxd, "" ,df):
     if lyr.isRasterLayer:
     #further code here for non-raster layers

Ultimately, you should use the following to accomplish both of the above tasks:

for lyr in arcpy.mapping.ListLayers(mxd, "" ,df):
     if not lyr.isFeatureLayer:
     #further code here for only feature layers (no rasters, basemaps or group layers)

You also seem to have run into a problem with the MakeFeatureLayer operation. I believe that it's unnecessary anyway (because you're working with layer objects already). Try removing that line altogether and just referencing lyr instead of "lyr".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.