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

Remove layers in sub folders using python- path orientation

$
0
0

Expanding on my previous question Remove layers using python- path orientation,
i try to remove layers from mxd’s (in ArcMap 10.3, Python 2.7.8), that don’t appear in the layout view.

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:desktopProject”.

The data view contain two layers:

enter image description here

The extent of river layer is:

enter image description here

and the extent of land use layer is:

enter image description here

In the layout view i need to zoom to this area only:

enter image description here

and river layer don’t appear in this area, although the river extent contain the land use extent inside of it. I using this code:

import arcpy,os
from arcpy import env

env.workspace = r"D:desktopProject"
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) 
    mxd.save()
del mxd

When i run the code i get project name and all layers name, and no filtering layers is done:

>>> 
project.mxd
91903.0350764695 574029.485062387 200813.043872879 654364.436708682 NaN NaN NaN NaN
river
150949.479567489 608397.791172772 163831.70252934 619047.879128692 NaN NaN NaN NaN
land use
>>> 

My target is to remove river layer from all MXD’s.


Viewing all articles
Browse latest Browse all 767

Trending Articles