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_50000\50000.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 Sep 15 '14 at 11:48
  • I mean to show only the layers that are in the visible current map extent – newGIS Sep 16 '14 at 6:41
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)
| improve this answer | |
  • where do i plug the statement in the code? – newGIS Sep 17 '14 at 10:24
  • Before you iterate through the dataframe layer list. – artwork21 Sep 17 '14 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
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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