Skip To Content ArcGIS for Developers Sign In Dashboard

Overview

You will learn: how to build an add-in which can assign a renderer to your feature layer to change feature symbology.

With the ArcGIS Pro SDK for .NET, you can extend ArcGIS Pro with your own unique tools and workflows. Using Microsoft Visual Studio and the Pro SDK, you can build Pro add-ins and configurations that provide custom functionality, such as the ability to interact with feature layers and their symbology. You can update layer symbology based upon an existing system style or a custom style that is loaded within your project.

This tutorial will walk you through the creation of an add-in with two buttons that update the symbology of a point feature layer with either a new symbol or graduated colors.

Steps

Create a new ArcGIS Pro Add-In solution

  1. Start Visual Studio.

  2. Choose File > New > Project and then from the ArcGIS templates group, select ArcGIS Pro Module Add-in. Name the add-in project "LayerSymbology". Click OK to proceed.

Create new Pro buttons and update DAML

  1. Right click the project and choose Add > New Item and then from the ArcGIS Pro Add-ins group select ArcGIS Pro Button from the list of item templates. When prompted, name the new class file SingleSymbol.cs and press Add to close the dialog box. Add a second button using the same steps, and name this class GraduatedColor.cs.

  2. Add the following code to the OnClick method of SingleSymbol.cs:

    Module1.ApplySymbol("single symbol");
    
  3. Add the following code to the OnClick method of GraduatedColor.cs:

    Module1.ApplySymbol("graduated color");
    
  4. Update the Config.daml file AddInInfo section so that the DAML appears as follows. This code updates the captions for each of the buttons, and the caption of the control group, which will appear on the Add-In tab.

    </AddInInfo>
    <!-- *** Add *** -->
    <modules>
        <insertModule id="LayerSymbology_Module" className="Module1" autoLoad="false" caption="Module1">
        <groups>
            <group id="LayerSymbology_Group1" caption="Point Layer Symbols" appearsOnAddInTab="true">
            <!-- host controls within groups -->
            <button refID="LayerSymbology_SingleSymbol" size="large" />
            <button refID="LayerSymbology_GraduatedColor" size="large" />
            </group>
        </groups>
        <controls>
            <button id="LayerSymbology_SingleSymbol" caption="Apply Single Symbol" className="SingleSymbol" loadOnClick="true" smallImage="pack://application:,,,/ArcGIS.Desktop.Resources;component/Images/GenericButtonBlue16.png" largeImage="pack://application:,,,/ArcGIS.Desktop.Resources;component/Images/GenericButtonBlue32.png">
            <tooltip heading="Tooltip Heading">Tooltip text<disabledText /></tooltip>
            </button>
            <button id="LayerSymbology_GraduatedColor" caption="Apply Graduated Color" className="GraduatedColor" loadOnClick="true" smallImage="pack://application:,,,/ArcGIS.Desktop.Resources;component/Images/GenericButtonBlue16.png" largeImage="pack://application:,,,/ArcGIS.Desktop.Resources;component/Images/GenericButtonBlue32.png">
            <tooltip heading="Tooltip Heading">Tooltip text<disabledText /></tooltip>
            </button>
        </controls>
        </insertModule>
    </modules>
    

