How to update symbology for a raster using arcpy and python?

1203
7
08-09-2021 06:55 AM
RehanChaudhary
Occasional Contributor

i have created a raster and saved it to a layer file. Now i want to change its symbology. Currently it has a "Stretch Colorizer" and i want to assign " ClassifyColorizer"

I am trying to implement the workflow without arcgispro and arcmap.

i came across this function of Update Colorizer which should change the colorizer but i am not sure if this will change all the properties. I am also not able to access the colorizer property with python scripting. I have attached the original colorizer proprty of the layer and the final property how it should look after changing updating classes and everything. 

Is there a way to do that using Python/arcpy or any other libraries from python?

Here is how i create the layer 

 

p=arcpy.md.MakeMultidimensionalRasterLayer(in_multidimensional_raster=netCDF_in, out_multidimensional_raster_layer=temperature,variables=["tmin"])

layer=p.getOutput(0)

k=arcpy.SaveToLayerFile_management(layer, "temperature.lyrx", "ABSOLUTE")

 

 

 

 

Here is the colorizer property of temperature.lyrx:

 

"colorizer" : {
        "type" : "CIMRasterStretchColorizer",
        "resamplingType" : "NearestNeighbor",
        "noDataColor" : {
          "type" : "CIMRGBColor",
          "values" : [
            255,
            255,
            255,
            0
          ]
        },
        "backgroundColor" : {
          "type" : "CIMRGBColor",
          "values" : [
            255,
            255,
            255,
            0
          ]
        },
        "colorRamp" : {
          "type" : "CIMMultipartColorRamp",
          "colorRamps" : [
            {
              "type" : "CIMLinearContinuousColorRamp",
              "colorSpace" : {
                "type" : "CIMICCColorSpace",
                "url" : "CIELAB"
              },
              "fromColor" : {
                "type" : "CIMLABColor",
                "values" : [
                  56.547017615341019,
                  76.899433471346256,
                  68.103444271380823,
                  100
                ]
              },
              "toColor" : {
                "type" : "CIMLABColor",
                "values" : [
                  96.683616491167115,
                  -20.419171018551619,
                  87.367929001284963,
                  100
                ]
              }
            },
            {
              "type" : "CIMLinearContinuousColorRamp",
              "colorSpace" : {
                "type" : "CIMICCColorSpace",
                "url" : "CIELAB"
              },
              "fromColor" : {
                "type" : "CIMLABColor",
                "values" : [
                  96.683616491167115,
                  -20.419171018551619,
                  87.367929001284963,
                  100
                ]
              },
              "toColor" : {
                "type" : "CIMLABColor",
                "values" : [
                  89.644590646826003,
                  -51.521697716782114,
                  -16.300660645455501,
                  100
                ]
              }
            },
            {
              "type" : "CIMLinearContinuousColorRamp",
              "colorSpace" : {
                "type" : "CIMICCColorSpace",
                "url" : "CIELAB"
              },
              "fromColor" : {
                "type" : "CIMLABColor",
                "values" : [
                  89.644590646826003,
                  -51.521697716782114,
                  -16.300660645455501,
                  100
                ]
              },
              "toColor" : {
                "type" : "CIMLABColor",
                "values" : [
                  34.668982832368499,
                  71.125560221348664,
                  -101.88789308244294,
                  100
                ]
              }
            }
          ],
          "weights" : [
            1,
            1,
            1
          ]
        },
        "colorScheme" : "Spectrum-Full Bright",
        "customStretchMax" : 1,
        "gammaValue" : 1,
        "hillshadeZFactor" : 1,
        "invert" : true,
        "maxPercent" : 2,
        "minPercent" : 2,
        "standardDeviationParam" : 2,
        "statsType" : "Dataset",
        "stretchClasses" : [
          {
            "type" : "CIMRasterStretchClass",
            "label" : "-59.0669",
            "value" : -59.066879272460938
          },
          {
            "type" : "CIMRasterStretchClass",
            "value" : -15.690719604492188
          },
          {
            "type" : "CIMRasterStretchClass",
            "label" : "27.6854",
            "value" : 27.685440063476563
          }
        ],
        "stretchStats" : {
          "type" : "StatsHistogram",
          "min" : -59.066879272460938,
          "max" : 27.685440063476563,
          "mean" : 0.58193869954082922,
          "stddev" : 21.252553064504141,
          "resolution" : 0.34020517386642157
        },
        "stretchType" : "MinimumMaximum"
      }

 

 

