Skip To Content ArcGIS for Developers Sign In Dashboard

ArcGIS Runtime SDK for .NET

Blend renderer

This code sample is available for these platforms:
View Sample on GitHub

Blend a hillshade with a raster by specifying the elevation data. The resulting raster looks similar to the original raster, but with some terrain shading, giving it a textured look.

screenshot

Use case

BlendRenderer can be used to apply a color ramp to a hillshade to emphasize areas of high or low elevation. A BlendRenderer can also be used to add a hillshade effect to aerial or satellite imagery, thereby making changes in elevation more visible.

How to use the sample

Choose and adjust the altitude, azimuth, slope type, and color ramp type settings to update the image.

How it works

  1. Create a Raster object from a raster file.
  2. Create a RasterLayer object from the raster.
  3. Create a Basemap object from the raster layer and set it to the map.
  4. Create another Raster object for elevation from a grayscale raster file.
  5. Create a BlendRenderer object, specifying the elevation raster, color ramp, and other properties.
    • If you specify a non-null color ramp, use the elevation raster as the base raster in addition to the elevation raster parameter. That way, the color ramp is used instead of the satellite imagery.
  6. Set the blend renderer to the raster layer.

Relevant API

  • BlendRenderer
  • ColorRamp
  • Raster
  • RasterLayer

Offline data

This sample downloads the following items from ArcGIS Online automatically:

Tags

Elevation, Hillshade, RasterLayer, color ramp, elevation, image, visualization

Sample Code

<UserControl x:Class="ArcGISRuntime.UWP.Samples.ChangeBlendRenderer.ChangeBlendRenderer"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:esriUI="using:Esri.ArcGISRuntime.UI.Controls">
    <UserControl.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="FontWeight" Value="SemiBold" />
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="HorizontalAlignment" Value="Right" />
        </Style>
        <Style TargetType="TextBox">
            <Setter Property="HorizontalAlignment" Value="Stretch" />
            <Setter Property="Margin" Value="0,5,0,0" />
        </Style>
        <Style TargetType="Slider">
            <Setter Property="Padding" Value="0,0,0,0" />
            <Setter Property="Margin" Value="5,0,0,-8" />
            <Setter Property="HorizontalAlignment" Value="Stretch" />
        </Style>
        <Style TargetType="ComboBox">
            <Setter Property="HorizontalAlignment" Value="Stretch" />
            <Setter Property="Margin" Value="5,5,0,0" />
        </Style>
    </UserControl.Resources>
    <Grid>
        <esriUI:MapView x:Name="MyMapView" />
        <Border Style="{StaticResource BorderStyle}">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <TextBlock
                    Text="Tap 'Update renderer' to change the settings for the blend renderer. If you use None as the ColorRamp, a standard hill shade raster output is displayed. For all the other ColorRamp types an elevation raster is used."
                    Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2"
                    Margin="0,0,0,10"
                    TextWrapping="Wrap" HorizontalAlignment="Stretch" />
                <TextBlock Text="Altitude: "
                           Grid.Row="1" Grid.Column="0" />
                <Slider x:Name="AltitudeSlider"
                        Grid.Row="1" Grid.Column="1" />
                <TextBlock Text="Azimuth:"
                           Grid.Row="2" Grid.Column="0" />
                <Slider x:Name="AzimuthSlider"
                        Grid.Row="2" Grid.Column="1" />
                <TextBlock Text="Slope type:"
                           Grid.Row="3" Grid.Column="0" />
                <ComboBox x:Name="SlopeTypes"
                          Grid.Row="3" Grid.Column="1" />
                <TextBlock Text="Color ramp:"
                           Grid.Row="4" Grid.Column="0" />
                <ComboBox x:Name="ColorRamps"
                          Grid.Row="4" Grid.Column="1" />
                <Button x:Name="UpdateRenderer"
                        Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="2"
                        Content="Update renderer"
                        Margin="0,5,0,0"
                        IsEnabled="False"
                        HorizontalAlignment="Stretch"
                        Click="OnUpdateRendererClicked" />
            </Grid>
        </Border>
    </Grid>
