Create and use a raster layer made from a local raster file.

Use case
Rasters can be digital aerial photographs, imagery from satellites, digital pictures, or even scanned maps. An end-user will frequently need to import raster files acquired through various data-collection methods into their map to view and analyze the data.
How to use the sample
The sample will load with a map showing a basemap and a loaded raster layer. Tap on “Add Raster” to browse for a new raster file. On desktop platforms the sample also allows you to drag and drop supported raster files to load.
How it works
- Create a
Rasterfrom a raster file. - Create a
RasterLayerfrom the raster. - Add it as an operational layer with
map::operationalLayers()::append(rasterLayer).
Relevant API
- Raster
- RasterLayer
Offline Data
Read more about how to set up the sample’s offline data here.
| Link | Local Location |
|---|---|
| Shasta.tif raster | <userhome>/ArcGIS/Runtime/Data/raster/Shasta.tif |
Additional information
See the topic What is raster data? in the ArcMap documentation for more information about raster images.
Tags
data, image, import, layer, raster, visualization
Sample Code
// [WriteFile Name=RasterLayerFile, Category=Analysis]// [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 "RasterLayerFile.h"
// ArcGIS Maps SDK headers#include "Basemap.h"#include "Envelope.h"#include "Error.h"#include "LayerListModel.h"#include "Map.h"#include "MapQuickView.h"#include "MapTypes.h"#include "MapViewTypes.h"#include "Point.h"#include "Raster.h"#include "RasterLayer.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
RasterLayerFile::RasterLayerFile(QQuickItem* parent /* = nullptr */): QQuickItem(parent){}
RasterLayerFile::~RasterLayerFile() = default;
void RasterLayerFile::init(){ qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView"); qmlRegisterType<RasterLayerFile>("Esri.Samples", 1, 0, "RasterLayerFileSample");}
void RasterLayerFile::componentComplete(){ QQuickItem::componentComplete();
const QString dataPath = defaultDataPath() + "/ArcGIS/Runtime/Data/raster/";
// find QML MapView component m_mapView = findChild<MapQuickView*>("mapView"); m_mapView->setWrapAroundMode(WrapAroundMode::Disabled);
Basemap* basemap = new Basemap(BasemapStyle::ArcGISImageryStandard, this); m_map = new Map(basemap, this); m_mapView->setMap(m_map);
// Create a map using a raster layer createAndAddRasterLayer(dataPath + "Shasta.tif");}
void RasterLayerFile::createAndAddRasterLayer(QString dataPath){ // correct data path if contains file:/ prefix if (dataPath.contains("file:/")) dataPath = QString(QUrl(dataPath).toLocalFile());
//! [RasterLayerFile cpp new raster layer] Raster* raster = new Raster(dataPath, this); RasterLayer* rasterLayer = new RasterLayer(raster, this); //! [RasterLayerFile cpp new raster layer]
connect(rasterLayer, &RasterLayer::doneLoading, this, [this, rasterLayer](const Error& loadError) { if (!loadError.isEmpty()) return;
m_mapView->setViewpointCenterAsync(rasterLayer->fullExtent().center(), 80000); });
m_map->operationalLayers()->clear(); m_map->operationalLayers()->append(rasterLayer);}// [WriteFile Name=RasterLayerFile, Category=Analysis]// [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 RASTERLAYERFILE_H#define RASTERLAYERFILE_H
// Qt headers#include <QQuickItem>
namespace Esri::ArcGISRuntime{ class Map; class MapQuickView;}
class RasterLayerFile : public QQuickItem{ Q_OBJECT
public: explicit RasterLayerFile(QQuickItem* parent = nullptr); ~RasterLayerFile() override;
void componentComplete() override; static void init();
Q_INVOKABLE void createAndAddRasterLayer(QString dataPath);
private: Esri::ArcGISRuntime::Map* m_map = nullptr; Esri::ArcGISRuntime::MapQuickView* m_mapView = nullptr;};
#endif // RASTERLAYERFILE_H// [WriteFile Name=RasterLayerFile, Category=Analysis]// [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
RasterLayerFileSample { id: rootRectangle clip: true
readonly property var supportedFormats: ["img","I12","dt0","dt1","dt2","tc2","geotiff","tif", "tiff", "hr1","jpg","jpeg","jp2","ntf","png","i21","ovr"]
// add a mapView component MapView { anchors.fill: parent objectName: "mapView"
Component.onCompleted: { // Set the focus on MapView to initially enable keyboard navigation forceActiveFocus(); } }
Rectangle { visible: addButton.visible anchors.centerIn: addButton radius: 8 height: addButton.height + (16) width: addButton.width + (16) color: "lightgrey" border.color: "darkgrey" border.width: 2 opacity: 0.75 }
Button { id: addButton anchors { bottom: parent.bottom horizontalCenter: parent.horizontalCenter margins: 32 }
text: "Add Raster" onClicked: loader.open(); }
RasterLoader { id: loader anchors.fill: rootRectangle supportedExtensions: supportedFormats
onRasterFileChosen: url => createAndAddRasterLayer(url); }}// [WriteFile Name=RasterLayerFile, 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 Qt.labs.platform
Item { id: root signal rasterFileChosen(url url);
property var supportedExtensions: [] property alias folder: fileDialog.folder
function open() { fileDialog.open(); }
DropArea { anchors.fill: parent
onDropped: { if (!drop.hasText) return;
if (drop.urls.length !== 1) return;
if (!root.validateFileExtension(drop.urls[0])) return;
if (drop.proposedAction !== Qt.MoveAction && drop.proposedAction !== Qt.CopyAction) return;
drop.acceptProposedAction(); root.rasterFileChosen(drop.urls[0]); } }
FileDialog { id: fileDialog
onAccepted: { root.rasterFileChosen(fileDialog.file) } }
function validateFileExtension(filePath) { const extension = filePath.split('.').pop(); const idx = supportedExtensions.indexOf(extension);
return idx !== -1; }
onSupportedExtensionsChanged: { let nameFiltersString = "Raster files ("; for (let i = 0; i < supportedFormats.length; i++) nameFiltersString += "*." + supportedFormats[i] + " ";
nameFiltersString += ")"; fileDialog.nameFilters = nameFiltersString; }}// [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 "RasterLayerFile.h"
// ArcGIS Maps SDK headers#include "ArcGISRuntimeEnvironment.h"
// Qt headers#include <QCommandLineParser>#include <QDir>#include <QGuiApplication>#include <QQmlApplicationEngine>#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("RasterLayerFile"));
// 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 RasterLayerFile::init();
// Initialize application engine QQmlApplicationEngine engine;
// Add the import Path engine.addImportPath(QDir(QCoreApplication::applicationDirPath()).filePath("qml")); // Add the Extras path engine.addImportPath(QUOTE(ARCGIS_RUNTIME_IMPORT_PATH)); engine.addImportPath(QUOTE(ARCGIS_TOOLKIT_IMPORT_PATH));
// Set the source engine.load(QUrl("qrc:/Samples/Layers/RasterLayerFile/main.qml"));
return app.exec();}import QtQuick.Controlsimport Esri.Samples
ApplicationWindow { visible: true width: 800 height: 600
RasterLayerFile { anchors.fill: parent }}