Colormap renderer

View inAndroidFormsUWPWPFWinUIiOSView on GitHub

Apply a colormap renderer to a raster.

Image of colormap renderer

Use case

A colormap renderer transforms pixel values in a raster to display raster data based on specific colors, aiding in visual analysis of the data. For example, a forestry commission may want to quickly visualize areas above and below the tree-line line occurring at a know elevation on a raster containing elevation values. They could overlay a transparent colormap set to color those areas below the tree-line elevation green, and those above white.

How to use the sample

Pan and zoom to explore the effect of the colormap applied to the raster.

How it works

To apply a ColormapRenderer to a RasterLayer:

  1. Create a Raster from a raster file.
  2. Create a RasterLayer from the raster.
  3. Create an IEnumerable<Color> representing colors. Colors at the beginning of the list replace the darkest values in the raster and colors at the end of the list replaced the brightest values of the raster.
  4. Create a ColormapRenderer with the color list: ColormapRenderer(colors), and apply it to the raster layer with rasterLayer.Renderer = colormapRenderer.

Relevant API

  • Map
  • Basemap
  • ColormapRenderer
  • MapView
  • Raster
  • RasterLayer

Offline data

ShastaBW.tif raster

About the data

The raster used in this sample shows an area in the south of the Shasta-Trinity National Forest, California.

Tags

data, colormap, raster, renderer, visualization

Sample Code

RasterColormapRenderer.cs
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2019 Esri.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
// language governing permissions and limitations under the License.

using ArcGISRuntime;
using ArcGISRuntime.Samples.Managers;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Rasters;
using Esri.ArcGISRuntime.UI.Controls;
using Foundation;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using UIKit;

namespace ArcGISRuntimeXamarin.Samples.RasterColormapRenderer
{
    [Register("RasterColormapRenderer")]
    [ArcGISRuntime.Samples.Shared.Attributes.Sample(
        name: "Colormap renderer",
        category: "Layers",
        description: "Apply a colormap renderer to a raster.",
        instructions: "Pan and zoom to explore the effect of the colormap applied to the raster.",
        tags: new[] { "colormap", "data", "raster", "renderer", "visualization" })]
    [ArcGISRuntime.Samples.Shared.Attributes.OfflineData("95392f99970d4a71bd25951beb34a508")]
    public class RasterColormapRenderer : UIViewController
    {
        // Hold references to UI controls.
        private MapView _myMapView;

        // A single band raster file.
        private readonly string _rasterPath = DataManager.GetDataFolder("95392f99970d4a71bd25951beb34a508", "shasta", "ShastaBW.tif");

        public RasterColormapRenderer()
        {
            Title = "Raster colormap renderer";
        }

        private async void Initialize()
        {
            // Add an imagery basemap.
            Map map = new Map(BasemapStyle.ArcGISImageryStandard);

            // Load the raster file.
            Raster rasterFile = new Raster(_rasterPath);

            // Create the layer.
            RasterLayer rasterLayer = new RasterLayer(rasterFile);

            // Create a color map where values 0-149 are red and 150-249 are yellow.
            IEnumerable<Color> colors = new int[250]
               .Select((c, i) => i < 150 ? Color.Red : Color.Yellow);

            // Create a colormap renderer.
            ColormapRenderer colormapRenderer = new ColormapRenderer(colors);

            // Set the colormap renderer on the raster layer.
            rasterLayer.Renderer = colormapRenderer;

            // Add the layer to the map.
            map.OperationalLayers.Add(rasterLayer);

            // Add map to the mapview.
            _myMapView.Map = map;

            try
            {
                // Wait for the layer to load.
                await rasterLayer.LoadAsync();

                // Set the viewpoint.
                await _myMapView.SetViewpointGeometryAsync(rasterLayer.FullExtent, 15);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                CreateErrorDialog(e.Message);
            }
        }

        public override void LoadView()
        {
            // Create the views.
            View = new UIView() { BackgroundColor = ApplicationTheme.BackgroundColor };

            _myMapView = new MapView();
            _myMapView.TranslatesAutoresizingMaskIntoConstraints = false;

            // Add the views.
            View.AddSubviews(_myMapView);

            // Lay out the views.
            NSLayoutConstraint.ActivateConstraints(new[]{
                _myMapView.TopAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.TopAnchor),
                _myMapView.BottomAnchor.ConstraintEqualTo(View.BottomAnchor),
                _myMapView.LeadingAnchor.ConstraintEqualTo(View.LeadingAnchor),
                _myMapView.TrailingAnchor.ConstraintEqualTo(View.TrailingAnchor)
            });
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            Initialize();
        }

        private void CreateErrorDialog(string message)
        {
            // Create Alert.
            UIAlertController okAlertController = UIAlertController.Create("Error", message, UIAlertControllerStyle.Alert);

            // Add Action.
            okAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));

            // Present Alert.
            PresentViewController(okAlertController, true, null);
        }
    }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.