1

How do I SET an ArcMap layer's symbology type using python?

Layer()SymbologyType property is read only and UpdateLayer() requires a pre-existing layer source to copy symbology from. Copying from another source doesn't work well because the properties to set are dynamic. For example derive break values for classified raster from the input source and an algorithm.

All I want to do is assign the type, e.g.

lyr.symbologyType = "RASTER_CLASSIFIED"
#or 
lyr.update(symbologyType="UNIQUE_VALUES")
1
  • 3
    I think apply symbology from layer is the only way to do this. You'll have to have one for classified raster and another for unique values, and so on. – Fezter Oct 25 '17 at 22:01
1

If you are on ArcGIS 10.2+, try creating a layer, then playing around with the

layer._arc_object.renderer

I have had some luck manipulating that before. Beware, you can destroy your mxd with this, and is likely the reason it isn't officially documented. It is get and settable though, so look for common patterns then either use regular expression or string replaces to do what you need.

EDIT - This is undocumented, and I have corrupted mxds several times in using the _arc_object property (there is a lot of undocumented stuff in there). Given that, I don't know exactly what does what and it takes some playing around with to get what you want. I have posted what a renderer looks like for a 8 bit integer stored raster, with 5 unique classes. I have not modified besides removing some of the int tags so it all fits. I suggest making a copy of the mxd in question. In addition, it depends on the symbol and symbology type as to how you update it. Sometimes you can't as there is a bytestring which I don't know how to decode. Make a copy of the mxd, print out the original renderer, modify it in notepad or something as required, then set the value back. Refresh to see if it worked. The TOC may not update until you force the TOC to refresh or restart the map.

