automated symbology for many layers

3785
8
Jump to solution
12-30-2012 02:53 PM
AdrianRingenbach1
New Contributor III
HI THERE

i have to szmbolyse a lot of rasterdatasets the same way. as I allready read, it should be possible to use arcpy.mapper i tried it to create a iteration< to get all rasters with the name #Reach_probability# out of my workspace. afterwards it would be realz nice< if those rasters (and not justone, all thogether > multiple) could figure as input rasters of the tool #apply symbology from layer#...   but this is not working..

here my code *exported from modelbuilder#


# ---------------------------------------------------------------------------
# iteration durch raster_und symbol.py
# Created on: 2012-12-31 01:39:43.00000
#   (generated by ArcGIS/ModelBuilder)
# Description:
# ---------------------------------------------------------------------------

# Import arcpy module
import arcpy

# Load required toolboxes
arcpy.ImportToolbox("Model Functions")


# Local variables:
RESULTATE = "\\\\ad.geo.unizh.ch\\home\\myhome\\Desktop\\Masterarbeit\\test\\RESULTATE"
Reach_Probability_____lyr = "\\\\ad.geo.unizh.ch\\home\\myhome\\Desktop\\Masterarbeit\\test\\RESULTATE\\lyr_files\\Reach Probability [%].lyr"

# Process: Iterate Rasters
arcpy.IterateRasters_mb(RESULTATE, "Reach_probability*", "ASC", "RECURSIVE")

# Process: Apply Symbology From Layer
tempEnvironment0 = arcpy.env.extent
arcpy.env.extent = "\\\\ad.geo.unizh.ch\\home\\myhome\\Desktop\\Masterarbeit\\test\\RESULTATE\\Raster"
arcpy.ApplySymbologyFromLayer_management("", Reach_Probability_____lyr)
arcpy.env.extent = tempEnvironment0





thanks for any help or tips
Tags (2)
1 Solution

Accepted Solutions
MelanieMaguire
New Contributor III
Hi Adrian, 

You are.  In terms of looping through the files in a folder or geodatabase, do you have a folder that already contains a lyr file for each raster that you want to update or do you have a folder/geodatabase that contains rasters that you would like to generate layer files for?

Here is code that loops through the layers in an mxd and applies the symbology of the source layer to all of the rasters in the mxd that start with "Reach_probability:

import arcpy  try:           #Specifies mxd       mxd = arcpy.mapping.MapDocument(r"H:/Desktop/Masterarbeit/test/Besprechung_Crap Ses.mxd")          #Specifies dataframe of interest     df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]          #Specifies layer file with the symbology you want to apply to the other raster layers     sourceLayer = arcpy.mapping.Layer(r"H:\Desktop\Masterarbeit\test\RESULTATE\lyr_files\Reach Probability.lyr")             #Loops through layers in the first data frame in your mxd and      #creates a list of Layers starting with Reach_probability:     for lyr in arcpy.mapping.ListLayers(mxd, "Reach_probability*", df):         #Determines if layer is a raster layer and applies lyr symbology if it is         if lyr.isRasterLayer:             arcpy.ApplySymbologyFromLayer_management(lyr, sourceLayer)                  #Saves mxd     mxd.save()  finally:          #Deletes the reference to the mxd from memory (not the actual mxd file)     del mxd

View solution in original post

0 Kudos
8 Replies
MelanieMaguire
New Contributor III
Hi Adrian,

The iterator tools are model builder only tools that do not work in Python scripts.  In your Python script, you can accomplish the same iteration with a for loop that iterates through a list of all the layers in your map using ListLayers. I will post a code example later. 

The code you want will be similar to the second example on the ListLayers help page, but your if statement will limit the layers to raster layers.  The code inside the if statement will apply symbology to your rasters like the code at the bottom of the Raster Classified Symbology help page. 

Mel
0 Kudos
AdrianRingenbach1
New Contributor III
Hi Melanie,

