Hi everyone,
i have mxd with 85 layers and i try to remove layers (with arcpy) that don't exist in the current data frame. i using this code:
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] for lyr in arcpy.mapping.ListLayers(mxd,"",df): if lyr in df.extent == False: arcpy.mapping.RemoveLayer(df, lyr) print 'RemoveLayer' mxd.save() del mxd
but i get en error:
>>> Project.mxd Traceback (most recent call last): File "C:/yaron/shonot/software/gis/tools/YARON_SCRIPTS/aaaaaaaaaaaaaa.py", line 15, in <module> if lyr in df.extent == False: TypeError: argument of type 'Extent' is not iterable >>>
i don't know why my code doesn't work.
Thanks for any help.
Solved! Go to Solution.
The 'overlaps' method must return False if the dataframe extent is completely within/completely contains the layer extent. Try the 'disjoint' method instead. See the documentation - http://resources.arcgis.com/en/help/main/10.2/index.html#//018z00000072000000
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 mxd.save() del mxd
extent is definitely not iterable... you should read up on the class objects before attempting them, there are functions in there that you can use to decide whether two geometries intersect and/or one contains the other etc. In your case, I suspect that you need the extent of the lyr, then you need to decide which of the options that you wish to use and of course, whether the files is in the same projection as the other extent object. Have a think, then propose your options before assessing code options.
Extent objects support a 'overlaps' method
Try something like:
if not df.extent.overlaps(lyr.getExtent()) :
etc...
Sorry about the lack of formatting and links, the GeoNet mobile site is terrible.
i try this:
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]
for lyr in arcpy.mapping.ListLayers(mxd, "" ,df):
if not df.extent.overlaps(lyr.getExtent()):
arcpy.mapping.RemoveLayer(df, lyr)
print lyr
mxd.save()
del mxd
but actually arcpy remove also layers that have been halfly in the data frame
look at some of the other options in the link I sent you...surely you can figure out one that works or send the files to Luke for testing
i ask a question because i new in all that python stuff, if you noticed that. If you think that my questions are not clever enough please don't answered them. your last answer are very sarcastic and I was very offended from you.
Then next time it would be best to try and articulate what you have tried. All the information for testing is contained in the first link...you chose to grab a code snippet and hope it worked...that is not how you garner help, you have to make some effort. I apologize if I offended you, but your questions and responses often don't show any attempt at research but seem to be geared to getting an immediate solution.
So did you try
in place of Luke's suggestion...you are the only one that can answer that
Checking intersects or overlaps of extents does no guarantee that a layer has features within the current data frame extent.
I am wondering why you want to remove these layers? It because they are showing up in the legend or so?
no, i want to remove them automatically because they located out of the data frame. i mean the layers has coordinate system like the data frame but in specific area in there several layers and not all the the layers. so instead remove the unseen layers i try to build code that will do it.
i do read it ,but didn't understand - that the reason i join this forum.