</UserControl>
// Copyright 2018 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.Samples.Managers;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Rasters;
using System;
using System.Collections.Generic;
using Windows.UI.Popups;

namespace ArcGISRuntime.UWP.Samples.ChangeBlendRenderer
{
    [ArcGISRuntime.Samples.Shared.Attributes.Sample(
        "Blend renderer",
        "Layers",
        "This sample demonstrates how to use blend renderer on a raster layer. You can get a hillshade blended with either a colored raster or color ramp.",
        "Tap on the 'Update Renderer' button to change the settings for the blend renderer. The sample allows you to change the Altitude, Azimuth, SlopeType and ColorRamp. If you use None as the ColorRamp, a standard hill shade raster output is displayed. For all the other ColorRamp types an elevation raster is used.",
        "Featured")]
	[ArcGISRuntime.Samples.Shared.Attributes.OfflineData("7c4c679ab06a4df19dc497f577f111bd","caeef9aa78534760b07158bb8e068462")]
    public partial class ChangeBlendRenderer
    {
        public ChangeBlendRenderer()
        {
            InitializeComponent();

            // Create the UI, setup the control references and execute initialization 
            Initialize();
        }

        private async void Initialize()
        {
            // Get all the ColorRamp names from the PresetColorRampType Enumeration and put them 
            // in an array of strings, then set the ComboBox.ItemSource to the array, and finally 
            // select the first item in the ComboBox
            string[] myPresetColorRampTypes = System.Enum.GetNames(typeof(PresetColorRampType));
            ColorRamps.ItemsSource = myPresetColorRampTypes;
            ColorRamps.SelectedIndex = 0;

            // Get all the SlopeType names from the SlopeType Enumeration and put them 
            // in an array of strings, then set the ComboBox.ItemSource to the array, and finally 
            // select the first item in the ComboBox
            string[] mySlopeTypes = System.Enum.GetNames(typeof(SlopeType));
            SlopeTypes.ItemsSource = mySlopeTypes;
            SlopeTypes.SelectedIndex = 0;

            // Set the altitude slider min/max and initial value
            AltitudeSlider.Minimum = 0;
            AltitudeSlider.Maximum = 90;
            AltitudeSlider.Value = 45;

            // Set the azimuth slider min/max and initial value
            AzimuthSlider.Minimum = 0;
            AzimuthSlider.Maximum = 360;
            AzimuthSlider.Value = 180;

            // Load the raster file using a path on disk
            Raster myRasterImagery = new Raster(GetRasterPath_Imagery());

            // Create the raster layer from the raster
            RasterLayer myRasterLayerImagery = new RasterLayer(myRasterImagery);

            // Create a new map using the raster layer as the base map 
            Map myMap = new Map(new Basemap(myRasterLayerImagery));

            try
            {
                // Wait for the layer to load - this enabled being able to obtain the extent information 
                // of the raster layer
                await myRasterLayerImagery.LoadAsync();

                // Create a new EnvelopeBuilder from the full extent of the raster layer 
                EnvelopeBuilder myEnvelopBuilder = new EnvelopeBuilder(myRasterLayerImagery.FullExtent);

                // Zoom in the extent just a bit so that raster layer encompasses the entire viewable area of the map
                myEnvelopBuilder.Expand(0.75);

                // Set the viewpoint of the map to the EnvelopeBuilder's extent
                myMap.InitialViewpoint = new Viewpoint(myEnvelopBuilder.ToGeometry().Extent);

                // Add map to the map view
                MyMapView.Map = myMap;

                // Wait for the map to load
                await myMap.LoadAsync();

                // Enable the 'Update Renderer' button now that the map has loaded
                UpdateRenderer.IsEnabled = true;
            }
            catch (Exception e)
            {
                await new MessageDialog(e.ToString(), "Error").ShowAsync();
            }
        }

