1

I need to update the table of contents for a given .mxd by keeping a certain order but switching out the contents. I can bring in the new feature classes as map layers, and delete the old map layers but I can't seem to keep the order the same. Changing data source is not an option, I need to add new layers. As you can see the new map layer order is not the same as the old order. Hope you guys can help! Here is the code:

enter image description here

import arcpy

#Add constants
mxd = arcpy.mapping.MapDocument("Current")
df = arcpy.mapping.ListDataFrames(mxd,'*')[0]
Myconnfile = r"redacted1filepath"
Mylayer = r"redacted2filepath"
addlayer1 = arcpy.mapping.Layer(Mylayer + r"\STWMGR.STW_ARCS2.lyr")
addlayer2 = arcpy.mapping.Layer(Mylayer + r"\STWMGR.STW_FACILITIES2.lyr")
addlayer3 = arcpy.mapping.Layer(Mylayer + r"\STWMGR.STW_NODES2.lyr")
addlayer4 = arcpy.mapping.Layer(Mylayer + r"\STWMGR.STW_ROUTE_REACHES2.lyr")
addlayer5 = arcpy.mapping.Layer(Mylayer + r"\STWMGR.STW_JUNCTIONS2.lyr")


#Add the new layers and put them in the right order
for lyr in arcpy.mapping.ListLayers(mxd, "",df):
    if lyr.supports("DATASOURCE"):
        if r"STWMGR.STORMNET_FEATURES\STWMGR.STORMNET_ARCS" in lyr.dataSource:
            arcpy.mapping.AddLayer(df, addlayer1, "BOTTOM")
            yr1 = arcpy.mapping.ListLayers(mxd, "STWMGR.STW_ARCS",df)[0]
            arcpy.mapping.MoveLayer(df, lyr, yr1, "AFTER")
    else:
        pass
for lyr in arcpy.mapping.ListLayers(mxd, "",df):
    if lyr.supports("DATASOURCE"):
        if r"STWMGR.STORMNET_FEATURES\STWMGR.STORMNET_FACILITIES" in lyr.dataSource:
            arcpy.mapping.AddLayer(df, addlayer2, "BOTTOM")
            yr2 = arcpy.mapping.ListLayers(mxd, "STWMGR.STW_FACILITIES",df)[0]
            arcpy.mapping.MoveLayer(df, lyr, yr2, "AFTER")
    else:
        pass
for lyr in arcpy.mapping.ListLayers(mxd, "",df):
    if lyr.supports("DATASOURCE"):
        if r"STWMGR.STORMNET_FEATURES\STWMGR.STORMNET_NODES" in lyr.dataSource:
            arcpy.mapping.AddLayer(df, addlayer3, "BOTTOM")
            yr3 = arcpy.mapping.ListLayers(mxd, "STWMGR.STW_NODES",df)[0]
            arcpy.mapping.MoveLayer(df, lyr, yr3, "AFTER")
    else:
        pass
for lyr in arcpy.mapping.ListLayers(mxd, "",df):
    if lyr.supports("DATASOURCE"):
        if r"STWMGR.FHD_HYDROGRAPHY\STWMGR.FHD_ROUTE_REACHES" in lyr.dataSource:
            arcpy.mapping.AddLayer(df, addlayer4, "BOTTOM")
            yr4 = arcpy.mapping.ListLayers(mxd, "STWMGR.STW_ROUTE_REACHES",df)[0]
            arcpy.mapping.MoveLayer(df, lyr, yr4, "AFTER")
    else:
        pass
for lyr in arcpy.mapping.ListLayers(mxd, "",df):
    if lyr.supports("DATASOURCE"):
        if r"STWMGR.FHD_HYDROGRAPHY\STWMGR.FHD_JUNCTIONS" in lyr.dataSource:
            arcpy.mapping.AddLayer(df, addlayer5, "BOTTOM")
            yr5 = arcpy.mapping.ListLayers(mxd, "STWMGR.STW_JUNCTIONS",df)[0]
            arcpy.mapping.MoveLayer(df, lyr, yr5, "AFTER")
    else:
        pass

#Delete the old layers
##################################################################################

for lyr in arcpy.mapping.ListLayers(mxd, "",df):
    if lyr.supports("DATASOURCE"):
        if r"STWMGR.STORMNET_FEATURES\STWMGR.STORMNET_ARCS" in lyr.dataSource:
            arcpy.mapping.RemoveLayer(df, lyr)
            print(lyr.dataSource + ": " + "Layer Deleted")
    else:
        pass
for lyr in arcpy.mapping.ListLayers(mxd, "",df):
    if lyr.supports("DATASOURCE"):
        if r"STWMGR.STORMNET_FEATURES\STWMGR.STORMNET_FACILITIES" in lyr.dataSource:
            arcpy.mapping.RemoveLayer(df, lyr)
            print(lyr.dataSource + ": " + "Layer Deleted")
    else:
        pass
