Skip To Content ArcGIS for Developers Sign In Dashboard

ArcGIS Runtime SDK for .NET

Raster hillshade renderer

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

Use a hillshade renderer on a raster.

screenshot

How to use the sample

Configure the options for rendering, then click 'Apply hillshade'.

How it works

  1. The parameters provided by the user are passed to HillshadeRender at instantiation: new HillshadeRenderer(mAltitude, mAzimuth, mZFactor, mSlopeType, mPixelSizeFactor, mPixelSizePower, mOutputBitDepth); which returns a RasterRenderer. The RasterRenderer is then added to the RasterLayer.

Offline data

This sample downloads the following items from ArcGIS Online automatically:

Tags

Visualization, hillshade, raster, shadow, slope

Sample Code

<UserControl x:Class="ArcGISRuntime.UWP.Samples.RasterHillshade.RasterHillshade"
             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">
    <Grid>
        <esriUI:MapView x:Name="MyMapView" />
        <Border Style="{StaticResource BorderStyle}">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition Width="2*"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <TextBlock Grid.Row="0" Grid.Column="0"
                           TextAlignment="Right" VerticalAlignment="Center"
                           Text="Slope type:"/>
                <ComboBox x:Name="SlopeTypeCombo"
                          Margin="5"
                          Grid.Row="0" Grid.Column="1"
                          HorizontalAlignment="Stretch"
                          />
                <TextBlock Grid.Row="1" Grid.Column="0"
                           TextAlignment="Right" VerticalAlignment="Center"
                           Text="Altitude:"/>
                <Slider x:Name="AltitudeSlider" 
                        Margin="5"
                        Grid.Row="1" Grid.Column="1"
                        Minimum="0" Maximum="90"
                        IsThumbToolTipEnabled="True"/>
                <TextBlock Grid.Row="2" Grid.Column="0"
                           TextAlignment="Right" VerticalAlignment="Center"
                           Text="Azimuth:"/>
                <Slider x:Name="AzimuthSlider" 
                        Margin="5"
                        Grid.Row="2" Grid.Column="1"
                        Minimum="0" Maximum="360"
                        IsThumbToolTipEnabled="True"/>
                <Button x:Name="ApplyHillshadeButton"
                        Grid.Row="3" Grid.ColumnSpan="2"
                        Margin="5,0" Padding="3"
                        VerticalAlignment="Center" HorizontalAlignment="Stretch"
                        Content="Apply hillshade"
                        Click="ApplyHillshadeButton_Click"/>
            </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.Mapping;
using Esri.ArcGISRuntime.Rasters;
using System;
using Windows.UI.Popups;

namespace ArcGISRuntime.UWP.Samples.RasterHillshade
{
    [ArcGISRuntime.Samples.Shared.Attributes.Sample(
        "Raster hillshade renderer",
        "Layers",
        "This sample demonstrates how to use a hillshade renderer on a raster layer. Hillshade renderers can adjust a grayscale raster (usually of terrain) according to a hypothetical sun position (azimuth and altitude).",
        "", "Featured")]
    [ArcGISRuntime.Samples.Shared.Attributes.OfflineData("134d60f50e184e8fa56365f44e5ce3fb")]
    public partial class RasterHillshade
    {
        // Constant to store a z-factor (conversion constant) applied to the hillshade.
        // If needed, this can be used to convert z-values to the same unit as the x/y coordinates or to apply a vertical exaggeration.
        private const double ZFactor = 1.0;

        // Constants to store the Pixel Size Power and Pixel Size Factor values.
        // Use these to account for altitude changes (scale) as the viewer zooms in and out (recommended when using worldwide datasets).
        private const double PixelSizePower = 1.0;

        private const double PixelSizeFactor = 1.0;

        // Constant to store the bit depth (pixel depth), which determines the range of values that the hillshade raster can store.
        private const int PixelBitDepth = 8;

        // Store a reference to the layer
        private RasterLayer _rasterLayer;

        public RasterHillshade()
        {
            InitializeComponent();

            // Set up the map and load the raster layer from a local file
            Initialize();
        }

        private async void Initialize()
        {
            // Create a map with a streets basemap
            Map map = new Map(Basemap.CreateStreets());

            // Get the file name for the local raster dataset
            string filepath = GetRasterPath();

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

            try
            {
                // Create and load a new raster layer to show the image
                _rasterLayer = new RasterLayer(rasterFile);
                await _rasterLayer.LoadAsync();

                // Enable the apply renderer button when the layer loads.
                ApplyHillshadeButton.IsEnabled = true;

                // Create a viewpoint with the raster's full extent
                Viewpoint fullRasterExtent = new Viewpoint(_rasterLayer.FullExtent);

                // Set the initial viewpoint for the map
                map.InitialViewpoint = fullRasterExtent;

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

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

                // Add slope type values to the combo box
                foreach (object slope in Enum.GetValues(typeof(SlopeType)))
                {
                    SlopeTypeCombo.Items.Add(slope);
                }

                // Select the "Scaled" slope type enum
                SlopeTypeCombo.SelectedIndex = 2;
            }
            catch (Exception e)
            {
                await new MessageDialog(e.ToString(), "Error").ShowAsync();
            }
        }

        private void ApplyHillshadeButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            // Get the current parameter values
            double altitude = AltitudeSlider.Value;
            double azimuth = AzimuthSlider.Value;
            SlopeType typeOfSlope = (SlopeType)SlopeTypeCombo.SelectedItem;

            // Create a hillshade renderer that uses the values selected by the user
            HillshadeRenderer hillshadeRenderer = new HillshadeRenderer(altitude, azimuth, ZFactor, typeOfSlope, PixelSizeFactor, PixelSizePower, PixelBitDepth);

            // Apply the new renderer to the raster layer
            _rasterLayer.Renderer = hillshadeRenderer;
        }

        private static string GetRasterPath()
        {
            return DataManager.GetDataFolder("134d60f50e184e8fa56365f44e5ce3fb", "srtm-hillshade", "srtm.tiff");
        }
    }
}