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?
-
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?artwork21– artwork212014年09月15日 11:48:58 +00:00Commented Sep 15, 2014 at 11:48
-
I mean to show only the layers that are in the visible current map extentnewGIS– newGIS2014年09月16日 06:41:18 +00:00Commented Sep 16, 2014 at 6:41
2 Answers 2
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)
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
Explore related questions
See similar questions with these tags.