Thanks for your reply and the remark, that the "iterations" just works within the modelbuilder. The mentioned for-loop construction within a map document (.mxd) would be useful, even better would be one within a workspace or folder. But as far as I can assess it (-> ListLayers), this is not possible this way. Thanks for your help, I would really appreciate a code example... 🙂  i'm trying and trying... but dont get anywhere...
0 Kudos
AdrianRingenbach1
New Contributor III
My current version looks like this

import arcpy
mxd = arcpy.mapping.MapDocument(r"H:/Desktop/Masterarbeit/test/Besprechung_Crap Ses.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
lyrFile = arcpy.mapping.Layer(r"H:\Desktop\Masterarbeit\test\RESULTATE\lyr_files\Reach Probability [%].lyr")
for lyr in arcpy.mapping.ListLayers(mxd, "Reach_probability", df):
    arcpy.mapping.UpdateLayer(df, lyr, lyrFile, True)
    if lyr.isRasterLayer == TRUE:
        lyr.symbology.reclassify()
del mxd, df, lyr, lyrFile


which implies this error message

Traceback (most recent call last):
  File "H:/Desktop/Masterarbeit/test_1", line 19, in <module>
    arcpy.mapping.UpdateLayer(df, lyr, lyrFile, True)
  File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\utils.py", line 181, in fn_
    return fn(*args, **kw)
  File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\mapping.py", line 1460, in UpdateLayer
    update_layer._arc_object.renderer = source_layer._arc_object.renderer
RuntimeError: LayerObject: Set attribute renderer does not exist


Am I on the right track? how can I set the attribute renderer? Thanks for feedbacks
0 Kudos
MelanieMaguire
New Contributor III
Hi Adrian, 

You are.  In terms of looping through the files in a folder or geodatabase, do you have a folder that already contains a lyr file for each raster that you want to update or do you have a folder/geodatabase that contains rasters that you would like to generate layer files for?

Here is code that loops through the layers in an mxd and applies the symbology of the source layer to all of the rasters in the mxd that start with "Reach_probability:

import arcpy  try:           #Specifies mxd       mxd = arcpy.mapping.MapDocument(r"H:/Desktop/Masterarbeit/test/Besprechung_Crap Ses.mxd")          #Specifies dataframe of interest     df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]          #Specifies layer file with the symbology you want to apply to the other raster layers     sourceLayer = arcpy.mapping.Layer(r"H:\Desktop\Masterarbeit\test\RESULTATE\lyr_files\Reach Probability.lyr")             #Loops through layers in the first data frame in your mxd and      #creates a list of Layers starting with Reach_probability:     for lyr in arcpy.mapping.ListLayers(mxd, "Reach_probability*", df):         #Determines if layer is a raster layer and applies lyr symbology if it is         if lyr.isRasterLayer:             arcpy.ApplySymbologyFromLayer_management(lyr, sourceLayer)                  #Saves mxd     mxd.save()  finally:          #Deletes the reference to the mxd from memory (not the actual mxd file)     del mxd
0 Kudos
AdrianRingenbach1
New Contributor III
Wow cool, this script worked well:), Thanks!! I asked for looping through a folder, because i haven't loaded yet all raster-layers into the .mxd. This is a bit extra work, because i have to generate sub- and sub-sub-folders.
But by now, i have prepared my seven different .lyr-source files. Next step will  be to load all layers into the map and then i will apply the script again to all layers with all different source layers...
0 Kudos
MelanieMaguire
New Contributor III
I have not tried it in my code yet, but there is a walk function in ArcPy 10.1 that should be able to navigate your folders and subfolders if they are nested.  It looks like it would be useful for adding all of the rasters to your mxd.  I might test that out this evening.
0 Kudos
AdrianRingenbach1
New Contributor III
Hi,

