1

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.
>>> 
asked Apr 16, 2015 at 9:49
3
  • 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. Commented 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. Commented Apr 16, 2015 at 11:07
  • Martin,i only want to take care of the layers, raster will stay in the map Commented Dec 23, 2015 at 7:00

1 Answer 1

2

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"):
 continue
 #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:
 continue
 #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:
 continue
 #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".

answered Apr 16, 2015 at 15:03

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.