arcpy.mapping.UpdateLayer vs arcpy.ApplySymbologyFromLayer

741
1
Jump to solution
02-05-2013 11:32 AM
RyanClancy
Occasional Contributor
I want to change the symbology of a raster so that it matches a .lyr file I have stashed away. Once the symbology is set I will read the classBreakValues and build a RemapRange so that I can reclassify the raster cell values using Reclassify.

I would prefer to do this via arcpy.ApplySymbologyFromLayer because it allows me to work outside of an MXD. The problem is that it doesn't seem to apply the symbology correctly. My .lyr file symbology uses the Standard Deviation classification method with 3 classes but when I run ApplySymbologyFromLayer the result always has 4 classes, even when I apply the symbology to the original source raster I used to create the .lyr file. Attempting to set the numClasses = 3 after applying the symbology doesn't work.

Conversely, arcpy.mapping.UpdateLayer applies the symbology correctly but requires an mxd and dataframe as input parameters. I want to minimize file dependencies and would prefer not to have dummy .mxd's laying around, which is what I need in order to use UpdateLayer.

So, why does a single .lyr file produce two different results when applied to the same dataset via UpdateLayer and ApplySymbologyFromLayer?

This script will be used by many users on many different rasters and I really, really, really, don't want to have dummy .mxd's stashed around our network. Is there any way to avoid this? I would prefer not to have a file dependency on the .lyr file either; does anyone see a way around that? Shouldn't there be a classificationMethod property so I can do something like:

layer.symbology.classificationMethod = standard_deviation
layer.symbology.numClasses = 3
layer.symbology.reclassify()
0 Kudos
1 Solution

Accepted Solutions
RyanClancy
Occasional Contributor
Since I don't need a visual output (i.e. a map) I decided to do away with the ArcGIS symbol render and do the classification myself. I break the data into three classes using standard deviation like so:
  def Reclass(value)     rasterSTD = arcpy.GetRasterProperties_management(my_raster, "STD")     std = rasterSTD.getOutput(0)     rasterMEAN = arcpy.GetRasterProperties_management(my_raster, "MEAN")     mean = rasterMEAN.getOutput(0)      diff = abs(value - mean)      if (diff < std):         return 1     elif ((diff > std) & (diff < 2*std)):         return 2     elif (diff > 2*std):         return 3


...presto! This has solved my file dependency (.lyr & .mxd) issue as well as the inconsistent results between ApplySymbologyFromLayer vs arcpy.mapping.UpdateLayer.

View solution in original post

0 Kudos
1 Reply
RyanClancy
Occasional Contributor
Since I don't need a visual output (i.e. a map) I decided to do away with the ArcGIS symbol render and do the classification myself. I break the data into three classes using standard deviation like so:
  def Reclass(value)     rasterSTD = arcpy.GetRasterProperties_management(my_raster, "STD")     std = rasterSTD.getOutput(0)     rasterMEAN = arcpy.GetRasterProperties_management(my_raster, "MEAN")     mean = rasterMEAN.getOutput(0)      diff = abs(value - mean)      if (diff < std):         return 1     elif ((diff > std) & (diff < 2*std)):         return 2     elif (diff > 2*std):         return 3


...presto! This has solved my file dependency (.lyr & .mxd) issue as well as the inconsistent results between ApplySymbologyFromLayer vs arcpy.mapping.UpdateLayer.
0 Kudos