for lyr in arcpy.mapping.ListLayers(mxd, "",df):
    if lyr.supports("DATASOURCE"):
        if r"STWMGR.STORMNET_FEATURES\STWMGR.STORMNET_NODES" in lyr.dataSource:
            arcpy.mapping.RemoveLayer(df, lyr)
            print(lyr.dataSource + ": " + "Layer Deleted")
    else:
        pass
for lyr in arcpy.mapping.ListLayers(mxd, "",df):
    if lyr.supports("DATASOURCE"):
        if r"STWMGR.FHD_HYDROGRAPHY\STWMGR.FHD_ROUTE_REACHES" in lyr.dataSource:
            arcpy.mapping.RemoveLayer(df, lyr)
            print(lyr.dataSource + ": " + "Layer Deleted")
    else:
        pass
for lyr in arcpy.mapping.ListLayers(mxd, "",df):
    if lyr.supports("DATASOURCE"):
        if r"STWMGR.FHD_HYDROGRAPHY\STWMGR.FHD_JUNCTIONS" in lyr.dataSource:
            arcpy.mapping.RemoveLayer(df, lyr)
            print(lyr.dataSource + ": " + "Layer Deleted")
    else:
        pass


#Clean up the mess
arcpy.RefreshTOC()
arcpy.RefreshActiveView()
del Myconnfile
del Mylayer
del addlayer1
del addlayer2
del addlayer3
del addlayer4
del addlayer5
del df
del lyr
del mxd
del yr1
del yr2
del yr3
del yr4
del yr5
  • are you trying to keep the same layers in the same order, but give a new source for each layer? – Midavalo Feb 9 '16 at 21:43
  • I am, at this point I am open to re-pointing the source but having tried that already I am worried that might not work either. – user63964 Feb 9 '16 at 22:33
1

A few small modifications to your code, but replaces each layer with a new layer and keeps the same layer order. Combined everything as elif into the one for, and added the remove into each if.

Before:
enter image description here

After:
enter image description here

import arcpy

#Add constants
mxd = arcpy.mapping.MapDocument("Current")
df = arcpy.mapping.ListDataFrames(mxd,'*')[0]

Mylayer = r"D:\Temp\GISSE"

addlayer1 = arcpy.mapping.Layer(Mylayer + r"\STW_ARCS.lyr")
addlayer2 = arcpy.mapping.Layer(Mylayer + r"\STW_FACILITIES.lyr")
addlayer3 = arcpy.mapping.Layer(Mylayer + r"\STW_NODES.lyr")
addlayer4 = arcpy.mapping.Layer(Mylayer + r"\STW_ROUTE_REACHES.lyr")
addlayer5 = arcpy.mapping.Layer(Mylayer + r"\STW_JUNCTIONS.lyr")

#Add the new layers and put them in the right order
for lyr in arcpy.mapping.ListLayers(mxd, "",df):
    if lyr.supports("DATASOURCE"):
        if r"STORMNET_ARCS" in lyr.dataSource:
            arcpy.mapping.AddLayer(df, addlayer1, "BOTTOM")
            yr1 = arcpy.mapping.ListLayers(mxd, "STW_ARCS",df)[0]
            arcpy.mapping.MoveLayer(df, lyr, yr1, "AFTER")
            arcpy.mapping.RemoveLayer(df, lyr)

        elif r"STORMNET_FACILITIES" in lyr.dataSource:
            arcpy.mapping.AddLayer(df, addlayer2, "BOTTOM")
            yr1 = arcpy.mapping.ListLayers(mxd, "STW_FACILITIES",df)[0]
            arcpy.mapping.MoveLayer(df, lyr, yr1, "AFTER")
            arcpy.mapping.RemoveLayer(df, lyr)

        elif r"STORMNET_NODES" in lyr.dataSource:
            arcpy.mapping.AddLayer(df, addlayer3, "BOTTOM")
            yr1 = arcpy.mapping.ListLayers(mxd, "STW_NODES",df)[0]
            arcpy.mapping.MoveLayer(df, lyr, yr1, "AFTER")
            arcpy.mapping.RemoveLayer(df, lyr)

        elif r"FHD_ROUTE_REACHES" in lyr.dataSource:
            arcpy.mapping.AddLayer(df, addlayer4, "BOTTOM")
            yr1 = arcpy.mapping.ListLayers(mxd, "STW_ROUTE_REACHES",df)[0]
            arcpy.mapping.MoveLayer(df, lyr, yr1, "AFTER")
            arcpy.mapping.RemoveLayer(df, lyr)

        elif r"FHD_JUNCTIONS" in lyr.dataSource:
            arcpy.mapping.AddLayer(df, addlayer5, "BOTTOM")
            yr1 = arcpy.mapping.ListLayers(mxd, "STW_JUNCTIONS",df)[0]
            arcpy.mapping.MoveLayer(df, lyr, yr1, "AFTER")
            arcpy.mapping.RemoveLayer(df, lyr)

