<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: UniqueValueRenderer: Grouping multiple values into single symbol in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/uniquevaluerenderer-grouping-multiple-values-into/m-p/1575909#M73571</link>
    <description>&lt;P&gt;See my post for the "removeAllValues" implementation, you have to supply that call with a valid mapping of existing group/value pairs.&lt;/P&gt;&lt;P&gt;There's also the odd thing with the updateRenderer function call for a Symbology object having a pseudo private/internal function header:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="HaydenWelch_0-1736963125356.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/123414i5CD011641EE4B01E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="HaydenWelch_0-1736963125356.png" alt="HaydenWelch_0-1736963125356.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Which is odd as I think that's a function that a user should call, and not one that is only used internally by a Symbology object. It's also wrapped by maskargs which is a nearly inscrutable decorator function that apparently does some magic with the function argument names?&lt;/P&gt;</description>
    <pubDate>Wed, 15 Jan 2025 17:50:14 GMT</pubDate>
    <dc:creator>HaydenWelch</dc:creator>
    <dc:date>2025-01-15T17:50:14Z</dc:date>
    <item>
      <title>UniqueValueRenderer: Grouping multiple values into single symbol</title>
      <link>https://community.esri.com/t5/python-questions/uniquevaluerenderer-grouping-multiple-values-into/m-p/1575604#M73559</link>
      <description>&lt;P&gt;I am trying to write code that symbolizes a feature in a UniqueValueRenderer that draws upon two fields: Status &amp;amp; tgt_ret. I want to have certain value combinations of these fields to by grouped into the same symbol/label. When intially 6 unique symbols are created for the 6 unique value combinations (Status, tgt_ret): [CLOSED, HE], [CLOSED, No Drop], [CLOSED, Practice], [OPEN, HE], [OPEN, No Drop], [OPEN Practice]&lt;BR /&gt;&lt;BR /&gt;I only need three symbols formatted as follows:&amp;nbsp;&lt;BR /&gt;[CLOSED, HE] with the label HE Closed&lt;/P&gt;&lt;P&gt;[[CLOSED, No Drop], [CLOSED, Practice]] with the label Closed&lt;/P&gt;&lt;P&gt;[[OPEN, HE], [OPEN, No Drop], [OPEN, Practice]] with the label Open&lt;BR /&gt;&lt;BR /&gt;Does anyone have any idea on how to combine the values? I am having a hard time deciphering the the example code within the documentation for uniquevaluerenderer, Groups, and GroupItems. This is how I have it set up, but have not made much meaningful progress after this.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Target_layer= map_view.listLayers("Targets")[0]&lt;BR /&gt;arcpy.AddMessage("Target Layer Found")&lt;BR /&gt;Target_Sym= Target_layer.symbology&lt;BR /&gt;arcpy.AddMessage("Symbology Accessed")&lt;BR /&gt;Target_Sym.updateRenderer('UniqueValueRenderer')&lt;BR /&gt;arcpy.AddMessage("Update Renderer")&lt;BR /&gt;Target_Sym.renderer.fields= ['Status', 'tgt_ret']&lt;BR /&gt;arcpy.AddMessage("Renderer Fields Set")&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jan 2025 21:11:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/uniquevaluerenderer-grouping-multiple-values-into/m-p/1575604#M73559</guid>
      <dc:creator>CodyDeVries1</dc:creator>
      <dc:date>2025-01-14T21:11:01Z</dc:date>
    </item>
    <item>
      <title>Re: UniqueValueRenderer: Grouping multiple values into single symbol</title>
      <link>https://community.esri.com/t5/python-questions/uniquevaluerenderer-grouping-multiple-values-into/m-p/1575623#M73560</link>
      <description>&lt;P&gt;Here some code, I haven't tested it but it should lead to right direction, I think.&lt;/P&gt;&lt;LI-CODE lang="c"&gt;import arcpy

# Get the target layer
Target_layer = map_view.listLayers("Targets")[0]
arcpy.AddMessage("Target Layer Found")

# Access the symbology
Target_Sym = Target_layer.symbology
arcpy.AddMessage("Symbology Accessed")