to load the layers to the mxd wasn't that hard. But instead of altering the code for each of the 20 (i recounted them:cool:) and dispatch them all one by one, i thought it should be possible to nest them. The result is the following code. First the nice thing about: the script worked totally through, all the 20. "dones" were printed.
But when i opened afterwards the mxd i realised, that just the symbology of sourceLayer1 and sourceLayer2 were adapted. Do you see the fault or problem?

# ---------------------------------------------------------------------------
# iteration durch raster_und symbol.py
# Created on: 2012-12-31 01:39:43.00000
#   (generated by ArcGIS/ModelBuilder)
# Description: 
# ---------------------------------------------------------------------------


import arcpy



#Specifies mxd  
mxd = arcpy.mapping.MapDocument(r"H:/Desktop/Masterarbeit/test/Besprechung_Crap Ses.mxd")

#Specifies dataframe of interest
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]

#Specifies layer file with the symbology you want to apply to the other raster layers
sourceLayer1 = arcpy.mapping.Layer(r"H:\Desktop\Masterarbeit\test\RESULTATE\lyr_files_neu\E_95CI.lyr")   
sourceLayer2 = arcpy.mapping.Layer(r"H:\Desktop\Masterarbeit\test\RESULTATE\lyr_files_neu\E_95CI_diff.lyr")   
sourceLayer3 = arcpy.mapping.Layer(r"H:\Desktop\Masterarbeit\test\RESULTATE\lyr_files_neu\E_mean.lyr")   
sourceLayer4 = arcpy.mapping.Layer(r"H:\Desktop\Masterarbeit\test\RESULTATE\lyr_files_neu\E_mean_diff.lyr")   
sourceLayer5 = arcpy.mapping.Layer(r"H:\Desktop\Masterarbeit\test\RESULTATE\lyr_files_neu\Nr_depos_diff.lyr")   
sourceLayer6 = arcpy.mapping.Layer(r"H:\Desktop\Masterarbeit\test\RESULTATE\lyr_files_neu\Nr_deposited.lyr")   
sourceLayer7 = arcpy.mapping.Layer(r"H:\Desktop\Masterarbeit\test\RESULTATE\lyr_files_neu\Nr_pass_diff.lyr")   
sourceLayer8 = arcpy.mapping.Layer(r"H:\Desktop\Masterarbeit\test\RESULTATE\lyr_files_neu\Nr_passages.lyr")   
sourceLayer9 = arcpy.mapping.Layer(r"H:\Desktop\Masterarbeit\test\RESULTATE\lyr_files_neu\Nr_tree_impacts.lyr")   
sourceLayer10 = arcpy.mapping.Layer(r"H:\Desktop\Masterarbeit\test\RESULTATE\lyr_files_neu\Ph_95CI.lyr")   
sourceLayer11 = arcpy.mapping.Layer(r"H:\Desktop\Masterarbeit\test\RESULTATE\lyr_files_neu\Ph_95CI_diff.lyr")   
sourceLayer12 = arcpy.mapping.Layer(r"H:\Desktop\Masterarbeit\test\RESULTATE\lyr_files_neu\Ph_mean.lyr")   
sourceLayer13 = arcpy.mapping.Layer(r"H:\Desktop\Masterarbeit\test\RESULTATE\lyr_files_neu\Ph_mean_diff.lyr")   
sourceLayer14 = arcpy.mapping.Layer(r"H:\Desktop\Masterarbeit\test\RESULTATE\lyr_files_neu\Reach Probability [%].lyr")   
sourceLayer15 = arcpy.mapping.Layer(r"H:\Desktop\Masterarbeit\test\RESULTATE\lyr_files_neu\Reach_prob_diff.lyr")   
sourceLayer16 = arcpy.mapping.Layer(r"H:\Desktop\Masterarbeit\test\RESULTATE\lyr_files_neu\Rvol.lyr")   
sourceLayer17 = arcpy.mapping.Layer(r"H:\Desktop\Masterarbeit\test\RESULTATE\lyr_files_neu\Rvol_diff.lyr")   
sourceLayer18 = arcpy.mapping.Layer(r"H:\Desktop\Masterarbeit\test\RESULTATE\lyr_files_neu\Tree_impact_heights .lyr")   
sourceLayer19 = arcpy.mapping.Layer(r"H:\Desktop\Masterarbeit\test\RESULTATE\lyr_files_neu\Vmax.lyr")
sourceLayer20 = arcpy.mapping.Layer(r"H:\Desktop\Masterarbeit\test\RESULTATE\lyr_files_neu\Vmax_diff.lyr")   
   