Here is the property of updated file which was created through ArcGIS Pro but i want such file to be created through python/arcpy:

 

 "colorizer" : {
        "type" : "CIMRasterClassifyColorizer",
        "resamplingType" : "NearestNeighbor",
        "noDataColor" : {
          "type" : "CIMRGBColor",
          "values" : [
            255,
            255,
            255,
            0
          ]
        },
        "normalizationType" : "Nothing",
        "classBreaks" : [
          {
            "type" : "CIMRasterClassBreak",
            "upperBound" : -36.273132623410618,
            "label" : "-59.0668 - -36.2731",
            "color" : {
              "type" : "CIMRGBColor",
              "values" : [
                165,
                0,
                38,
                100
              ]
            }
          },
          {
            "type" : "CIMRasterClassBreak",
            "upperBound" : -16.541232539158102,
            "label" : "-36.273 - -16.5412",
            "color" : {
              "type" : "CIMRGBColor",
              "values" : [
                248.5,
                141.5,
                82,
                100
              ]
            }
          },
          {
            "type" : "CIMRasterClassBreak",
            "upperBound" : -1.2319997151691424,
            "label" : "-16.5411 - -1.232",
            "color" : {
              "type" : "CIMRGBColor",
              "values" : [
                255,
                255,
                191,
                100
              ]
            }
          },
          {
            "type" : "CIMRasterClassBreak",
            "upperBound" : 13.737027934953399,
            "label" : "-1.23199 - 13.737",
            "color" : {
              "type" : "CIMRGBColor",
              "values" : [
                143.5,
                195,
                221,
                100
              ]
            }
          },
          {
            "type" : "CIMRasterClassBreak",
            "upperBound" : 27.685440063476563,
            "label" : "13.7371 - 27.6854",
            "color" : {
              "type" : "CIMRGBColor",
              "values" : [
                49,
                54,
                149,
                100
              ]
            }
          }
        ],
        "classificationMethod" : "NaturalBreaks",
        "colorRamp" : {
          "type" : "CIMMultipartColorRamp",
          "colorSpace" : {
            "type" : "CIMICCColorSpace",
            "url" : "Default RGB"
          },
          "colorRamps" : [
            {
              "type" : "CIMLinearContinuousColorRamp",
              "colorSpace" : {
                "type" : "CIMICCColorSpace",
                "url" : "Default RGB"
              },
              "fromColor" : {
                "type" : "CIMRGBColor",
                "colorSpace" : {
                  "type" : "CIMICCColorSpace",
                  "url" : "Default RGB"
                },
                "values" : [
                  165,
                  0,
                  38,
                  100
                ]
              },
              "toColor" : {
                "type" : "CIMRGBColor",
                "colorSpace" : {
                  "type" : "CIMICCColorSpace",
                  "url" : "Default RGB"
                },
                "values" : [
                  215,
                  48,
                  39,
                  100
                ]
              }
            },
            {
              "type" : "CIMLinearContinuousColorRamp",
              "colorSpace" : {
                "type" : "CIMICCColorSpace",
                "url" : "Default RGB"
              },
              "fromColor" : {
                "type" : "CIMRGBColor",
                "colorSpace" : {
                  "type" : "CIMICCColorSpace",
                  "url" : "Default RGB"
                },
                "values" : [
                  215,
                  48,
                  39,
                  100
                ]
              },
              "toColor" : {
                "type" : "CIMRGBColor",
                "colorSpace" : {
                  "type" : "CIMICCColorSpace",
                  "url" : "Default RGB"
                },
                "values" : [
                  244,
                  109,
                  67,
                  100
                ]
              }
            },
            {
              "type" : "CIMLinearContinuousColorRamp",
              "colorSpace" : {
                "type" : "CIMICCColorSpace",
                "url" : "Default RGB"
              },
              "fromColor" : {
                "type" : "CIMRGBColor",
                "colorSpace" : {
                  "type" : "CIMICCColorSpace",
                  "url" : "Default RGB"
                },
                "values" : [
                  244,
                  109,
                  67,
                  100
                ]
              },
              "toColor" : {
                "type" : "CIMRGBColor",
                "colorSpace" : {
                  "type" : "CIMICCColorSpace",
                  "url" : "Default RGB"
                },
                "values" : [
                  253,
                  174,
                  97,
                  100
                ]
              }
            },
            {
              "type" : "CIMLinearContinuousColorRamp",
              "colorSpace" : {
                "type" : "CIMICCColorSpace",
                "url" : "Default RGB"
              },
              "fromColor" : {
                "type" : "CIMRGBColor",
                "colorSpace" : {
                  "type" : "CIMICCColorSpace",
                  "url" : "Default RGB"
                },
                "values" : [
                  253,
                  174,
                  97,
                  100
                ]
              },
              "toColor" : {
                "type" : "CIMRGBColor",
                "colorSpace" : {
                  "type" : "CIMICCColorSpace",
                  "url" : "Default RGB"
                },
                "values" : [
                  254,
                  224,
                  144,
                  100
                ]
              }
            },
            {
              "type" : "CIMLinearContinuousColorRamp",
              "colorSpace" : {
                "type" : "CIMICCColorSpace",
                "url" : "Default RGB"
              },
              "fromColor" : {
                "type" : "CIMRGBColor",
                "colorSpace" : {
                  "type" : "CIMICCColorSpace",
                  "url" : "Default RGB"
                },
                "values" : [
                  254,
                  224,
                  144,
                  100
                ]
              },
              "toColor" : {
                "type" : "CIMRGBColor",
                "colorSpace" : {
                  "type" : "CIMICCColorSpace",
                  "url" : "Default RGB"
                },
                "values" : [
                  255,
                  255,
                  191,
                  100
                ]
              }
            },
            {
              "type" : "CIMLinearContinuousColorRamp",
              "colorSpace" : {
                "type" : "CIMICCColorSpace",
                "url" : "Default RGB"
              },
              "fromColor" : {
                "type" : "CIMRGBColor",
                "colorSpace" : {
                  "type" : "CIMICCColorSpace",
                  "url" : "Default RGB"
                },
                "values" : [
                  255,
                  255,
                  191,
                  100
                ]
              },
              "toColor" : {
                "type" : "CIMRGBColor",
                "colorSpace" : {
                  "type" : "CIMICCColorSpace",
                  "url" : "Default RGB"
                },
                "values" : [
                  224,
                  243,
                  248,
                  100
                ]
              }
            },
            {
              "type" : "CIMLinearContinuousColorRamp",
              "colorSpace" : {
                "type" : "CIMICCColorSpace",
                "url" : "Default RGB"
              },
              "fromColor" : {
                "type" : "CIMRGBColor",
                "colorSpace" : {
                  "type" : "CIMICCColorSpace",
                  "url" : "Default RGB"
                },
                "values" : [
                  224,
                  243,
                  248,
                  100
                ]
              },
              "toColor" : {
                "type" : "CIMRGBColor",
                "colorSpace" : {
                  "type" : "CIMICCColorSpace",
                  "url" : "Default RGB"
                },
                "values" : [
                  171,
                  217,
                  233,
                  100
                ]
              }
            },
            {
              "type" : "CIMLinearContinuousColorRamp",
              "colorSpace" : {
                "type" : "CIMICCColorSpace",
                "url" : "Default RGB"
              },
              "fromColor" : {
                "type" : "CIMRGBColor",
                "colorSpace" : {
                  "type" : "CIMICCColorSpace",
                  "url" : "Default RGB"
                },
                "values" : [
                  171,
                  217,
                  233,
                  100
                ]
              },
              "toColor" : {
                "type" : "CIMRGBColor",
                "colorSpace" : {
                  "type" : "CIMICCColorSpace",
                  "url" : "Default RGB"
                },
                "values" : [
                  116,
                  173,
                  209,
                  100
                ]
              }
            },
            {
              "type" : "CIMLinearContinuousColorRamp",
              "colorSpace" : {
                "type" : "CIMICCColorSpace",
                "url" : "Default RGB"
              },
              "fromColor" : {
                "type" : "CIMRGBColor",
                "colorSpace" : {
                  "type" : "CIMICCColorSpace",
                  "url" : "Default RGB"
                },
                "values" : [
                  116,
                  173,
                  209,
                  100
                ]
              },
              "toColor" : {
                "type" : "CIMRGBColor",
                "colorSpace" : {
                  "type" : "CIMICCColorSpace",
                  "url" : "Default RGB"
                },
                "values" : [
                  69,
                  117,
                  180,
                  100
                ]
              }
            },
            {
              "type" : "CIMLinearContinuousColorRamp",
              "colorSpace" : {
                "type" : "CIMICCColorSpace",
                "url" : "Default RGB"
              },
              "fromColor" : {
                "type" : "CIMRGBColor",
                "colorSpace" : {
                  "type" : "CIMICCColorSpace",
                  "url" : "Default RGB"
                },
                "values" : [
                  69,
                  117,
                  180,
                  100
                ]
              },
              "toColor" : {
                "type" : "CIMRGBColor",
                "colorSpace" : {
                  "type" : "CIMICCColorSpace",
                  "url" : "Default RGB"
                },
                "values" : [
                  49,
                  54,
                  149,
                  100
                ]
              }
            }
          ],
          "weights" : [
            0.10000000000000001,
            0.10000000000000001,
            0.10000000000000001,
            0.10000000000000001,
            0.10000000000000001,
            0.10000000000000001,
            0.10000000000000001,
            0.10000000000000001,
            0.10000000000000001,
            0.10000000000000001
          ]
        },
        "field" : "Value",
        "hillshadeZFactor" : 1,
        "minimumBreak" : -59.066879272460938,
        "showInAscendingOrder" : true,
        "numberFormat" : {
          "type" : "CIMNumericFormat",
          "alignmentOption" : "esriAlignLeft",
          "alignmentWidth" : 12,
          "roundingOption" : "esriRoundNumberOfSignificantDigits",
          "roundingValue" : 6,
          "useSeparator" : true
        }
      }

 

