Quantcast
Channel: Question and Answer » arcpy
Viewing all articles
Browse latest Browse all 767

How to remove layers that are not in visible df extent

$
0
0

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     
     mxd.save()
del  mxd 

and I get:

 >>> 
Project.mxd
>>> 

but no layers have been removed.


Viewing all articles
Browse latest Browse all 767

Trending Articles