#Loops through layers in the first data frame in your mxd and 
#creates a list of Layers starting with Reach_probability:
for lyr in arcpy.mapping.ListLayers(mxd, "E_95CI", df):
    #Determines if layer is a raster layer and applies lyr symbology if it is
    if lyr.isRasterLayer:
        arcpy.ApplySymbologyFromLayer_management(lyr, sourceLayer1)

print "done_ 1"

for lyr in arcpy.mapping.ListLayers(mxd, "E_95CI_diff*", df):
    #Determines if layer is a raster layer and applies lyr symbology if it is
    if lyr.isRasterLayer:
        arcpy.ApplySymbologyFromLayer_management(lyr, sourceLayer2)

#Saves mxd
mxd.save()
print "done_2"


for lyr in arcpy.mapping.ListLayers(mxd, "E_mean", df):
    #Determines if layer is a raster layer and applies lyr symbology if it is
    if lyr.isRasterLayer:
        arcpy.ApplySymbologyFromLayer_management(lyr, sourceLayer3)

print "done_3"


for lyr in arcpy.mapping.ListLayers(mxd, "E_mean_diff*", df):
    #Determines if layer is a raster layer and applies lyr symbology if it is
    if lyr.isRasterLayer:
        arcpy.ApplySymbologyFromLayer_management(lyr, sourceLayer4)

#Saves mxd
mxd.save()
print "done_4"


for lyr in arcpy.mapping.ListLayers(mxd, "Nr_depos_diff*", df):
    #Determines if layer is a raster layer and applies lyr symbology if it is
    if lyr.isRasterLayer:
        arcpy.ApplySymbologyFromLayer_management(lyr, sourceLayer5)

print "done_5"


for lyr in arcpy.mapping.ListLayers(mxd, "Nr_deposited", df):
    #Determines if layer is a raster layer and applies lyr symbology if it is
    if lyr.isRasterLayer:
        arcpy.ApplySymbologyFromLayer_management(lyr, sourceLayer6)

#Saves mxd
mxd.save()
print "done_6"


for lyr in arcpy.mapping.ListLayers(mxd, "Nr_pass_diff*", df):
    #Determines if layer is a raster layer and applies lyr symbology if it is
    if lyr.isRasterLayer:
        arcpy.ApplySymbologyFromLayer_management(lyr, sourceLayer7)

print "done_7"


for lyr in arcpy.mapping.ListLayers(mxd, "Nr_passages", df):
    #Determines if layer is a raster layer and applies lyr symbology if it is
    if lyr.isRasterLayer:
        arcpy.ApplySymbologyFromLayer_management(lyr, sourceLayer8)

#Saves mxd
mxd.save()
print "done_8"

for lyr in arcpy.mapping.ListLayers(mxd, "Nr_tree_impacts*", df):
    #Determines if layer is a raster layer and applies lyr symbology if it is
    if lyr.isRasterLayer:
        arcpy.ApplySymbologyFromLayer_management(lyr, sourceLayer9)

print "done_9"


for lyr in arcpy.mapping.ListLayers(mxd, "Ph_95CI", df):
    #Determines if layer is a raster layer and applies lyr symbology if it is
    if lyr.isRasterLayer:
        arcpy.ApplySymbologyFromLayer_management(lyr, sourceLayer10)

#Saves mxd
mxd.save()
print "done_10"



