1

I try to select by location all layers that intersect layer named "frame", and then remove from the maps other layers that don't intersect it. Finally, i want to print the all the removed layers and the remain layers in the table of content.

This is my table of content:

enter image description here

UPDATE CODE: I using this code:

import arcpy,os,sys,string
import arcpy.mapping
from arcpy import env

env.overwriteOutput = True  
env.workspace = r"G:\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):
                if lyr.isGroupLayer == True: continue
                if lyr.isRasterLayer == True: continue
                arcpy.MakeFeatureLayer_management(lyr.dataSource, "lyr")
                frameLyr = r"G:\desktop\Project\layers\frame.shp"
                arcpy.SelectLayerByLocation_management("lyr", "INTERSECT", frameLyr)
                if int(arcpy.GetCount_management("lyr").getOutput(0)) == 0:
                        arcpy.mapping.RemoveLayer(df, lyr)
                        print u'Removed '+unicode(lyr) + ' lyr'
                print lyr.name
mxd.save()        
del mxd

This code works, but i get this result and i wonder if i can shorten this code, so i will get print only one time the removed layer and not twice as below:

>>> 
--- Project.mxd ---
REMOVED LYR
Removed point2 lyr
point2
Removed wells lyr
wells
Removed point lyr
point
Removed river lyr
river
Removed line lyr
line
Removed openSpace lyr
openSpace
frame
land use
>>> 

enter image description here

closed as unclear what you're asking by PolyGeo Feb 24 '18 at 11:15

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • I would not use continue statements in code like this. Have you tried using just nested if statements instead? Also, you should be able to present a much more minimalist code snippet. By focussing on just one MXD that is giving the wrong result you can get rid of the iteration through all the MXDs for a simpler test from which you can better explain what you mean by an "unwanted result". – PolyGeo Mar 9 '16 at 8:25
  • Your code is still iterating through the MXD files returned by arcpy.ListFiles('*.mxd') and does not appear to be the code run to produce that output. Once that is addressed the output would be easier to interpret if you showed us what the Table of Contents of what one MXD contains in the way of layers, and how those layers sit in relation to the frame polygon. – PolyGeo Mar 9 '16 at 10:56
  • ok, then this question will not be on hold? – newGIS Mar 9 '16 at 12:14
  • Whenever your question is edited it goes into the review queue to be assessed for re-opening but whether that happens will depend on the level of improvement. What I described is the improvement that I think this question needs. – PolyGeo Mar 9 '16 at 12:19
  • @PolyGeo What's wrong with "continue"? Aside from being in a compound statement, his use of it seems to be exactly how the Python docs demonstrate it's use. – Tom Mar 9 '16 at 15:07
0

To try and help you debug/improve (I'm still not clear which you are trying to do) your code I suggest using the test code below:

import arcpy,os

mxd = arcpy.mapping.MapDocument(r"G:\desktop\Project\Project.mxd")  
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
for lyr in arcpy.mapping.ListLayers(mxd, "" ,df)
    if lyr.isGroupLayer == True: continue
    if lyr.isRasterLayer == True: continue
    arcpy.MakeFeatureLayer_management(lyr.dataSource, "lyr")
    frameLyr = r"G:\desktop\Project\layers\frame.shp"
    arcpy.SelectLayerByLocation_management("lyr", "INTERSECT", frameLyr)
    if int(arcpy.GetCount_management("lyr").getOutput(0)) == 0:
        arcpy.mapping.RemoveLayer(df, lyr)
        print u'Removed '+unicode(lyr) + ' lyr'
    print lyr.name
mxd.save()        
del mxd

If that is not producing the expected output, or if it throws an error then show us exactly what is output when it is run and describe precisely how what you observe being output departs from what you would expect to see, and why.

The why part is where a zoomed in picture of the Table of Contents from G:\desktop\Project\Project.mxd, and its layers drawn in relation to your frame.shp would be invaluable.

  • PolyGeo, where can i add the result of your code? – newGIS Mar 10 '16 at 6:46
  • @newGIS The type of Q&A you are trying to do is quite unorthodox but I suggest you put it at the end of your question prefaced by "This is the result of running the script in @PolyGeo's answer: ". – PolyGeo Mar 10 '16 at 6:51
  • Actually, I think this test should replace the code in your question so that you can just say "when I run this code ... this is the result", but keep reading and re-reading your question to make sure it flows. – PolyGeo Mar 10 '16 at 6:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.