# Update the renderer to UniqueValueRenderer
Target_Sym.updateRenderer('UniqueValueRenderer')
arcpy.AddMessage("Update Renderer")

# Set the renderer fields
Target_Sym.renderer.fields = ['Status', 'tgt_ret']
arcpy.AddMessage("Renderer Fields Set")

# Clear existing values
Target_Sym.renderer.removeAllValues()

# Define the unique value combinations and their labels
value_combinations = {
    ("CLOSED", "HE"): "HE Closed",
    ("CLOSED", "No Drop"): "Closed",
    ("CLOSED", "Practice"): "Closed",
    ("OPEN", "HE"): "Open",
    ("OPEN", "No Drop"): "Open",
    ("OPEN", "Practice"): "Open"
}

# Add the unique value combinations to the renderer
for (status, tgt_ret), label in value_combinations.items():
    value_string = f"{status}, {tgt_ret}"  # Format as expected by the renderer
    Target_Sym.renderer.addValue(value_string)  # Add the value
    Target_Sym.renderer.updateValue(value_string, {'label': label})  # Update the label

# Apply the symbology changes to the layer
Target_layer.symbology = Target_Sym
arcpy.AddMessage("Symbology Applied")&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 14 Jan 2025 21:42:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/uniquevaluerenderer-grouping-multiple-values-into/m-p/1575623#M73560</guid>
      <dc:creator>TonyAlmeida</dc:creator>
      <dc:date>2025-01-14T21:42:11Z</dc:date>
    </item>
    <item>
      <title>Re: UniqueValueRenderer: Grouping multiple values into single symbol</title>
      <link>https://community.esri.com/t5/python-questions/uniquevaluerenderer-grouping-multiple-values-into/m-p/1575807#M73566</link>
      <description>&lt;P&gt;This looks like something that ChatGPT wrote up for me. The problem that I have with this are the functions that it calls up don't exist. For example I run into the the error: UniqueValueRenderer object has no attribute 'removeAllValues'&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jan 2025 15:13:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/uniquevaluerenderer-grouping-multiple-values-into/m-p/1575807#M73566</guid>
      <dc:creator>CodyDeVries1</dc:creator>
      <dc:date>2025-01-15T15:13:34Z</dc:date>
    </item>
    <item>
      <title>Re: UniqueValueRenderer: Grouping multiple values into single symbol</title>
      <link>https://community.esri.com/t5/python-questions/uniquevaluerenderer-grouping-multiple-values-into/m-p/1575865#M73567</link>
      <description>&lt;P&gt;have you tried creating a .lyrx and use arcpy.management.ApplySymbologyFromLayer to update?&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jan 2025 16:53:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/uniquevaluerenderer-grouping-multiple-values-into/m-p/1575865#M73567</guid>
      <dc:creator>TonyAlmeida</dc:creator>
      <dc:date>2025-01-15T16:53:29Z</dc:date>
    </item>
    <item>
      <title>Re: UniqueValueRenderer: Grouping multiple values into single symbol</title>
      <link>https://community.esri.com/t5/python-questions/uniquevaluerenderer-grouping-multiple-values-into/m-p/1575881#M73568</link>
      <description>&lt;P&gt;It should be "removeValues"&amp;nbsp;&lt;A href="https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/uniquevaluerenderer-class.htm" target="_blank"&gt;UniqueValueRenderer—ArcGIS Pro | Documentation&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jan 2025 17:17:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/uniquevaluerenderer-grouping-multiple-values-into/m-p/1575881#M73568</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2025-01-15T17:17:50Z</dc:date>
    </item>
    <item>
      <title>Re: UniqueValueRenderer: Grouping multiple values into single symbol</title>
      <link>https://community.esri.com/t5/python-questions/uniquevaluerenderer-grouping-multiple-values-into/m-p/1575899#M73569</link>
      <description>&lt;P&gt;Yeah, ChatGPT is really bad for arcpy scripting and I would absolutely avoid it unless you know how to validate what it spits out. Lots of hallucinations because there isn't much public arcpy code on GitHub.&lt;/P&gt;&lt;P&gt;This is also a really complex workflow, As shown here:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from typing import Literal, TypeAlias, Union