for lyr in arcpy.mapping.ListLayers(mxd, "Ph_95CI_diff*", df):
    #Determines if layer is a raster layer and applies lyr symbology if it is
    if lyr.isRasterLayer:
        arcpy.ApplySymbologyFromLayer_management(lyr, sourceLayer11)

print "done_11"


for lyr in arcpy.mapping.ListLayers(mxd, "Ph_mean", df):
    #Determines if layer is a raster layer and applies lyr symbology if it is
    if lyr.isRasterLayer:
        arcpy.ApplySymbologyFromLayer_management(lyr, sourceLayer12)

#Saves mxd
mxd.save()
print "done_12"



for lyr in arcpy.mapping.ListLayers(mxd, "Ph_mean_diff*", df):
    #Determines if layer is a raster layer and applies lyr symbology if it is
    if lyr.isRasterLayer:
        arcpy.ApplySymbologyFromLayer_management(lyr, sourceLayer13)

print "done_13"


for lyr in arcpy.mapping.ListLayers(mxd, "Reach Probability [%]*", df):
    #Determines if layer is a raster layer and applies lyr symbology if it is
    if lyr.isRasterLayer:
        arcpy.ApplySymbologyFromLayer_management(lyr, sourceLayer14)

#Saves mxd
mxd.save()
print "done_14"


for lyr in arcpy.mapping.ListLayers(mxd, "Reach_prob_diff*", df):
    #Determines if layer is a raster layer and applies lyr symbology if it is
    if lyr.isRasterLayer:
        arcpy.ApplySymbologyFromLayer_management(lyr, sourceLayer15)

print "done_15"


for lyr in arcpy.mapping.ListLayers(mxd, "Rvol", df):
    #Determines if layer is a raster layer and applies lyr symbology if it is
    if lyr.isRasterLayer:
        arcpy.ApplySymbologyFromLayer_management(lyr, sourceLayer16)

#Saves mxd
mxd.save()
print "done_15"


for lyr in arcpy.mapping.ListLayers(mxd, "Rvol_diff*", df):
    #Determines if layer is a raster layer and applies lyr symbology if it is
    if lyr.isRasterLayer:
        arcpy.ApplySymbologyFromLayer_management(lyr, sourceLayer17)

print "done_17"

for lyr in arcpy.mapping.ListLayers(mxd, "Tree_impact_heights *", df):
    #Determines if layer is a raster layer and applies lyr symbology if it is
    if lyr.isRasterLayer:
        arcpy.ApplySymbologyFromLayer_management(lyr, sourceLayer18)

#Saves mxd
mxd.save()
print "done_18"


for lyr in arcpy.mapping.ListLayers(mxd, "Vmax", df):
    #Determines if layer is a raster layer and applies lyr symbology if it is
    if lyr.isRasterLayer:
        arcpy.ApplySymbologyFromLayer_management(lyr, sourceLayer19)

print "done_19"


for lyr in arcpy.mapping.ListLayers(mxd, "Vmax_diff*", df):
    #Determines if layer is a raster layer and applies lyr symbology if it is
    if lyr.isRasterLayer:
        arcpy.ApplySymbologyFromLayer_management(lyr, sourceLayer20)

   
#Saves mxd
mxd.save()

print "done_ 20! :D ! "


#Deletes the reference to the mxd from memory (not the actual mxd file)
del mxd



In the "search function", i had intentionally dropped the * within the "E_95CI", because the layers are called exactly like this and there are other layers called E_95CI_diff_... .
Suggestions are welcome. Also for the walk function, I arranged more a workaround (with a long typing code..) than to delve into the walk-function...
AdrianRingenbach1
New Contributor III
Hi Mel,
I got the problem, why it stopped after two layers: it was due to the mxd.save() after every second layer symbolisation.

Thanks a lot for all your Help and your Time you have invested! Now i can really work with the data, compare them and make useful maps!
0 Kudos