Select to view content in your preferred language

Add a multi Layer files to mxd using python

6626
6
Jump to solution
01-19-2015 01:57 AM
Yaron_YosefCohen
Frequent Contributor

Hello

I have more than 300 layer files located in many sub-Folders and sub-sub-Folders. All Sub Folders are located in one large directory.I read http://gis.stackexchange.com/questions/35693/use-python-to-add-layers-to-toc?rq=1

and I try, with arcpy, to detect all the layers and add them to mxd.

Is it possible?

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

Hi,

First of all you should really use syntax highlighting: Posting Code blocks in the new GeoNet

To recursively loop through folder and sub folder and detect the layerfiles (.lyr) you can use the arcpy.da.Walk functionality (asuming you have a recent version of ArcGIS).

import arcpy
import os

workspace = r"D:\Xander\GeoNet"

walk = arcpy.da.Walk(workspace, datatype="Layer")
for dirpath, dirnames, filenames in walk:
    for filename in filenames:
        print (os.path.join(dirpath, filename))

This prints each .lyr file it finds:

D:\Xander\GeoNet\StackedLabels\aLayer.lyr

D:\Xander\GeoNet\StackedLabels\subfolder\subsubfolder\subsubsubfolder\AnotherLayer.lyr

In case you want to add all the layer found to an existing MXD you should open the MXD before the loop and add put the add layerfile logic inside the loop. Adding a lot of layer to an MXD will probably make it very slow.

Your code might look like this (in case you run this inside a session of ArcMap and want to add the layers to the current session of ArcMap, hence the keyword "CURRENT"):

import arcpy
import os

# suppose you want to add it to the current MXD (open MXD)
mxd = arcpy.mapping.MapDocument("CURRENT")
dataFrame = arcpy.mapping.ListDataFrames(mxd, "*")[0]

# base folder
workspace = r"D:\Xander\GeoNet"
walk = arcpy.da.Walk(workspace, datatype="Layer")

for dirpath, dirnames, filenames in walk:
    for filename in filenames:
        layerfile = os.path.join(dirpath, filename)
        addlayer = arcpy.mapping.Layer(layerfile)
        arcpy.mapping.AddLayer(dataFrame, addlayer, "BOTTOM")

arcpy.RefreshTOC()
arcpy.RefreshActiveView()
del addlayer, mxd

In case you want to run it as a standalone script and/or add the layers to a different existing MXD, you should replace the keyword "CURRENT" on line 5 by a reference (string containing path and filename) to the MXD.

View solution in original post

6 Replies
KishorGhatage
Deactivated User

I believe it is possible.

Search the layer using .lyr extension in the directory and then use Add layer to add the searched lyr in MXD.

Add layer

http://resources.arcgis.com/en/help/main/10.2/index.html#//00s300000025000000

import arcpy, os

folderPath = r"C:\Project"

for filename in os.listdir(folderPath):

    fullpath = os.path.join(folderPath, filename)

    if os.path.isfile(fullpath):

        basename, extension = os.path.splitext(fullpath)

        if extension.lower() == ".mxd":

            # Use Add layer here

            mxd.save()

del mxd

In the  above code .mxd files are searched. Using  the same code you can search your .lyr and add then in the desired mxd.

I am not python expert. Hope this information is helpful.

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi,

First of all you should really use syntax highlighting: Posting Code blocks in the new GeoNet

To recursively loop through folder and sub folder and detect the layerfiles (.lyr) you can use the arcpy.da.Walk functionality (asuming you have a recent version of ArcGIS).

import arcpy
import os

workspace = r"D:\Xander\GeoNet"

walk = arcpy.da.Walk(workspace, datatype="Layer")
for dirpath, dirnames, filenames in walk:
    for filename in filenames:
        print (os.path.join(dirpath, filename))

This prints each .lyr file it finds:

D:\Xander\GeoNet\StackedLabels\aLayer.lyr

D:\Xander\GeoNet\StackedLabels\subfolder\subsubfolder\subsubsubfolder\AnotherLayer.lyr

In case you want to add all the layer found to an existing MXD you should open the MXD before the loop and add put the add layerfile logic inside the loop. Adding a lot of layer to an MXD will probably make it very slow.

Your code might look like this (in case you run this inside a session of ArcMap and want to add the layers to the current session of ArcMap, hence the keyword "CURRENT"):

import arcpy
import os

# suppose you want to add it to the current MXD (open MXD)
mxd = arcpy.mapping.MapDocument("CURRENT")
dataFrame = arcpy.mapping.ListDataFrames(mxd, "*")[0]

# base folder
workspace = r"D:\Xander\GeoNet"
walk = arcpy.da.Walk(workspace, datatype="Layer")

for dirpath, dirnames, filenames in walk:
    for filename in filenames:
        layerfile = os.path.join(dirpath, filename)
        addlayer = arcpy.mapping.Layer(layerfile)
        arcpy.mapping.AddLayer(dataFrame, addlayer, "BOTTOM")

arcpy.RefreshTOC()
arcpy.RefreshActiveView()
del addlayer, mxd

In case you want to run it as a standalone script and/or add the layers to a different existing MXD, you should replace the keyword "CURRENT" on line 5 by a reference (string containing path and filename) to the MXD.

KishorGhatage
Deactivated User

Thanks Xander. It's very helpful.

0 Kudos
Yaron_YosefCohen
Frequent Contributor

Hello

I cant find the >> sign so the code will marked...

i run this code and nothing happend. the first code is working

what to do now?

import arcpy 

import os 

 

# suppose you want to add it to the current MXD (open MXD) 

mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd") 

dataFrame = arcpy.mapping.ListDataFrames(mxd, "*")[0] 

 

# base folder 

workspace = r"C:\Project\layers" 

walk = arcpy.da.Walk(workspace, datatype="Layer") 

 

for dirpath, dirnames, filenames in walk: 

    for filename in filenames: 

        layerfile = os.path.join(dirpath, filename) 

        addlayer = arcpy.mapping.Layer(layerfile) 

        arcpy.mapping.AddLayer(dataFrame, addlayer, "BOTTOM") 

 

arcpy.RefreshTOC() 

arcpy.RefreshActiveView() 

del addlayer, mxd

0 Kudos
KishorGhatage
Deactivated User

Just added save statement

import arcpy
import os

# suppose you want to add it to the current MXD (open MXD)
mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd")
dataFrame = arcpy.mapping.ListDataFrames(mxd, "*")[0]

# base folder
workspace = r"C:\Project\layers"
walk = arcpy.da.Walk(workspace, datatype="Layer")

for dirpath, dirnames, filenames in walk:
    for filename in filenames:
        layerfile = os.path.join(dirpath, filename)
        addlayer = arcpy.mapping.Layer(layerfile)
        arcpy.mapping.AddLayer(dataFrame, addlayer, "BOTTOM")
        mxd.save()

arcpy.RefreshTOC()
arcpy.RefreshActiveView()
del addlayer, mxd
0 Kudos
Yaron_YosefCohen
Frequent Contributor

the code added just 2 lyr.file in the workspace. it didn't added the lyr in the sub folders

0 Kudos