Create a new method in the module file

  1. Copy the following code to create a new ApplySymbol method, and paste it into the Module1.cs file below the Overrides region.
        #region Business Logic
    
        public static void ApplySymbol(string symbolChoice)
        {
            QueuedTask.Run(() =>
            {
                // Check for an active 2D mapview, if not, then prompt and exit.
                if (MapView.Active == null || (MapView.Active.ViewingMode != ArcGIS.Core.CIM.MapViewingMode.Map))
                {
                    ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("An active 2D MapView is required to use this tool. Exiting...", "Info");
                    return;
                }
                // Get the layer(s) selected in the Contents pane, if there is not just one, then prompt then exit.
                if (MapView.Active.GetSelectedLayers().Count != 1)
                {
                    ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("One feature layer must be selected in the Contents pane. Exiting...", "Info");
                    return;
                }
                // Check to see if the selected layer is a feature layer, if not, then prompt and exit.
                var featLayer = MapView.Active.GetSelectedLayers().First() as FeatureLayer;
                if (featLayer == null)
                {
                    ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("A feature layer must be selected in the Contents pane. Exiting...", "Info");
                    return;
                }
                // Check if the feature layer shape type is point, if not, then prompt and exit.
                else if (featLayer.ShapeType != esriGeometryType.esriGeometryPoint)
                {
                    ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Selected feature layer must be shape type POINT. Exiting...", "Info");
                    return;
                }
                try
                {
                    // If symbolChoice is a single symbol
                    if (symbolChoice == "single symbol")
                    {
                        // Get all styles in the project
                        var styles = Project.Current.GetItems<StyleProjectItem>();
    
                        // Get a specific style in the project
                        StyleProjectItem style = styles.First(s => s.Name == "ArcGIS 2D");
    
                        // Get the Push Pin 1 symbol
                        var pt_ssi = style.SearchSymbols(StyleItemType.PointSymbol, "Push Pin 1").FirstOrDefault();
    
                        // Create a new renderer definition and reference the symbol
                        SimpleRendererDefinition srDef = new SimpleRendererDefinition();
                        srDef.SymbolTemplate = pt_ssi.Symbol.MakeSymbolReference();
    
                        // Create the renderer and apply the definition
                        CIMSimpleRenderer ssRenderer = (CIMSimpleRenderer)featLayer.CreateRenderer(srDef);
    
                        // Update the feature layer renderer
                        featLayer.SetRenderer(ssRenderer);
                    }
                    else if (symbolChoice == "graduated color")
                    {
                        // Get a style and color ramp to apply to the renderer
                        StyleProjectItem style = Project.Current.GetItems<StyleProjectItem>().First(s => s.Name == "ColorBrewer Schemes (RGB)");
                        IList<ColorRampStyleItem> colorRampList = style.SearchColorRamps("Greens (Continuous)");
                        ColorRampStyleItem aColorRamp = colorRampList[0];
    
                        // Create a graduated color renderer definition with 3 breaks, a populated numeric field named "Symbol" is required
                        GraduatedColorsRendererDefinition gcDef = new GraduatedColorsRendererDefinition()
                        {
                            ClassificationField = "Symbol",
                            ClassificationMethod = ArcGIS.Core.CIM.ClassificationMethod.EqualInterval,
                            BreakCount = 4,
                            ColorRamp = aColorRamp.ColorRamp,
                            SymbolTemplate = SymbolFactory.Instance.ConstructPointSymbol().MakeSymbolReference(),
                        };
    
                        // Create the renderer and apply the definition
                        CIMClassBreaksRenderer cbRndr = (CIMClassBreaksRenderer)featLayer.CreateRenderer(gcDef);
    
                        // Update the feature layer renderer
                        featLayer.SetRenderer(cbRndr);
                    }
                }
                catch (Exception exc)
                {
                    // Catch any exception found and display in a message box
                    ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Exception caught: " + exc.Message);
                    return;
                }
            });
        }
    
        #endregion
    

Update the dataset and test the add-in

  1. Updating the layer symbology to graduated colors will require a numeric attribute named Symbol on which to apply an equal interval classification. One way to provide this is to add a new field to the Pro SDK community sample dataset, FeatureTest, which will normally be installed under C:\Data\FeatureTest\. The remaining steps assume you are using this dataset for testing.

  2. Build your project and resolve any issues. Run your project by choosing Start Debugging which will launch ArcGIS Pro. Open the FeatureTest.aprx project. In the map view, find the layer named TestPoints. Add a new integer attribute named Symbol to this layer and then calculate its values to match the values in the ObjectID attribute.

  3. Once the new Symbol attribute is available and populated, click on the Add-In tab and find your new control group with your two new buttons. Test both of your buttons with the TestPoints point feature layer to see it rendered with either a new single symbol or graduated colors.

Congratulations, you're done!

Compare your solution with our completed solution project.

Challenge

Review the online documentation

Read more about working with the Map Authoring API and renderers in the document ProConcepts Map Authoring.

Try a community sample

Try some of the Map Authoring topic community samples on GitHub.