0 Kudos
7 Replies
MatthewMuehlhauser
New Contributor III

I would take a look at this page:

https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/symbology-class.htm

There's even a code snippet that seems close to what you're looking for:

import arcpy
import os
import sys

relpath = os.path.dirname(sys.argv[0])

p = arcpy.mp.ArcGISProject(relpath + r'\\RasterClassify.aprx')
m = p.listMaps('Map')[0]
l = m.listLayers('Raster1')[0]
sym = l.symbology

if hasattr(sym, 'colorizer'):
    if sym.colorizer.type == 'RasterClassifyColorizer':
        sym.colorizer.classificationField = 'Value'
        sym.colorizer.breakCount = 7
        sym.colorizer.colorRamp = p.listColorRamps('Cyan to Purple')[0]
        l.symbology = sym

p.saveACopy(relpath + r'\\SavedOutput.aprx')

 

0 Kudos
RehanChaudhary
Occasional Contributor

@MatthewMuehlhauser i am trying to implement a workflow that works without project files (.aprx) or without using ArcGIS Pro. I have come across this function of   Add ColorMap    which i think could assign symbology but i am not sure how to create the clr file through python. do you have an idea for it?

0 Kudos
RehanChaudhary
Occasional Contributor

also when i run 

p = arcpy.md.MakeMultidimensionalRasterLayer(in_multidimensional_raster=netCDF_in, out_multidimensional_raster_layer=temperature,variables=["tmin"])
 print(p.symbology)

 i get an error : AttributeError: 'Result' object has no attribute 'symbology'