        private void OnUpdateRendererClicked(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            // Define the RasterLayer that will be used to display in the map
            RasterLayer layerForDisplayInMap;

            // Define the ColorRamp that will be used by the BlendRenderer
            ColorRamp myColorRamp;

            // Based on ColorRamp type chosen by the user, create a different
            // RasterLayer and define the appropriate ColorRamp option
            if (ColorRamps.SelectedValue.ToString() == "None")
            {
                // The user chose not to use a specific ColorRamp, therefore 
                // need to create a RasterLayer based on general imagery (ie. Shasta.tif)
                // for display in the map and use null for the ColorRamp as one of the
                // parameters in the BlendRenderer constructor

                // Load the raster file using a path on disk
                Raster rasterImagery = new Raster(GetRasterPath_Imagery());

                // Create the raster layer from the raster
                layerForDisplayInMap = new RasterLayer(rasterImagery);

                // Set up the ColorRamp as being null
                myColorRamp = null;
            }
            else
            {
                // The user chose a specific ColorRamp (options: are Elevation, DemScreen, DemLight), 
                // therefore create a RasterLayer based on an imagery with elevation 
                // (ie. Shasta_Elevation.tif) for display in the map. Also create a ColorRamp 
                // based on the user choice, translated into an Enumeration, as one of the parameters 
                // in the BlendRenderer constructor

                // Load the raster file using a path on disk
                Raster rasterElevation = new Raster(GetRasterPath_Elevation());

                // Create the raster layer from the raster
                layerForDisplayInMap = new RasterLayer(rasterElevation);

                // Create a ColorRamp based on the user choice, translated into an Enumeration
                PresetColorRampType myPresetColorRampType = (PresetColorRampType)Enum.Parse(typeof(PresetColorRampType), ColorRamps.SelectedValue.ToString());
                myColorRamp = ColorRamp.Create(myPresetColorRampType, 256);
            }

            // Define the parameters used by the BlendRenderer constructor
            Raster rasterForMakingBlendRenderer = new Raster(GetRasterPath_Elevation());
            IEnumerable<double> myOutputMinValues = new List<double> { 9 };
            IEnumerable<double> myOutputMaxValues = new List<double> { 255 };
            IEnumerable<double> mySourceMinValues = new List<double>();
            IEnumerable<double> mySourceMaxValues = new List<double>();
            IEnumerable<double> myNoDataValues = new List<double>();
            IEnumerable<double> myGammas = new List<double>();
            SlopeType mySlopeType = (SlopeType)Enum.Parse(typeof(SlopeType), SlopeTypes.SelectedValue.ToString());

            BlendRenderer myBlendRenderer = new BlendRenderer(
                rasterForMakingBlendRenderer, // elevationRaster - Raster based on a elevation source
                myOutputMinValues, // outputMinValues - Output stretch values, one for each band
                myOutputMaxValues, // outputMaxValues - Output stretch values, one for each band
                mySourceMinValues, // sourceMinValues - Input stretch values, one for each band
                mySourceMaxValues, // sourceMaxValues - Input stretch values, one for each band
                myNoDataValues, // noDataValues - NoData values, one for each band
                myGammas, // gammas - Gamma adjustment
                myColorRamp, // colorRamp - ColorRamp object to use, could be null
                AltitudeSlider.Value, // altitude - Altitude angle of the light source
                AzimuthSlider.Value, // azimuth - Azimuth angle of the light source, measured clockwise from north
                1, // zfactor - Factor to convert z unit to x,y units, default is 1
                mySlopeType, // slopeType - Slope Type
                1, // pixelSizeFactor - Pixel size factor, default is 1
                1, // pixelSizePower - Pixel size power value, default is 1
                8); // outputBitDepth - Output bit depth, default is 8-bi

            // Set the RasterLayer.Renderer to be the BlendRenderer
            layerForDisplayInMap.Renderer = myBlendRenderer;

            // Set the new base map to be the RasterLayer with the BlendRenderer applied
            MyMapView.Map.Basemap = new Basemap(layerForDisplayInMap);
        }

        private static string GetRasterPath_Imagery()
        {
            return DataManager.GetDataFolder("7c4c679ab06a4df19dc497f577f111bd", "raster-file", "Shasta.tif");
        }

        private static string GetRasterPath_Elevation()
        {
            return DataManager.GetDataFolder("caeef9aa78534760b07158bb8e068462", "Shasta_Elevation.tif");
        }

    }
}