from arcpy.mp import ArcGISProject
from arcpy._renderer import Base_renderer, UniqueValueRenderer, SimpleRenderer, Graduated_colors_renderer, Unclassed_colors_renderer, Graduated_symbols_renderer
from arcpy._symbology import Symbology, ItemGroup, Item
from arcpy._mp import Layer, Map

RendererName: TypeAlias = Literal[
    'GraduatedColorsRenderer', 
    'GraduatedSymbolsRenderer',
    'UnclassedColorsRenderer',
    'SimpleRenderer',
    'UniqueValueRenderer']

RendererType: TypeAlias = Union[
    Base_renderer, 
    UniqueValueRenderer,
    SimpleRenderer,
    Graduated_colors_renderer,
    Unclassed_colors_renderer,
    Graduated_symbols_renderer]

def get_symbology(layer: Layer) -&amp;gt; Symbology:
    return layer.symbology

def get_renderer(symbology: Symbology) -&amp;gt; RendererType:
    return symbology.renderer

def set_renderer(symbology: Symbology, renderer: RendererName) -&amp;gt; RendererType:
    symbology._updateRenderer(renderer)
    return get_renderer(symbology)

def get_unique_value_renderer_values(renderer: UniqueValueRenderer) -&amp;gt; dict[str, list[str]]:
    values: dict[str, list[str]] = {}
    groups: list[ItemGroup] = renderer.groups
    for group in groups:
        items: list[Item] = group.items
        values[group.heading] = [item.label for item in items]
    return values

def remove_all_values(renderer: UniqueValueRenderer):
    renderer.removeValues(get_unique_value_renderer_values(renderer))

def add_values(renderer: UniqueValueRenderer, values: dict[str, list[str]]) -&amp;gt; dict[str, list[str]]:
    return renderer.addValues(values) or values

def main():
    project = ArcGISProject(r"Path\To\Project")
    _map: Map = project.listMaps()[0]
    layer: Layer = _map.listLayers()[0]
    
    symbology = get_symbology(layer)
    renderer: UniqueValueRenderer = set_renderer(symbology, 'UniqueValueRenderer')
    remove_all_values(renderer)
    new_values = {}
    
    add_values(renderer, new_values)
    
    project.save()
    return

