1

How can I use the arcpy.mapping.ListLayers() function without calling the current mxd? I'm hoping to run the Python script without having an ArcMap document open.

My setsymbology code runs extremely slowly as it is now. I could do it faster manually, and my computer is definitely not the limiting factor. I'm guessing it's slow because ArcMap is open. I'll show the code as it is now and the code that I'm trying to figure out a way for it to call an input directory/workspace rather than an mxd, but unfortunately it gets errors ('module' object has no attribute 'Listlayers').

# Import system modules
import arcpy
from arcpy import env

mxd = arcpy.mapping.MapDocument("CURRENT")

symbologyLayer = r"C:\VMshared\small_example_valley3\SnowColor2\snowdepthCOLOR.tif.lyr"

for lyr in arcpy.mapping.ListLayers(mxd):
    arcpy.ApplySymbologyFromLayer_management(lyr,symbologyLayer)
5
  • No, the ListLayers needs to know where to list the layers from... the 'workspace' if you will, just the same ListFeatureclasses needs to have the workspace set. You have to call an mxd (any valid mxd) to tell ListLayers where to get its list from. Are you trying to list the layer files in a folder? Feb 6 '15 at 3:17
  • Yes, I'd like to list layer files saved to the SnowDepth3 folder. Or can I save a mxd with the files in the table of contents and call the mxd without arcmap open?
    – AF2k15
    Feb 6 '15 at 3:20
  • Use ListFiles ("*.lyr") resources.arcgis.com/en/help/main/10.1/index.html#//… or os.ListDir() docs.python.org/2/library/os.html#os.listdir with os.path.splitext to find the extension to decide if the file is a layer or not. Does this help any? Feb 6 '15 at 3:24
  • 1
    Possibly...I'm not positive how to edit the code correctly. The layers in the snowdepth3 folder have .tif.lyr extensions. I'll edit my post to show how I'm attempting to use ListFiles(). I also tried ("*.lyr")-----OH CRAP, forgot the acrpy.ListFiles. It works!
    – AF2k15
    Feb 6 '15 at 3:33
  • 1
    Perhaps post that as an answer to your own question. I'm sure someone else will get something out of that in the future. Feb 6 '15 at 3:41
2

To your first question:

how can I use the arcpy.mapping.ListLayers() function without calling the current mxd?

I would say the answer is to use something like:

mxd = arcpy.mapping.MapDocument(r"C:\temp\test.mxd")
for lyr in arcpy.mapping.ListLayers(mxd):
    print lyr.name  # Added to show that ListLayers is working

in place of:

mxd = arcpy.mapping.MapDocument("CURRENT")
for lyr in arcpy.mapping.ListLayers(mxd):
    print lyr.name  # Added to show that ListLayers is working

C:\temp\test.mxd does not need to be open and if you want to save changes to it use mxd.save()


In answer to what your question became, there is a code sample in the Online Help which I abbreviated slightly. It appears to use the same coding pattern that you did in your answer:

# Name: ApplySym.py
# Purpose: apply the symbology from one layer to another

# Import system modules
import arcpy

# Set the current workspace
arcpy.env.workspace = "C:/data"

# Set layer to apply symbology to
inputLayer = "sf_points.lyr"

# Set layer that output symbology will be based on
symbologyLayer = "water_symbols_pnt.lyr"

# Apply the symbology from the symbology layer to the input layer
arcpy.ApplySymbologyFromLayer_management (inputLayer, symbologyLayer)
4
  • That's good advice PolyGeo, it doesn't have to be the current mxd but any valid mxd specified by full path. As it turns out they were after layer files in a folder, not associated with a map document at all, which is why ListFiles was the solution. Feb 6 '15 at 3:51
  • I tested what would happen if I saved a test.mxd with my layer files in the TOC and call it as you say. It doesn't appear to work, but ListFiles successfully changed the symbology from windows command line.
    – AF2k15
    Feb 6 '15 at 4:01
  • 1
    You can use layer files to create layers in your TOC by adding them to your map but you cannot save an mxd with "layer files in the TOC".
    – PolyGeo
    Feb 6 '15 at 4:04
  • Changes made to the layer files added as layers within a map document do not affect the original layer files - this is good news, otherwise changing the symbology would also affect any map that uses the same layer. Unfortunately this also means to update your layer files from a mxd you must save/export them (layer.saveACopy("path") resources.arcgis.com/en/help/main/10.1/index.html#//…) Feb 6 '15 at 4:23
1

Solution --

import arcpy
from arcpy import env

arcpy.env.workspace = r"C:\VMshared\small_example_valley3\SnowDepth3"

arcpy.env.overwriteOutput = True

layerList = arcpy.ListFiles("*.tif.lyr")

symbologyLayer = r"C:\VMshared\small_example_valley3\SnowColor2\snowdepthCOLOR.tif.lyr"

for lyr in layerList:
    arcpy.ApplySymbologyFromLayer_management(lyr,symbologyLayer)

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.