4

I working on mxd (I work with ArcMap 10.2.2 & python 2.7.5.) that have 100 layers. I'm trying with python script to add layers to dataframe and check if the layers are within the dataframe. if it within I want that python will add the layers to the table of content, and if it not within the dataframe- the python will remove the layers. Manually I can do it by adding each layer and see if it in the dataframe and then start to choose what to remove, but it will take a lot of time. I also can use this option:

legend properties-->legend

and it also will take a lot of time. I have this script:

import arcpy
from arcpy import env
area= '162000 631000 172000 641000' 
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] 
df.extent = area
for lyr in arcpy.mapping.ListLayers(mxd, "",df):
 if lyr.name == "residence":
 addLayer = arcpy.mapping.Layer(r"C:\project\layers\residence.lyr")
 arcpy.mapping.AddLayerToGroup(df, lyr, addLayer, "BOTTOM")
 else lyr.dataSource == r"F:\GIS\topo_5000050000円.sid": 
 arcpy.mapping.RemoveLayer(df, lyr)
 if lyr.name == "rivers2":
 addLayer = arcpy.mapping.Layer(r"C:\project\layers\rivers2.lyr")
 arcpy.mapping.AddLayerToGroup(df, lyr, addLayer, "BOTTOM")
mxd.save()
del mxd

I hope someone can describe an easy way to accomplish what I'm after with python script?

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jul 16, 2014 at 11:24
2
  • Can you expand more what you mean "check if layers in a dataframe"? Technically, all added layers will be within a dataframe. Are you wanting a layer to be within a certain extent or area of interest? Commented Sep 15, 2014 at 11:48
  • I mean to show only the layers that are in the visible current map extent Commented Sep 16, 2014 at 6:41

2 Answers 2

4

Using the statement below, you could create a temp polygon layer that correspondes to the dataframe extent, iterate and select (select by location) each layer in the map, if selection is> 0 than layer exists within dataframe extent if not then remove.

# create temp polygon layer to dataframe extent
dfAsFeature = arcpy.Polygon(arcpy.Array([df.extent.lowerLeft, df.extent.lowerRight, df.extent.upperRight, df.extent.upperLeft]),df.spatialReference)
answered Sep 16, 2014 at 12:00
2
  • where do i plug the statement in the code? Commented Sep 17, 2014 at 10:24
  • Before you iterate through the dataframe layer list. Commented Sep 17, 2014 at 11:51
1

this is a general code for this question:

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)
 legend = arcpy.mapping.ListLayoutElements(mxd,"LEGEND_ELEMENT")[0]
 for lyr in legend.listLegendItemLayers():
 legend.updateItem(lyr, use_visible_extent = True)
 print 'updateItem' 
 mxd.save()
del mxd
answered Sep 15, 2014 at 10:51

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.