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:
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") 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 >>>