I am working on ArcMap 10.3 and I have a map with 50 layers in it.

I am trying to check if layers are in a data frame.

Unlike How to check if layers in a dataframe with Arcpy, I want to define the area to the current data frame.

My code is:

import arcpy,os,sys,string
import arcpy.mapping
from arcpy import env

env.workspace = r"C:\Project"
for mxdname in arcpy.ListFiles('*.mxd'):
     print mxdname
     mxd = arcpy.mapping.MapDocument(r"C:\Project\\" + mxdname)
     df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
     data_frame = df
     for lyr in arcpy.mapping.ListLayers(mxd,"",df):
          if lyr in data_frame == False:
               arcpy.mapping.RemoveLayer(df, lyr)
               print lyr     
del  mxd 

and I get:


but no layers have been removed.

  • Your question title seems highly related to the extent of your data frame but the code in its body makes no mention of extents. I think you should edit your title so that it summarizes the code you are seeking help with. – PolyGeo Apr 12 '15 at 11:10
  • I'm not sure, without testing, if testing whether a layer object is in a data frame object is valid syntax, but even if it is, you seem to be saying "if layer is not in data frame then remove it from that data frame" i.e. do nothing. – PolyGeo Apr 12 '15 at 11:17
  • Crossposted - geonet.esri.com/message/495384 – user2856 Apr 12 '15 at 23:51
  • i can't crossposted a question? – newGIS Apr 13 '15 at 7:37
  • @newGIS yes you can crosspost, I was just noting that it was crosposted for other users information. – user2856 Apr 14 '15 at 20:42

The Extent object supports a 'disjoint' (i.e. does not intersect) method.

Try something like:

for mxdname in arcpy.ListFiles('*.mxd'):

     print mxdname
     mxd = arcpy.mapping.MapDocument(os.path.join(env.workspace, mxdname))
     df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
     for lyr in arcpy.mapping.ListLayers(mxd, "" ,df):
          if df.extent.disjoint(lyr.getExtent()):
               arcpy.mapping.RemoveLayer(df, lyr)
               print lyr


del  mxd

One way to do this is to create a polygon feature layer of the dataframe extent, then you may select features in your dataframe to see if they intersect the extent polygon.


mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
layers = arcpy.mapping.ListLayers(mxd, "", df)
# Extent Polygon
extentPolygon = arcpy.Polygon(arcpy.Array([df.extent.lowerLeft,df.extent.lowerRight, df.extent.upperRight, df.extent.upperLeft]),
# Select all features in current extent
for lyr in layers:
   arcpy.SelectLayerByLocation_management(lyr, "INTERSECT", extentPolygon, "", "NEW_SELECTION")
del mxd

Finally, for each layer you may use the Get Count (Data Management) method to see if the count is greater than zero, if so then remove the layer from the map:

arcpy.mapping.RemoveLayer(df, lyr)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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