Remove layers-arcpy

5337
7
Jump to solution
12-27-2015 04:23 AM
Yaron_YosefCohen
Occasional Contributor II

Expanding on my previous question Remove Layers ,

i try to remove layers from mxd's (in ArcMap 10.3, Python 2.7.8), that don't appear in the data frame.

The layers located in different disk drivers and in different folders and sub folders and each layer has different path orientation (right to left- different language from English). Only the MXD's located in "D:\desktop\Project" I using this code:

# -*- coding: cp1255 -*-
import arcpy,os
from arcpy import env

env.workspace = r"D:\desktop\Project"
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):
        print str(lyr.getExtent())
        print lyr.name
        if lyr.getExtent() != None:
            if df.extent.disjoint(lyr.getExtent()):
                arcpy.mapping.RemoveLayer(df, lyr)
                print u'Removed '+unicode(lyr) + ' lyr' 


    mxd.save()
del mxd

For clarity, i asked it in arcpy - Remove layers in sub folders using python- path orientation - Geographic Information Systems...

0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

Using the example provided by Darren, you could use a select by location (Intersect) using the extent from the dataframe. If the resulting layer does not contain any features, you can remove the layer.

View solution in original post

7 Replies
DarrenWiens2
MVP Honored Contributor

I suppose you know that even though no features overlap the data frame, the extent may overlap:

Yaron_YosefCohen
Occasional Contributor II

Darren you understand me exactly. How can i solve this issue?

0 Kudos
XanderBakker
Esri Esteemed Contributor

Using the example provided by Darren, you could use a select by location (Intersect) using the extent from the dataframe. If the resulting layer does not contain any features, you can remove the layer.

Yaron_YosefCohen
Occasional Contributor II

I know that fact, but my question is how can i remove layers that don't appear in the data frame, even though their layer extent cover up the data frame? is it possible?

0 Kudos
DanPatterson_Retired
MVP Emeritus

And I am assuming that you ruled out the obvious extent.overlaps?  Extent—Help | ArcGIS for Desktop

Yaron_YosefCohen
Occasional Contributor II

When i use  extent.overlaps it remove land use layer- that ,in fact do exist in the layout, and finally i remain with river layer that don't exist in layout -but it extent coordination do cover the layout view:

1.jpg

0 Kudos
DanPatterson_Retired
MVP Emeritus

You now have a disconnect between your dataframe and its layout view, go back and zoom to the extent of the data (one of the layers) or to the full extent.  Does your image now appear back on the layout view?  If not, then you have your layout fixed and it doesn't change asyour data frame changes

0 Kudos