1

Recently I saw a script that moves layers according to their names in TOC. I wanted to make a complete script for my work in which all needed layers are listed and probably should be sorted in a way I want.

Here it is:

import arcpy
mxd=arcpy.mapping.MapDocument("CURRENT")
df= arcpy.mapping.ListDataFrames(mxd,"")[0]
from arcpy import mapping
for lyr in mapping.ListLayers(mxd,"",df):
    if lyr.name == "ramka":
        moveLayer = lyr
    if lyr.name == "address":
        refLayer = lyr
mapping.MoveLayer(df,refLayer,moveLayer,"BEFORE")

for lyr in mapping.ListLayers(mxd,"",df):
    if lyr.name == "address":
        moveLayer1 = lyr
    if lyr.name == "river":
        refLayer1 = lyr
mapping.MoveLayer(df,refLayer1,moveLayer1,"BEFORE")

for lyr in mapping.ListLayers(mxd,"",df):
    if lyr.name == "river":
        moveLayer2 = lyr
    if lyr.name == "allhouse":
        refLayer2 = lyr
mapping.MoveLayer(df,refLayer2,moveLayer2,"BEFORE")

for lyr in mapping.ListLayers(mxd,"",df):
    if lyr.name == "allhouse":
        moveLayer3 = lyr
    if lyr.name == "topo":
        refLayer3 = lyr
mapping.MoveLayer(df,refLayer3,moveLayer3,"BEFORE")


for lyr in mapping.ListLayers(mxd,"",df):
    if lyr.name == "topo":
        moveLayer4 = lyr
    if lyr.name == "village":
        refLayer4 = lyr
mapping.MoveLayer(df,refLayer4,moveLayer4,"BEFORE")


for lyr in mapping.ListLayers(mxd,"",df):
    if lyr.name == "village":
        moveLayer5 = lyr
    if lyr.name == "quarter":
        refLayer5 = lyr
mapping.MoveLayer(df,refLayer5,moveLayer5,"BEFORE")

for lyr in mapping.ListLayers(mxd,"",df):
    if lyr.name == "quarter":
        moveLayer6 = lyr
    if lyr.name == "town":
        refLayer6 = lyr
mapping.MoveLayer(df,refLayer6,moveLayer6,"BEFORE")

for lyr in mapping.ListLayers(mxd,"",df):
    if lyr.name == "town":
        moveLayer7 = lyr
    if lyr.name == "district_t":
        refLayer7 = lyr
mapping.MoveLayer(df,refLayer7,moveLayer7,"BEFORE")

for lyr in mapping.ListLayers(mxd,"",df):
    if lyr.name == "district_t":
        moveLayer8 = lyr
    if lyr.name == "district_a1":
        refLayer8 = lyr
mapping.MoveLayer(df,refLayer8,moveLayer8,"BEFORE")

for lyr in mapping.ListLayers(mxd,"",df):
    if lyr.name == "district_a1":
        moveLayer9 = lyr
    if lyr.name == "district_a":
        refLayer9 = lyr
mapping.MoveLayer(df,refLayer9,moveLayer9,"BEFORE")

for lyr in mapping.ListLayers(mxd,"",df):
    if lyr.name == "district_a":
        moveLayer10 = lyr
    if lyr.name == "admin2":
        refLayer10 = lyr
mapping.MoveLayer(df,refLayer10,moveLayer10,"BEFORE")

for lyr in mapping.ListLayers(mxd,"",df):
    if lyr.name == "admin2":
        moveLayer11 = lyr
    if lyr.name == "admin1":
        refLayer11 = lyr
mapping.MoveLayer(df,refLayer11,moveLayer11,"BEFORE")

for lyr in mapping.ListLayers(mxd,"",df):
    if lyr.name == "admin1":
        moveLayer12 = lyr
    if lyr.name == "admin":
        refLayer12 = lyr
mapping.MoveLayer(df,refLayer12,moveLayer12,"BEFORE")


arcpy.RefreshTOC()
arcpy.RefreshActiveView()

Unfortunately, testing of this script in other databases throws an error like

Runtime error 
Traceback (most recent call last):
  File "<string>", line 39, in <module>
NameError: name 'refLayer4' is not defined

It was because of one of the layers did not exist in TOC. So what would you recommend if it is needed to have an order in TOC like in script despite the absence of some layers. Or is there any other script sample that sorts layers?

1 Answer 1

2

Create empty parameters for each for loop, and then check it's populated before your mapping.MoveLayer() to avoid any errors due to missing layers.

moveLayer4 = None
refLayer4 = None
for lyr in mapping.ListLayers(mxd,"",df):
    if lyr.name == "topo":
        moveLayer4 = lyr
    if lyr.name == "village":
        refLayer4 = lyr
if refLayer4 and moveLayer4:
    mapping.MoveLayer(df,refLayer4,moveLayer4,"BEFORE")

If your refLayer4 or moveLayer4 (etc.) haven't been populated, then the mapping.MoverLayer() will be skipped for that step.

2
  • @Midvalo very well, that works! Commented Oct 19, 2016 at 9:18
  • But in one of my tests there was a kind of a bug. I haven't some layers in TOC and it was sorted like "admin2 - admin 1 - some other layers - address - river - allhouse". It actually makes right things but in general it is not an order I want to see. Commented Oct 19, 2016 at 9:22

Your Answer

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

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