arcpy.RefreshTOC()
arcpy.RefreshActiveView()
1

This is the final solution I used, thanks to user "Midavalo" for providing the breakthrough I needed with the provided code:

    import arcpy
    #Add constants
    mxd = arcpy.mapping.MapDocument(path1)
    df = arcpy.mapping.ListDataFrames(mxd,'*')[0]
    Mylayer = r"path2"
    addlayer1 = arcpy.mapping.Layer(Mylayer + r"\STWMGR.STW_ARCS.lyr")
    addlayer2 = arcpy.mapping.Layer(Mylayer + r"\STWMGR.STW_FACILITIES.lyr")
    addlayer3 = arcpy.mapping.Layer(Mylayer + r"\STWMGR.STW_NODES.lyr")
    addlayer4 = arcpy.mapping.Layer(Mylayer + r"\STWMGR.STW_ROUTE_REACHES.lyr")
    addlayer5 = arcpy.mapping.Layer(Mylayer + r"\STWMGR.STW_JUNCTIONS.lyr")
    addlayer6 = arcpy.mapping.Layer(Mylayer + r"\STWMGR.STW_ARCS_MAINT.lyr")
    addlayer7 = arcpy.mapping.Layer(Mylayer + r"\STWMGR.STW_NODES_MAINT.lyr")
    #Add the new layers and put them in the right order
    for lyr in arcpy.mapping.ListLayers(mxd, "",df):
        if lyr.supports("DATASOURCE"):
            if r"STWMGR.STORMNET_FEATURES\STWMGR.STORMNET_ARCS" in lyr.dataSource:
                arcpy.mapping.AddLayer(df, addlayer1, "BOTTOM")
                yr1 = arcpy.mapping.ListLayers(mxd, "STWMGR.STW_ARCS",df)[0]
                arcpy.mapping.AddLayer(df, addlayer6, "BOTTOM")
                yr1maint = arcpy.mapping.ListLayers(mxd, "STWMGR.STW_ARCS_MAINT",df)[0]
                arcpy.mapping.MoveLayer(df, lyr, yr1maint, "AFTER")
                arcpy.mapping.MoveLayer(df, lyr, yr1, "AFTER")
                arcpy.mapping.RemoveLayer(df, lyr)
                arcpy.RefreshTOC()

            elif r"STWMGR.STORMNET_FEATURES\STWMGR.STORMNET_FACILITIES" in lyr.dataSource:
                arcpy.mapping.AddLayer(df, addlayer2, "BOTTOM")
                yr2 = arcpy.mapping.ListLayers(mxd, "STWMGR.STW_FACILITIES",df)[0]
                arcpy.mapping.MoveLayer(df, lyr, yr2, "AFTER")
                arcpy.mapping.RemoveLayer(df, lyr)
                arcpy.RefreshTOC()

            elif r"STWMGR.STORMNET_FEATURES\STWMGR.STORMNET_NODES" in lyr.dataSource:
                arcpy.mapping.AddLayer(df, addlayer3, "BOTTOM")
                yr3 = arcpy.mapping.ListLayers(mxd, "STWMGR.STW_NODES",df)[0]
                arcpy.mapping.AddLayer(df, addlayer7, "BOTTOM")
                yr3maint = arcpy.mapping.ListLayers(mxd, "STWMGR.STW_NODES_MAINT",df)[0]
                arcpy.mapping.MoveLayer(df, lyr, yr3maint, "AFTER")
                arcpy.mapping.MoveLayer(df, lyr, yr3, "AFTER")
                arcpy.mapping.RemoveLayer(df, lyr)
                arcpy.RefreshTOC()

            elif r"STWMGR.FHD_HYDROGRAPHY\STWMGR.FHD_ROUTE_REACHES" in lyr.dataSource:
                arcpy.mapping.AddLayer(df, addlayer4, "BOTTOM")
                yr4 = arcpy.mapping.ListLayers(mxd, "STWMGR.STW_ROUTE_REACHES",df)[0]
                arcpy.mapping.MoveLayer(df, lyr, yr4, "AFTER")
                arcpy.mapping.RemoveLayer(df, lyr)
                arcpy.RefreshTOC()

            elif r"STWMGR.FHD_HYDROGRAPHY\STWMGR.FHD_JUNCTIONS" in lyr.dataSource:
                arcpy.mapping.AddLayer(df, addlayer5, "BOTTOM")
                yr5 = arcpy.mapping.ListLayers(mxd, "STWMGR.STW_JUNCTIONS",df)[0]
                arcpy.mapping.MoveLayer(df, lyr, yr5, "AFTER")
                arcpy.mapping.RemoveLayer(df, lyr)
                arcpy.RefreshTOC()
    mxd.save()

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.