0 Kudos
MatthewMuehlhauser
New Contributor III

So, rasters don't have a symbology property which would be why you get that error. However, they do have a getColormap({variable_name}) method that you could possibly use to get the current Color map and then you might be able to re-map it from there? If you have access to Pro, it might be easiest to just change the symbology to the Yellow-Orange-Brown in a project and then use the getColormap to generate the .clr file that you could use in your code.  Depending on your data though and what the clr file looks like, you might need to account for values that weren't present in your .clr file when it's used on other data since they'll be assumed to be 0 0 0 otherwise.

https://pro.arcgis.com/en/pro-app/latest/arcpy/classes/raster-object.htm

https://desktop.arcgis.com/en/arcmap/10.3/manage-data/raster-and-images/creating-a-color-map-clr-fil...

0 Kudos
RehanChaudhary
Occasional Contributor

@MatthewMuehlhauser so i was able to find the symbology property once i saved the layer correctly. i was having issues with saving the layer.  However currently its using the "Stretch" Colorizer. how can i change it to "ClassifyColorizer" and something similar to the snippet that you have pasted it above? I have pasted the "Colorizer" property  below from the .lyrx file

 "colorizer" : {
        "type" : "CIMRasterStretchColorizer",
        "resamplingType" : "NearestNeighbor",
        "noDataColor" : {
          "type" : "CIMRGBColor",
          "values" : [
            255,
            255,
            255,
            0
          ]
        },
        "backgroundColor" : {
          "type" : "CIMRGBColor",
          "values" : [
            255,
            255,
            255,
            0
          ]
        },
        "colorRamp" : {
          "type" : "CIMMultipartColorRamp",
          "colorRamps" : [
            {
              "type" : "CIMLinearContinuousColorRamp",
              "colorSpace" : {
                "type" : "CIMICCColorSpace",
                "url" : "CIELAB"
              },
              "fromColor" : {
                "type" : "CIMLABColor",
                "values" : [
                  56.547017615341019,
                  76.899433471346256,
                  68.103444271380823,
                  100
                ]
              },
              "toColor" : {
                "type" : "CIMLABColor",
                "values" : [
                  96.683616491167115,
                  -20.419171018551619,
                  87.367929001284963,
                  100
                ]
              }
            },
            {
              "type" : "CIMLinearContinuousColorRamp",
              "colorSpace" : {
                "type" : "CIMICCColorSpace",
                "url" : "CIELAB"
              },
              "fromColor" : {
                "type" : "CIMLABColor",
                "values" : [
                  96.683616491167115,
                  -20.419171018551619,
                  87.367929001284963,
                  100
                ]
              },
              "toColor" : {
                "type" : "CIMLABColor",
                "values" : [
                  89.644590646826003,
                  -51.521697716782114,
                  -16.300660645455501,
                  100
                ]
              }
            },
            {
              "type" : "CIMLinearContinuousColorRamp",
              "colorSpace" : {
                "type" : "CIMICCColorSpace",
                "url" : "CIELAB"
              },
              "fromColor" : {
                "type" : "CIMLABColor",
                "values" : [
                  89.644590646826003,
                  -51.521697716782114,
                  -16.300660645455501,
                  100
                ]
              },
              "toColor" : {
                "type" : "CIMLABColor",
                "values" : [
                  34.668982832368499,
                  71.125560221348664,
                  -101.88789308244294,
                  100
                ]
              }
            }
          ],
          "weights" : [
            1,
            1,
            1
          ]
        },
        "colorScheme" : "Spectrum-Full Bright",
        "customStretchMax" : 1,
        "gammaValue" : 1,
        "hillshadeZFactor" : 1,
        "invert" : true,
        "maxPercent" : 2,
        "minPercent" : 2,
        "standardDeviationParam" : 2,
        "statsType" : "Dataset",
        "stretchClasses" : [
          {
            "type" : "CIMRasterStretchClass",
            "label" : "-59.0669",
            "value" : -59.066879272460938
          },
          {
            "type" : "CIMRasterStretchClass",
            "value" : -15.690719604492188
          },
          {
            "type" : "CIMRasterStretchClass",
            "label" : "27.6854",
            "value" : 27.685440063476563
          }
        ],
        "stretchStats" : {
          "type" : "StatsHistogram",
          "min" : -59.066879272460938,
          "max" : 27.685440063476563,
          "mean" : 0.58193869954082922,
          "stddev" : 21.252553064504141,
          "resolution" : 0.34020517386642157
        },
        "stretchType" : "MinimumMaximum"
      }
