Edit the values of a KML ground overlay.

Use case
KML ground overlays are used for showing aerial imagery, symbology, or other images draped over a scene. Changing the geometry, rotation, and other attributes of a ground overlay after it has been loaded allows for live editing. For example, editing the geometry and opacity of a historical image draped over present day satellite imagery makes it possible to view change over time.
How to use the sample
Use the slider to adjust the opacity of the ground overlay.
How it works
- Create an
Envelopedefining the geometry of the overlay. - Create a
KmlIconusing aUrllinking to an image. - Create a
KmlGroundOverlayusing the envelope and icon. - Set the value of
KmlGroundOverlay::rotation. - Create a
KmlDatasetusing the ground overlay. - Create a
KmlLayerusing the dataset. - Add the KML layer to the scene.
- Listen for changes to the opacity slider and change the
KmlGroundOverlay::colorvalue appropriately.
Relevant API
- KmlDataset
- KmlGroundOverlay
- KmlIcon
- KmlLayer
About the data
The image used in this sample is an aerial view of the campus of the University of Oregon. This imagery was taken in 1944 by the U.S. Army Corps of Engineers. It is publicly available as Online Aerial Imagery hosted by Oregon University Library. It is also available as a Portal item on ArcGIS Online as University of Oregon Campus Aerial Imagery - 1944.
Tags
imagery, keyhole, KML, KMZ, OGC
Sample Code
// [WriteFile Name=EditKmlGroundOverlay, Category=EditData]// [Legal]// 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.// [Legal]
#ifdef PCH_BUILD#include "pch.hpp"#endif // PCH_BUILD
// sample headers#include "EditKmlGroundOverlay.h"
// ArcGIS Maps SDK headers#include "Camera.h"#include "Envelope.h"#include "Error.h"#include "KmlDataset.h"#include "KmlGroundOverlay.h"#include "KmlIcon.h"#include "KmlLayer.h"#include "LayerListModel.h"#include "MapTypes.h"#include "Point.h"#include "Scene.h"#include "SceneQuickView.h"#include "SpatialReference.h"
// Qt headers#include <QFuture>#include <QStandardPaths>#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
EditKmlGroundOverlay::EditKmlGroundOverlay(QObject* parent /* = nullptr */): QObject(parent), m_scene(new Scene(BasemapStyle::ArcGISImageryStandard, this)){ // Create Geometry const Envelope env(-123.066227926904, 44.04736963555683, -123.0796942287304, 44.03878298600624, SpatialReference(4326));
// Create KML Icon KmlIcon* kmlIcon = new KmlIcon(QUrl(defaultDataPath() + "/ArcGIS/Runtime/Data/raster/1944.jpg"), this);
// Create Ground Overlay m_groundOverlay = new KmlGroundOverlay(env, kmlIcon, this); m_groundOverlay->setRotation(-3.046024799346924);
// Create Dataset KmlDataset* kmlDataset = new KmlDataset(m_groundOverlay, this);
// Create Layer KmlLayer* kmlLayer = new KmlLayer(kmlDataset, this);
// Connect to know when the layer loads connect(kmlLayer, &KmlLayer::doneLoading, this, [this, env](const Error& e) { if (!e.isEmpty() || !m_sceneView) return;
const Camera camera(env.center(), 1250, 45, 60, 0); m_sceneView->setViewpointCameraAsync(camera); }); m_scene->operationalLayers()->append(kmlLayer);}
EditKmlGroundOverlay::~EditKmlGroundOverlay() = default;
void EditKmlGroundOverlay::init(){ // Register classes for QML qmlRegisterType<SceneQuickView>("Esri.Samples", 1, 0, "SceneView"); qmlRegisterType<EditKmlGroundOverlay>("Esri.Samples", 1, 0, "EditKmlGroundOverlaySample");}
void EditKmlGroundOverlay::setOpacity(int opacity){ if (!m_groundOverlay) return;
m_groundOverlay->setColor(QColor(0, 0, 0, opacity));}
SceneQuickView* EditKmlGroundOverlay::sceneView() const{ return m_sceneView;}
// Set the view (created in QML)void EditKmlGroundOverlay::setSceneView(SceneQuickView* sceneView){ if (!sceneView || sceneView == m_sceneView) return;
m_sceneView = sceneView; m_sceneView->setArcGISScene(m_scene);
emit sceneViewChanged();}// [WriteFile Name=EditKmlGroundOverlay, Category=EditData]// [Legal]// 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.// [Legal]
#ifndef EDITKMLGROUNDOVERLAY_H#define EDITKMLGROUNDOVERLAY_H
// Qt headers#include <QObject>
namespace Esri::ArcGISRuntime{class Scene;class SceneQuickView;class KmlGroundOverlay;}
Q_MOC_INCLUDE("SceneQuickView.h")
class EditKmlGroundOverlay : public QObject{ Q_OBJECT
Q_PROPERTY(Esri::ArcGISRuntime::SceneQuickView* sceneView READ sceneView WRITE setSceneView NOTIFY sceneViewChanged)
public: explicit EditKmlGroundOverlay(QObject* parent = nullptr); ~EditKmlGroundOverlay();
static void init(); Q_INVOKABLE void setOpacity(int opacity);
signals: void sceneViewChanged();
private: Esri::ArcGISRuntime::SceneQuickView* sceneView() const; void setSceneView(Esri::ArcGISRuntime::SceneQuickView* sceneView);
Esri::ArcGISRuntime::Scene* m_scene = nullptr; Esri::ArcGISRuntime::SceneQuickView* m_sceneView = nullptr; Esri::ArcGISRuntime::KmlGroundOverlay* m_groundOverlay = nullptr;};
#endif // EDITKMLGROUNDOVERLAY_H// [WriteFile Name=EditKmlGroundOverlay, Category=EditData]// [Legal]// 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.// [Legal]
import QtQuickimport QtQuick.Controlsimport Esri.Samples
Item {
SceneView { id: view anchors.fill: parent
Component.onCompleted: { // Set the focus on SceneView to initially enable keyboard navigation forceActiveFocus(); } }
// Declare the C++ instance which creates the scene etc. and supply the view EditKmlGroundOverlaySample { id: model sceneView: view }
Rectangle { anchors.fill: slider radius: 5 }
Slider { id: slider anchors { left: parent.left top: parent.top margins: 10 } from: 0 to: 255 value: 255 stepSize: 25 onValueChanged: { // modify the overlay's color/alpha value model.setOpacity(value); } }}// [Legal]// 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.// [Legal]
// sample headers#include "EditKmlGroundOverlay.h"
// ArcGIS Maps SDK headers#include "ArcGISRuntimeEnvironment.h"
// Qt headers#include <QDir>#include <QGuiApplication>#include <QQmlApplicationEngine>#include <QSurfaceFormat>
// 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);#if defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID) // Linux requires 3.2 OpenGL Context // in order to instance 3D symbols QSurfaceFormat fmt = QSurfaceFormat::defaultFormat(); fmt.setVersion(3, 2); QSurfaceFormat::setDefaultFormat(fmt);#endif
QGuiApplication app(argc, argv); app.setApplicationName(QString("EditKmlGroundOverlay"));
// 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 EditKmlGroundOverlay::init();
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
// Initialize application view QQmlApplicationEngine engine; // Add the import Path engine.addImportPath(QDir(QCoreApplication::applicationDirPath()).filePath("qml")); // Add the Runtime and Extras path engine.addImportPath(arcGISRuntimeImportPath);
// Set the source engine.load(QUrl("qrc:/Samples/EditData/EditKmlGroundOverlay/main.qml"));
return app.exec();}// Copyright 2019 ESRI//// All rights reserved under the copyright laws of the United States// and applicable international laws, treaties, and conventions.//// You may freely redistribute and use this sample code, with or// without modification, provided you include the original copyright// notice and use restrictions.//// See the Sample code usage restrictions document for further information.//
import QtQuick.Controlsimport Esri.Samples
ApplicationWindow { visible: true width: 800 height: 600
EditKmlGroundOverlay { anchors.fill: parent }}