if __name__ == "__main__":
    main()&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The arcpy code that we're using here is meant to be pseudo private, so I'm not sure how stable it will be, but it seems to at least update things. If this still doesn't work, you'll probably have to start digging into CIM definitions.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jan 2025 17:34:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/uniquevaluerenderer-grouping-multiple-values-into/m-p/1575899#M73569</guid>
      <dc:creator>HaydenWelch</dc:creator>
      <dc:date>2025-01-15T17:34:40Z</dc:date>
    </item>
    <item>
      <title>Re: UniqueValueRenderer: Grouping multiple values into single symbol</title>
      <link>https://community.esri.com/t5/python-questions/uniquevaluerenderer-grouping-multiple-values-into/m-p/1575909#M73571</link>
      <description>&lt;P&gt;See my post for the "removeAllValues" implementation, you have to supply that call with a valid mapping of existing group/value pairs.&lt;/P&gt;&lt;P&gt;There's also the odd thing with the updateRenderer function call for a Symbology object having a pseudo private/internal function header:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="HaydenWelch_0-1736963125356.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/123414i5CD011641EE4B01E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="HaydenWelch_0-1736963125356.png" alt="HaydenWelch_0-1736963125356.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Which is odd as I think that's a function that a user should call, and not one that is only used internally by a Symbology object. It's also wrapped by maskargs which is a nearly inscrutable decorator function that apparently does some magic with the function argument names?&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jan 2025 17:50:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/uniquevaluerenderer-grouping-multiple-values-into/m-p/1575909#M73571</guid>
      <dc:creator>HaydenWelch</dc:creator>
      <dc:date>2025-01-15T17:50:14Z</dc:date>
    </item>
    <item>
      <title>Re: UniqueValueRenderer: Grouping multiple values into single symbol</title>
      <link>https://community.esri.com/t5/python-questions/uniquevaluerenderer-grouping-multiple-values-into/m-p/1575912#M73572</link>
      <description>&lt;P&gt;Logically everything you wrote out makes sense. I just don't understand why the simple addValues doesn't work. Here is how far I got. I will start digging into what you sent me to see if it works, but any insight into this? It doesn't result in an error but it doesn't result in the new symbol being made.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;##Creates a list of layers that have "Buff" in their name&lt;BR /&gt;Target_layer= map_view.listLayers("Targets")[0]&lt;BR /&gt;arcpy.AddMessage("Target Layer Found")&lt;BR /&gt;Target_Sym= Target_layer.symbology&lt;BR /&gt;arcpy.AddMessage("Symbology Accessed")&lt;BR /&gt;Target_Sym.updateRenderer('UniqueValueRenderer')&lt;BR /&gt;arcpy.AddMessage("Update Renderer")&lt;BR /&gt;Target_Sym.renderer.fields= ['Status', 'tgt_ret']&lt;BR /&gt;arcpy.AddMessage("Renderer Fields Set")&lt;/P&gt;&lt;P&gt;StatusGroup= Target_Sym.renderer.groups[0]&lt;BR /&gt;StatusGroup.heading="Target Status"&lt;/P&gt;&lt;P&gt;Target_Sym.renderer.removeValues({"Target Status": ["CLOSED, Practice"]})&lt;BR /&gt;Target_Sym.renderer.removeValues({"Target Status": ["CLOSED, No Drop"]})&lt;BR /&gt;Target_Sym.renderer.removeValues({"Target Status": ["OPEN, No Drop"]})&lt;BR /&gt;Target_Sym.renderer.removeValues({"Target Status": ["OPEN, Practice"]})&lt;BR /&gt;Target_Sym.renderer.removeValues({"Target Status": ["OPEN, HE"]})&lt;BR /&gt;Target_layer.symbology = Target_Sym&lt;/P&gt;&lt;P&gt;string_pairs= [['OPEN', 'HE'],['OPEN', 'No Drop'],['OPEN', 'Practice']]&lt;BR /&gt;sliced_pairs = string_pairs[:3]&lt;BR /&gt;arcpy.AddMessage(sliced_pairs)&lt;/P&gt;&lt;P&gt;# Add the new values to the renderer&lt;BR /&gt;Target_Sym.renderer.addValues({"Target Status": sliced_pairs})&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Target_layer.symbology = Target_Sym&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jan 2025 18:06:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/uniquevaluerenderer-grouping-multiple-values-into/m-p/1575912#M73572</guid>
      <dc:creator>CodyDeVries1</dc:creator>
      <dc:date>2025-01-15T18:06:42Z</dc:date>
    </item>
    <item>
      <title>Re: UniqueValueRenderer: Grouping multiple values into single symbol</title>
      <link>https://community.esri.com/t5/python-questions/uniquevaluerenderer-grouping-multiple-values-into/m-p/1575914#M73573</link>
      <description>&lt;P&gt;I have done this in the past but I am trying to go a different route for simplicity for the people I am making the tool for.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jan 2025 18:10:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/uniquevaluerenderer-grouping-multiple-values-into/m-p/1575914#M73573</guid>
      <dc:creator>CodyDeVries1</dc:creator>
      <dc:date>2025-01-15T18:10:30Z</dc:date>
    </item>
    <item>
      <title>Re: UniqueValueRenderer: Grouping multiple values into single symbol</title>
      <link>https://community.esri.com/t5/python-questions/uniquevaluerenderer-grouping-multiple-values-into/m-p/1575917#M73574</link>
      <description>&lt;P&gt;Look at the type hint for my add_values function&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def add_values(..., values: dict[str, list[str]]) -&amp;gt; ... : ...&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You are currenty passing the addValues function&lt;/P&gt;&lt;LI-CODE lang="python"&gt;dict[str, list[list[str, ...]]]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Which is an incorrect format.&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jan 2025 18:20:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/uniquevaluerenderer-grouping-multiple-values-into/m-p/1575917#M73574</guid>
      <dc:creator>HaydenWelch</dc:creator>
      <dc:date>2025-01-15T18:20:31Z</dc:date>
    </item>
  </channel>
</rss>

