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...
Solved! Go to Solution.
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.
I suppose you know that even though no features overlap the data frame, the extent may overlap:
Darren you understand me exactly. How can i solve this issue?
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.
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?
And I am assuming that you ruled out the obvious extent.overlaps? Extent—Help | ArcGIS for Desktop
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:
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