<Renderer xsi:type="typens:RasterClassifyRenderer" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:typens="http://www.esri.com/schemas/ArcGIS/10.3">
    <Indexed>false</Indexed>
    <Brightness>0</Brightness>
    <Contrast>0</Contrast>
    <ResamplingType>RSP_CubicConvolution</ResamplingType>
    <NoDataColor xsi:type="typens:RgbColor">
        <UseWindowsDithering>false</UseWindowsDithering>
        <AlphaValue>0</AlphaValue>
        <Red>255</Red>
        <Green>255</Green>
        <Blue>255</Blue>
    </NoDataColor>
    <NoDataValue xsi:type="typens:ArrayOfDouble">
        <Double>2.2250738585072014e-308</Double>
        <Double>2.2250738585072014e-308</Double>
        <Double>2.2250738585072014e-308</Double>
    </NoDataValue>
    <AlphaBandIndex>-1</AlphaBandIndex>
    <UseAlphaBand>false</UseAlphaBand>
    <ClassField>&lt;VALUE&gt;</ClassField>
    <NormField>&lt;None&gt;</NormField>
    <ClassificationComponent>true</ClassificationComponent>
    <Guid>{62144BEA-E05E-11D1-AAAE-00C04FA334B3}</Guid>
    <ColorSchema>Black to White</ColorSchema>
    <LegendGroupsCount>1</LegendGroupsCount>
    <LegendGroups xsi:type="typens:ArrayOfLegendGroup">
        <LegendGroup xsi:type="typens:LegendGroup">
            <Visible>true</Visible>
            <Editable>true</Editable>
            <Heading>&lt;VALUE&gt;</Heading>
            <LegendClasses xsi:type="typens:ArrayOfLegendClass">
                <LegendClass xsi:type="typens:LegendClass">
                    <Symbol xsi:type="typens:SimpleFillSymbol">
                        <Color xsi:type="typens:HsvColor">
                            <UseWindowsDithering>false</UseWindowsDithering>
                            <AlphaValue>255</AlphaValue>
                            <Hue>-1</Hue>
                            <Saturation>0</Saturation>
                            <Value>0</Value>
                        </Color>
                        <Outline xsi:type="typens:SimpleLineSymbol">
                            <Color xsi:type="typens:RgbColor">
                                <UseWindowsDithering>true</UseWindowsDithering>
                                <AlphaValue>255</AlphaValue>
                                <Red>110</Red>
                                <Green>110</Green>
                                <Blue>110</Blue>
                            </Color>
                            <Style>esriSLSSolid</Style>
                            <Width>1</Width>
                        </Outline>
                        <Style>esriSFSSolid</Style>
                    </Symbol>
                    <Label>0 - 32</Label>
                    <Description></Description>
                </LegendClass>
                <LegendClass xsi:type="typens:LegendClass">
                    <Symbol xsi:type="typens:SimpleFillSymbol">
                        <Color xsi:type="typens:HsvColor">
                            <UseWindowsDithering>false</UseWindowsDithering>
                            <AlphaValue>255</AlphaValue>
                            <Hue>-2</Hue>
                            <Saturation>0</Saturation>
                            <Value>25</Value>
                        </Color>
                        <Outline xsi:type="typens:SimpleLineSymbol">
                            <Color xsi:type="typens:RgbColor">
                                <UseWindowsDithering>true</UseWindowsDithering>
                                <AlphaValue>255</AlphaValue>
                                <Red>110</Red>
                                <Green>110</Green>
                                <Blue>110</Blue>
                            </Color>
                            <Style>esriSLSSolid</Style>
                            <Width>1</Width>
                        </Outline>
                        <Style>esriSFSSolid</Style>
                    </Symbol>
                    <Label>32.00000001 - 97</Label>
                    <Description></Description>
                </LegendClass>
                <LegendClass xsi:type="typens:LegendClass">
                    <Symbol xsi:type="typens:SimpleFillSymbol">
                        <Color xsi:type="typens:HsvColor">
                            <UseWindowsDithering>false</UseWindowsDithering>
                            <AlphaValue>255</AlphaValue>
                            <Hue>-2</Hue>
                            <Saturation>0</Saturation>
                            <Value>50</Value>
                        </Color>
                        <Outline xsi:type="typens:SimpleLineSymbol">
                            <Color xsi:type="typens:RgbColor">
                                <UseWindowsDithering>true</UseWindowsDithering>
                                <AlphaValue>255</AlphaValue>
                                <Red>110</Red>
                                <Green>110</Green>
                                <Blue>110</Blue>
                            </Color>
                            <Style>esriSLSSolid</Style>
                            <Width>1</Width>
                        </Outline>
                        <Style>esriSFSSolid</Style>
                    </Symbol>
                    <Label>97.00000001 - 175</Label>
                    <Description></Description>
                </LegendClass>
                <LegendClass xsi:type="typens:LegendClass">
                    <Symbol xsi:type="typens:SimpleFillSymbol">
                        <Color xsi:type="typens:HsvColor">
                            <UseWindowsDithering>false</UseWindowsDithering>
                            <AlphaValue>255</AlphaValue>
                            <Hue>-2</Hue>
                            <Saturation>0</Saturation>
                            <Value>75</Value>
                        </Color>
                        <Outline xsi:type="typens:SimpleLineSymbol">
                            <Color xsi:type="typens:RgbColor">
                                <UseWindowsDithering>true</UseWindowsDithering>
                                <AlphaValue>255</AlphaValue>
                                <Red>110</Red>
                                <Green>110</Green>
                                <Blue>110</Blue>
                            </Color>
                            <Style>esriSLSSolid</Style>
                            <Width>1</Width>
                        </Outline>
                        <Style>esriSFSSolid</Style>
                    </Symbol>
                    <Label>175.0000001 - 236</Label>
                    <Description></Description>
                </LegendClass>
                <LegendClass xsi:type="typens:LegendClass">
                    <Symbol xsi:type="typens:SimpleFillSymbol">
                        <Color xsi:type="typens:HsvColor">
                            <UseWindowsDithering>false</UseWindowsDithering>
                            <AlphaValue>255</AlphaValue>
                            <Hue>-1</Hue>
                            <Saturation>0</Saturation>
                            <Value>100</Value>
                        </Color>
                        <Outline xsi:type="typens:SimpleLineSymbol">
                            <Color xsi:type="typens:RgbColor">
                                <UseWindowsDithering>true</UseWindowsDithering>
                                <AlphaValue>255</AlphaValue>
                                <Red>110</Red>
                                <Green>110</Green>
                                <Blue>110</Blue>
                            </Color>
                            <Style>esriSLSSolid</Style>
                            <Width>1</Width>
                        </Outline>
                        <Style>esriSFSSolid</Style>
                    </Symbol>
                    <Label>236.0000001 - 255</Label>
                    <Description></Description>
                </LegendClass>
            </LegendClasses>
        </LegendGroup>
    </LegendGroups>
    <BreakSize>6</BreakSize>
    <ArrayOfBreak xsi:type="typens:ArrayOfDouble">
        <Double>0</Double>
        <Double>32</Double>
        <Double>97</Double>
        <Double>175</Double>
        <Double>236</Double>
        <Double>255</Double>
    </ArrayOfBreak>
    <Ascending>true</Ascending>
    <NumberFormat xsi:type="typens:NumericFormat">
        <RoundingOption>esriRoundNumberOfSignificantDigits</RoundingOption>
        <AlignmentOption>esriAlignLeft</AlignmentOption>
        <RoundingValue>10</RoundingValue>
        <AlignmentWidth>12</AlignmentWidth>
        <UseSeparator>true</UseSeparator>
        <ZeroPad>false</ZeroPad>
        <ShowPlus>false</ShowPlus>
    </NumberFormat>
    <ShowClassGaps>false</ShowClassGaps>
    <DeviationInterval>0</DeviationInterval>
    <ExclusionShowClass>false</ExclusionShowClass>
    <ExclusionLegendClass xsi:type="typens:LegendClass">
        <Symbol xsi:type="typens:SimpleFillSymbol">
            <Color xsi:type="typens:RgbColor">
                <UseWindowsDithering>true</UseWindowsDithering>
                <AlphaValue>0</AlphaValue>
                <Red>0</Red>
                <Green>0</Green>
                <Blue>0</Blue>
            </Color>
            <Outline xsi:type="typens:SimpleLineSymbol">
                <Color xsi:type="typens:RgbColor">
                    <UseWindowsDithering>true</UseWindowsDithering>
                    <AlphaValue>255</AlphaValue>
                    <Red>110</Red>
                    <Green>110</Green>
                    <Blue>110</Blue>
                </Color>
                <Style>esriSLSSolid</Style>
                <Width>1</Width>
            </Outline>
            <Style>esriSFSSolid</Style>
        </Symbol>
        <Label></Label>
        <Description></Description>
    </ExclusionLegendClass>
    <UniqueValues xsi:type="typens:RasterUniqueValues">
        <UniqueValuesSize>256</UniqueValuesSize>
        <Values xsi:type="typens:ArrayOfValue">
            <Value xsi:type="xs:double">0</Value>
            <Value xsi:type="xs:double">1</Value>
            <Value xsi:type="xs:double">2</Value>
            <Value xsi:type="xs:double">3</Value>
            <Value xsi:type="xs:double">4</Value>
            <Value xsi:type="xs:double">5</Value>
            <Value xsi:type="xs:double">6</Value>
            <Value xsi:type="xs:double">7</Value>
            <Value xsi:type="xs:double">8</Value>
            ... I erased these values so it fits in the box, but it goes to 
            255
            <Value xsi:type="xs:double">255</Value>
        </Values>
        <Counts xsi:type="typens:ArrayOfInt">
            <Int>69499</Int>
            ... There were 255 total int elements here, but erased to fit.
            <Int>8664536</Int>
        </Counts>
    </UniqueValues>
    <UseHillShader>false</UseHillShader>
    <ZScale>1</ZScale>
    <SymbolFlipped>false</SymbolFlipped>
</Renderer>

The python to get is this (returns xml formatted string):

lyr = arcpy.ListLayers(mxd)[0]
renderer = lyr._arc_object.renderer

The python to set is this (set with xml formatted string):

lyr._arc_object.renderer = modified_renderer
9
  • Can you give more info? "Play around with the..." doesn't fill me with a lot of confidence. Please edit your answer to include an example of how you'd do it, and of the result. Please also expand on the "can destroy your mxd" – Midavalo Oct 26 '17 at 2:57
  • I updated my answer, but you shouldn't feel confident updating this, its undocumented. This should be a last resort, but since ESRI doesn't have any better options, I have ended up using this several times. It takes "playing around with". – isamson Oct 26 '17 at 14:21
  • There appears to be only xml and no python here? – Midavalo Oct 26 '17 at 14:22
  • Updated the answer to have python. I had already included the property at the top, but specified how to get and set. The renderer returns an xml string. – isamson Oct 26 '17 at 14:26
  • Thanks for adding the python initialization. I had to insert .mapping. (arcgis 10.5.1): [gist.github.com/maphew/… example). Does this mean Accessing ArcObjects from Python? isn't needed anymore?? – matt wilkie Oct 26 '17 at 22:34

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.