Add layer and scale using python

3396
2
Jump to solution
11-05-2015 02:27 AM
Yaron_YosefCohen
Occasional Contributor II

Hi everyone,

I have 14 mxd's with scale 50000 and one mxd with scale 25000. with this code i try to add  a new layer, extent to this layer then set a new scale 500000- just to mxd's with scale 50000 . i doesn't want to change the 25000 scale mxd, but i do want just to add him the new layer - and it doesn't work.

i don't know what wrong with this code- i think i didn't place the "if" statement in the right place:

import arcpy,os,sys
from arcpy import env
env.workspace = r"C:\Project"
lyr1 = arcpy.mapping.Layer(r"D:\PROJECTS\industryArad\gis\layers\4_11_15\GVUL TOHNIT.dwg Polyline.lyr")
for mxdname in arcpy.ListFiles("*.mxd"):
    print mxdname
    mxd = arcpy.mapping.MapDocument(r"C:\Project\\" + mxdname)
    df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
    if df.scale == 25000:
        continue
    arcpy.mapping.AddLayer(df, lyr1, "TOP") # TOP \ BOTTOM \ AUTO_ARRANGE
    print 'AddLayer' 
    lyr = arcpy.mapping.ListLayers(mxd, "GVUL TOHNIT.dwg Polyline", df)[0]
    ext = lyr.getExtent()
    df.extent = ext    
    print 'getExtent'
    df.scale = 500000
    print df.scale
    for lyr in arcpy.mapping.ListLayers(mxd, "bbb",df):
        arcpy.mapping.RemoveLayer(df, lyr)
        print 'remove'
    mxd.save()
del mxd
0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

the logic is

if the scale is 25000

      then do something that only applies to layers of that scale

else: (if it is not 25000)

      do something else that only applies to layers not of 25000 scale

carry on doing stuff that applies to both situations.

View solution in original post

0 Kudos
2 Replies
DanPatterson_Retired
MVP Emeritus

the logic is

if the scale is 25000

      then do something that only applies to layers of that scale

else: (if it is not 25000)

      do something else that only applies to layers not of 25000 scale

carry on doing stuff that applies to both situations.

0 Kudos
Yaron_YosefCohen
Occasional Contributor II

great it works !!! thanks Dan

for mxdname in arcpy.ListFiles("*.mxd"):
    print mxdname
    mxd = arcpy.mapping.MapDocument(r"C:\Project\\" + mxdname)
    df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
    
    if df.scale == 25000:
        arcpy.mapping.AddLayer(df, lyr1, "TOP") # TOP \ BOTTOM \ AUTO_ARRANGE
        print 'AddLayer to ' ,mxdname

    else:
        
        arcpy.mapping.AddLayer(df, lyr1, "TOP") # TOP \ BOTTOM \ AUTO_ARRANGE
        print 'AddLayer to ' ,mxdname
        lyr = arcpy.mapping.ListLayers(mxd, "GVUL TOHNIT.dwg Polyline", df)[0]
        ext = lyr.getExtent()
        df.extent = ext    
        print 'getExtent'
        df.scale = 100000
        print df.scale
        
    for lyr in arcpy.mapping.ListLayers(mxd, "bbb",df):
        arcpy.mapping.RemoveLayer(df, lyr)
        print 'remove'
0 Kudos