Apply a hillshade renderer to a raster.

Use case
An environmental agency may track coastal erosion by comparing images of an area taken over a a longer period of time with hillshade renderers applied.
How to use the sample
Choose and adjust the settings to update the hillshade renderer on the raster layer. The sample allows you to change the Altitude, Azimuth, and Slope Type.
How it works
- Create a
Rasterfrom a grayscale raster file. - Create a
RasterLayerfrom the raster. - Create a
Basemapfrom the raster layer and set it to the map. - Create a
HillshadeRenderer, specifying the slope type and other properties,new HillshadeRenderer(Altitude, Azimuth, ZFactor, SlopeType, PixelSizeFactor, PixelSizePower, OutputBitDepth). - Set the hillshade renderer to be used on the raster layer with
rasterLayer::setRenderer(renderer).
Relevant API
- Basemap
- HillshadeRenderer
- Raster
- RasterLayer
Offline Data
Read more about how to set up the sample’s offline data here.
| Link | Local Location |
|---|---|
| Hillshade raster | <userhome>/ArcGIS/Runtime/Data/raster/srtm.tiff |
Tags
altitude, angle, azimuth, raster, slope, visualization
Sample Code
// [WriteFile Name=Hillshade_Renderer, Category=Layers]// [Legal]// Copyright 2016 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.// [Legal]
import QtQuickimport QtQuick.Controlsimport QtQuick.Layoutsimport Esri.Samples
Rectangle { id: root color: "transparent" visible: false
Rectangle { anchors.fill: parent color: "#60000000" }
MouseArea { anchors.fill: parent onClicked: mouse => mouse.accepted = true onWheel: wheel => wheel.accepted = true }
Rectangle { anchors.centerIn: parent width: childrenRect.width height: childrenRect.height color: "lightgrey" radius: 5 border { color: "#4D4D4D" width: 1 }
GridLayout { columns: 2
Text { Layout.margins: 5 Layout.columnSpan: 2 Layout.alignment: Qt.AlignHCenter text: "Hillshade Renderer Settings" font.weight: Font.DemiBold }
Text { Layout.margins: 5 text: "Altitude" }
Slider {
id: altitudeSlider Layout.margins: 5 from: 0 to: 90 }
Text { Layout.margins: 5 text: "Azimuth" }
Slider { id: azimuthSlider Layout.margins: 5 from: 0 to: 360 }
Text { Layout.margins: 5 text: "Slope" }
ComboBox { id: slopeBox property int modelWidth: 0 Layout.minimumWidth: modelWidth + leftPadding + rightPadding + (indicator ? indicator.width : 10) Layout.margins: 5 Layout.fillWidth: true model: HillshadeSlopeTypeModel{} textRole: "name"
Component.onCompleted : { for (let i = 0; i < model.count; ++i) { metrics.text = model.get(i).name; modelWidth = Math.max(modelWidth, metrics.width); } } TextMetrics { id: metrics font: slopeBox.font } }
Button { Layout.margins: 5 Layout.columnSpan: 2 Layout.alignment: Qt.AlignHCenter text: "Apply" onClicked: { const altitude = altitudeSlider.value; const azimuth = azimuthSlider.value; const slope = slopeBox.model.get(slopeBox.currentIndex).value; applyHillshadeRenderer(altitude, azimuth, slope); root.visible = false; } } } }}// [WriteFile Name=Hillshade_Renderer, Category=Layers]// [Legal]// Copyright 2016 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.// [Legal]
import QtQuick
ListModel { ListElement { name: "None" value: -1 } ListElement { name: "Degree" value: 0 } ListElement { name: "Percent Rise" value: 1 } ListElement { name: "Scaled" value: 2 }}// [WriteFile Name=Hillshade_Renderer, Category=Layers]// [Legal]// Copyright 2016 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.// [Legal]
#ifdef PCH_BUILD#include "pch.hpp"#endif // PCH_BUILD
// sample headers#include "Hillshade_Renderer.h"
// ArcGIS Maps SDK headers#include "Basemap.h"#include "HillshadeRenderer.h"#include "Map.h"#include "MapQuickView.h"#include "MapTypes.h"#include "MapViewTypes.h"#include "Raster.h"#include "RasterLayer.h"#include "RasterTypes.h"
// Qt headers#include <QFuture>#include <QStandardPaths>#include <QUrl>#include <QtCore/qglobal.h>
using namespace Esri::ArcGISRuntime;
// helper method to get cross platform data pathnamespace{QString defaultDataPath(){ QString dataPath;
#ifdef Q_OS_IOS dataPath = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);#else dataPath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);#endif
return dataPath;}} // namespace
Hillshade_Renderer::Hillshade_Renderer(QQuickItem* parent /* = nullptr */): QQuickItem(parent){}
Hillshade_Renderer::~Hillshade_Renderer() = default;
void Hillshade_Renderer::init(){ qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView"); qmlRegisterType<Hillshade_Renderer>("Esri.Samples", 1, 0, "Hillshade_RendererSample");}
void Hillshade_Renderer::componentComplete(){ QQuickItem::componentComplete();
// find QML MapView component m_mapView = findChild<MapQuickView*>("mapView"); m_mapView->setWrapAroundMode(WrapAroundMode::Disabled);
// Create the raster and raster layer const QString dataPath = defaultDataPath() + "/ArcGIS/Runtime/Data/raster"; Raster* raster = new Raster(dataPath + "/srtm.tiff", this); m_rasterLayer = new RasterLayer(raster, this);
// Add the raster to the map Basemap* basemap = new Basemap(m_rasterLayer, this); Map* map = new Map(basemap, this);
// zoom to the new layer once loaded connect(map, &Map::loadStatusChanged, this, [this](LoadStatus loadStatus) { if (loadStatus == LoadStatus::Loaded) { m_mapView->setViewpointScaleAsync(754479); } }); m_mapView->setMap(map);
//! [HillshadeRenderer apply to layer snippet] // Apply the hillshade renderer to the raster layer constexpr double altitude = 45.0; constexpr double azimuth = 315.0; constexpr double zFactor = 0.000016; constexpr SlopeType slopeType = SlopeType::None; constexpr double pixelSizeFactor = 1.0; constexpr double pixelSizePower = 1.0; constexpr int outputBitDepth = 8; HillshadeRenderer* hillshadeRenderer = new HillshadeRenderer(altitude, azimuth, zFactor, slopeType, pixelSizeFactor, pixelSizePower, outputBitDepth, this); m_rasterLayer->setRenderer(hillshadeRenderer); //! [HillshadeRenderer apply to layer snippet]}
void Hillshade_Renderer::applyHillshadeRenderer(double altitude, double azimuth, int slope){ // create the new renderer SlopeType slopeType = static_cast<SlopeType>(slope); HillshadeRenderer* hillshadeRenderer = new HillshadeRenderer(altitude, azimuth, 0.000016, slopeType, 1.0, 1.0, 8, this);
// set the renderer on the layer m_rasterLayer->setRenderer(hillshadeRenderer);}// [WriteFile Name=Hillshade_Renderer, Category=Layers]// [Legal]// Copyright 2016 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.// [Legal]
#ifndef HILLSHADE_RENDERER_H#define HILLSHADE_RENDERER_H
// Qt headers#include <QQuickItem>
namespace Esri::ArcGISRuntime{ class MapQuickView; class RasterLayer;}
class Hillshade_Renderer : public QQuickItem{ Q_OBJECT
public: explicit Hillshade_Renderer(QQuickItem* parent = nullptr); ~Hillshade_Renderer() override;
void componentComplete() override; static void init(); Q_INVOKABLE void applyHillshadeRenderer(double altitude, double azimuth, int slope);
private: Esri::ArcGISRuntime::MapQuickView* m_mapView = nullptr; Esri::ArcGISRuntime::RasterLayer* m_rasterLayer = nullptr;};
#endif // HILLSHADE_RENDERER_H// [WriteFile Name=Hillshade_Renderer, Category=Layers]// [Legal]// Copyright 2016 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.// [Legal]
import QtQuickimport QtQuick.Controlsimport Esri.Samples
Hillshade_RendererSample { id: rootRectangle clip: true width: 800 height: 600
// add a mapView component MapView { anchors.fill: parent objectName: "mapView"
Component.onCompleted: { // Set the focus on MapView to initially enable keyboard navigation forceActiveFocus(); } }
Button { anchors { horizontalCenter: parent.horizontalCenter bottom: parent.bottom bottomMargin: 25 } text: "Edit Renderer" onClicked: hillshadeSettings.visible = true; }
HillshadeSettings { id: hillshadeSettings anchors.fill: parent }}// [Legal]// Copyright 2015 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.// [Legal]
// sample headers#include "Hillshade_Renderer.h"
// ArcGIS Maps SDK headers#include "ArcGISRuntimeEnvironment.h"
// Qt headers#include <QCommandLineParser>#include <QDir>#include <QGuiApplication>#include <QQmlEngine>#include <QQuickView>
// Platform specific headers#ifdef Q_OS_WIN#include <Windows.h>#endif
#define STRINGIZE(x) #x#define QUOTE(x) STRINGIZE(x)
int main(int argc, char *argv[]){ Esri::ArcGISRuntime::ArcGISRuntimeEnvironment::setUseLegacyAuthentication(false); QGuiApplication app(argc, argv); app.setApplicationName(QString("Hillshade_Renderer"));
// Use of ArcGIS location services, such as basemap styles, geocoding, and routing services, // requires an access token. For more information see // https://links.esri.com/arcgis-runtime-security-auth.
// The following methods grant an access token:
// 1. User authentication: Grants a temporary access token associated with a user's ArcGIS account. // To generate a token, a user logs in to the app with an ArcGIS account that is part of an // organization in ArcGIS Online or ArcGIS Enterprise.
// 2. API key authentication: Get a long-lived access token that gives your application access to // ArcGIS location services. Go to the tutorial at https://links.esri.com/create-an-api-key. // Copy the API Key access token.
const QString accessToken = QString("");
if (accessToken.isEmpty()) { qWarning() << "Use of ArcGIS location services, such as the basemap styles service, requires" << "you to authenticate with an ArcGIS account or set the API Key property."; } else { Esri::ArcGISRuntime::ArcGISRuntimeEnvironment::setApiKey(accessToken); }
// Initialize the sample Hillshade_Renderer::init();
// Initialize application view QQuickView view; view.setResizeMode(QQuickView::SizeRootObjectToView);
// Add the import Path view.engine()->addImportPath(QDir(QCoreApplication::applicationDirPath()).filePath("qml"));
QString arcGISRuntimeImportPath = QUOTE(ARCGIS_RUNTIME_IMPORT_PATH);
#if defined(LINUX_PLATFORM_REPLACEMENT) // on some linux platforms the string 'linux' is replaced with 1 // fix the replacement paths which were created QString replaceString = QUOTE(LINUX_PLATFORM_REPLACEMENT); arcGISRuntimeImportPath = arcGISRuntimeImportPath.replace(replaceString, "linux", Qt::CaseSensitive); #endif
// Add the Runtime and Extras path view.engine()->addImportPath(arcGISRuntimeImportPath);
// Set the source view.setSource(QUrl("qrc:/Samples/Layers/Hillshade_Renderer/Hillshade_Renderer.qml"));
view.show();
return app.exec();}