0 Kudos
RehanChaudhary
Occasional Contributor

@MatthewMuehlhauser i made changes to the question. maybe it might help you understand what i want to achieve and help me in how can i achieve it 🙂

0 Kudos
MatthewMuehlhauser
New Contributor III

I feel like in the Update Colorizer link, example 2 would do what you need. But if you have to do it without a Pro project, I'm not sure how that would work. 

p = arcpy.mp.ArcGISProject(relpath + r'\\RasterClassify.aprx')
m = p.listMaps('Map')[0]
l = m.listLayers('Raster1')[0]
sym = l.symbology   #<--- this is what it seems you have

sym.updateColorizer("RasterClassifyColorizer")
sym.colorizer.classificationField = 'Value' # or your temperature field
sym.colorizer.breakCount = 5  # or however many classes you want
sym.colorizer.colorRamp = p.listColorRamps('Yellow-Orange-Brown')[0] # this step requires a pro project, so not sure how to get past that
l.symbology = sym

If you were going to apply a symbology with the clr file, you could just save that second one as a .clr file and reference it to apply the symbology. The only thing that would need to be updated I believe would be the class breaks array where you would just update the upper bounds and the labels if the upper and lower bounds aren't the same in your new feature. Although, it would be nice to have the breaks based on the distributions of the data, which could potentially be missed unless you build in that logic as well to determine the appropriate breaks.

0 Kudos