How to refresh a CIMLegendItem?

462
2
Jump to solution
03-05-2020 04:44 PM
LesleyBross1
New Contributor III

I am using the Layer.ReplaceDataSource() to swap out the data source on a raster layer. This is a continuous raster. I also update the raster layer name. However, the legendItem associated with that layer does not completely update. The name changes to the new layer name, but the min and max values for the layer in the legend retain their original values. Is there a way to force this to update?

0 Kudos
1 Solution

Accepted Solutions
UmaHarano
Esri Regular Contributor

Hi Lesley

When you change the underlying datasource for a raster (or any feature layer for that matter), you will have also change the Colorizer (Change the renderer for a feature layer). This will update the Legend in the TOC. So it is a 2 step process:

1. Change the datasource using the ReplaceDataSource method.

2. Then apply the colorizer suitable for the new data.

This is the code snippet I used:

var rasterLayer = MapView.Active.Map.GetLayersAsFlattenedList().OfType<RasterLayer>().FirstOrDefault();
QueuedTask.Run(async () => {
        using (Geodatabase geodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri(@"\\Path-to-Data\raster.gdb"))))
        {
          using (Dataset dataset = geodatabase.OpenDataset<RasterDataset>("elevation"))
          {
            rasterLayer.ReplaceDataSource(dataset);

            // Check if the Stretch colorizer can be applied to the raster layer.
            if (rasterLayer.GetApplicableColorizers().Contains(RasterColorizerType.StretchColorizer))
            {
              // Create a new Stretch Colorizer Definition using the default constructor.
              StretchColorizerDefinition stretchColorizerDef_default = new StretchColorizerDefinition();
              // Create a new Stretch colorizer using the colorizer definition created above.
              CIMRasterStretchColorizer newStretchColorizer_default =
                await rasterLayer.CreateColorizerAsync(stretchColorizerDef_default) as CIMRasterStretchColorizer;
              // Set the new colorizer on the raster layer.
              rasterLayer.SetColorizer(newStretchColorizer_default);
            }
          }       
        }
      });‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

2 Replies
UmaHarano
Esri Regular Contributor

Hi Lesley

When you change the underlying datasource for a raster (or any feature layer for that matter), you will have also change the Colorizer (Change the renderer for a feature layer). This will update the Legend in the TOC. So it is a 2 step process:

1. Change the datasource using the ReplaceDataSource method.

2. Then apply the colorizer suitable for the new data.

This is the code snippet I used:

var rasterLayer = MapView.Active.Map.GetLayersAsFlattenedList().OfType<RasterLayer>().FirstOrDefault();
QueuedTask.Run(async () => {
        using (Geodatabase geodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri(@"\\Path-to-Data\raster.gdb"))))
        {
          using (Dataset dataset = geodatabase.OpenDataset<RasterDataset>("elevation"))
          {
            rasterLayer.ReplaceDataSource(dataset);

            // Check if the Stretch colorizer can be applied to the raster layer.
            if (rasterLayer.GetApplicableColorizers().Contains(RasterColorizerType.StretchColorizer))
            {
              // Create a new Stretch Colorizer Definition using the default constructor.
              StretchColorizerDefinition stretchColorizerDef_default = new StretchColorizerDefinition();
              // Create a new Stretch colorizer using the colorizer definition created above.
              CIMRasterStretchColorizer newStretchColorizer_default =
                await rasterLayer.CreateColorizerAsync(stretchColorizerDef_default) as CIMRasterStretchColorizer;
              // Set the new colorizer on the raster layer.
              rasterLayer.SetColorizer(newStretchColorizer_default);
            }
          }       
        }
      });‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
LesleyBross1
New Contributor III

Thanks Uma. That works!

0 Kudos