Position graphics relative to a surface using different surface placement modes.

Use case
Depending on the use case, data might be displayed at an absolute height (e.g. flight data recorded with altitude information), at a relative height to the terrain (e.g. transmission lines positioned relative to the ground), at a relative height to objects in the scene (e.g. extruded polygons, integrated mesh scene layer), or draped directly onto the terrain (e.g. location markers, area boundaries).
How to use the sample
The application loads a scene showing four points that use individual surface placement modes (Absolute, Relative, Relative to Scene, and either Draped Billboarded or Draped Flat). Use the toggle to change the draped mode and the slider to dynamically adjust the Z value of the graphics. Explore the scene by zooming in/out and by panning around to observe the effects of the surface placement rules.
How it works
- Create a
GraphicsOverlayfor each placement mode, settingLayerSceneProperties::surfacePlacement:Absolute, position graphic using only its Z value.Relative, position graphic using its Z value plus the elevation of the surface.DrapedBillboarded, position graphic upright on the surface and always facing the camera, not using its z value.DrapedFlat, position graphic flat on the surface, not using its z value.RelativeToScene, position graphic using its Z value plus the altitude values of the scene.
- Add graphics to the graphics overlay,
GraphicsOverlay::graphics()::append(Graphic). - Add each graphics overlay to the scene view by calling
SceneView::graphicsOverlays()::append(overlay).
Relevant API
- Graphic
- GraphicsOverlay
- LayerSceneProperties::surfacePlacement
- SceneProperties
- Surface
About the data
The scene launches with a view of Brest, France. Four points are shown hovering with positions defined by each of the different surface placement modes.
Additional information
This sample uses an elevation service to add elevation/terrain to the scene. Graphics are positioned relative to that surface for the DrapedBillboarded, DrapedFlat, and Relative surface placement modes. It also uses a scene layer containing 3D models of buildings. Graphics are positioned relative to that scene layer for the RelativeToScene surface placement mode.
Tags
3D, absolute, altitude, draped, elevation, floating, relative, scenes, sea level, surface placement
Sample Code
// [WriteFile Name=SetSurfacePlacementMode, Category=Scenes]// [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 "SetSurfacePlacementMode.h"
// ArcGIS Maps SDK headers#include "ArcGISSceneLayer.h"#include "ArcGISTiledElevationSource.h"#include "Camera.h"#include "ElevationSourceListModel.h"#include "Graphic.h"#include "GraphicListModel.h"#include "GraphicsOverlay.h"#include "GraphicsOverlayListModel.h"#include "LayerListModel.h"#include "LayerSceneProperties.h"#include "MapTypes.h"#include "Point.h"#include "Scene.h"#include "SceneQuickView.h"#include "SceneViewTypes.h"#include "SimpleMarkerSymbol.h"#include "SpatialReference.h"#include "Surface.h"#include "SymbolTypes.h"#include "TextSymbol.h"#include "Viewpoint.h"
using namespace Esri::ArcGISRuntime;
SetSurfacePlacementMode::SetSurfacePlacementMode(QQuickItem* parent /* = nullptr */): QQuickItem(parent){}
SetSurfacePlacementMode::~SetSurfacePlacementMode() = default;
void SetSurfacePlacementMode::init(){ qmlRegisterType<SceneQuickView>("Esri.Samples", 1, 0, "SceneView"); qmlRegisterType<SetSurfacePlacementMode>("Esri.Samples", 1, 0, "SetSurfacePlacementModeSample");}
void SetSurfacePlacementMode::componentComplete(){ QQuickItem::componentComplete();
//! [Create Scene API snippet] // Create a scene with the imagery basemap and world elevation surface m_sceneView = findChild<SceneQuickView*>("sceneView"); Scene* scene = new Scene(BasemapStyle::ArcGISImageryStandard, this); Surface* surface = new Surface(this); surface->elevationSources()->append(new ArcGISTiledElevationSource(QUrl("https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer"), this)); scene->setBaseSurface(surface);
// Create scene layer from the Brest, France scene server. ArcGISSceneLayer* sceneLayer = new ArcGISSceneLayer(QUrl("https://tiles.arcgis.com/tiles/P3ePLMYs2RVChkJx/arcgis/rest/services/Buildings_Brest/SceneServer"), this); scene->operationalLayers()->append(sceneLayer); //! [Create Scene API snippet]
// set an initial viewpoint const Point initialViewPoint(-4.45968, 48.3889, 37.9922); const Camera camera(initialViewPoint, 329.91, 96.6632, 0); const Viewpoint viewpoint(initialViewPoint, camera); scene->setInitialViewpoint(viewpoint);
// set the scene to the scene view m_sceneView->setArcGISScene(scene);
// add graphics overlays and set surface placement addGraphicsOverlays();
// add graphics addGraphics();}
void SetSurfacePlacementMode::addGraphicsOverlays(){ // Graphics overlay with draped billboarded surface placement m_drapedBillboardedOverlay = new GraphicsOverlay(this); m_drapedBillboardedOverlay->setSceneProperties(LayerSceneProperties(SurfacePlacement::DrapedBillboarded)); m_sceneView->graphicsOverlays()->append(m_drapedBillboardedOverlay);
// Graphics overlay with draped flat surface placement m_drapedFlatOverlay = new GraphicsOverlay(this); m_drapedFlatOverlay->setSceneProperties(LayerSceneProperties(SurfacePlacement::DrapedFlat)); m_drapedFlatOverlay->setVisible(false); m_sceneView->graphicsOverlays()->append(m_drapedFlatOverlay);
// Graphics overlay with relative surface placement m_relativeOverlay = new GraphicsOverlay(this); m_relativeOverlay->setSceneProperties(LayerSceneProperties(SurfacePlacement::Relative)); m_sceneView->graphicsOverlays()->append(m_relativeOverlay);
// Graphics overlay with relative to scene surface placement m_relativeToSceneOverlay = new GraphicsOverlay(this); m_relativeToSceneOverlay->setSceneProperties(LayerSceneProperties(SurfacePlacement::RelativeToScene)); m_sceneView->graphicsOverlays()->append(m_relativeToSceneOverlay);
// Graphics overlay with absolute surface placement m_absoluteOverlay = new GraphicsOverlay(this); m_absoluteOverlay->setSceneProperties(LayerSceneProperties(SurfacePlacement::Absolute)); m_sceneView->graphicsOverlays()->append(m_absoluteOverlay);}
void SetSurfacePlacementMode::addGraphics(){ // create point for the scene related graphic with a z value of 70 const Point sceneRelatedPoint(-4.4610562, 48.3902727, 70, SpatialReference::wgs84());
// create point for the surface related graphics with z value of 70 const Point surfaceRelatedPoint(-4.4609257, 48.3903965 , 70, SpatialReference::wgs84());
// create simple marker symbol SimpleMarkerSymbol* simpleMarkerSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle::Triangle, QColor("red"), 20, this);
// create text symbols TextSymbol* drapedBillboardedText = new TextSymbol("DRAPED BILLBOARDED", QColor("blue"), 20, HorizontalAlignment::Left, VerticalAlignment::Middle, this); drapedBillboardedText->setOffsetX(20); TextSymbol* drapedFlatText = new TextSymbol("DRAPED FLAT", QColor("blue"), 20, HorizontalAlignment::Left, VerticalAlignment::Middle, this); drapedFlatText->setOffsetX(20); TextSymbol* relativeText = new TextSymbol("RELATIVE", QColor("blue"), 20, HorizontalAlignment::Left, VerticalAlignment::Middle, this); relativeText->setOffsetX(20); TextSymbol* relativeToSceneText = new TextSymbol("RELATIVE TO SCENE", QColor("blue"), 20, HorizontalAlignment::Right, VerticalAlignment::Middle, this); relativeToSceneText->setOffsetX(-20); TextSymbol* absoluteText = new TextSymbol("ABSOLUTE", QColor("blue"), 20, HorizontalAlignment::Left, VerticalAlignment::Middle, this); absoluteText->setOffsetX(20);
// add graphics to each overlay // Graphics will be draped on the surface of the scene and will always face the camera. m_drapedBillboardedOverlay->graphics()->append(new Graphic(surfaceRelatedPoint, simpleMarkerSymbol)); m_drapedBillboardedOverlay->graphics()->append(new Graphic(surfaceRelatedPoint, drapedBillboardedText));
// Graphics will be draped on the surface of the scene m_drapedFlatOverlay->graphics()->append(new Graphic(surfaceRelatedPoint, simpleMarkerSymbol)); m_drapedFlatOverlay->graphics()->append(new Graphic(surfaceRelatedPoint, drapedFlatText));
// Graphics will be placed at z value relative to the surface m_relativeOverlay->graphics()->append(new Graphic(surfaceRelatedPoint, simpleMarkerSymbol)); m_relativeOverlay->graphics()->append(new Graphic(surfaceRelatedPoint, relativeText));
// Graphics will be placed at z value relative to the scene m_relativeToSceneOverlay->graphics()->append(new Graphic(sceneRelatedPoint, simpleMarkerSymbol)); m_relativeToSceneOverlay->graphics()->append(new Graphic(sceneRelatedPoint, relativeToSceneText));
// Graphics will be placed at absolute z value m_absoluteOverlay->graphics()->append(new Graphic(surfaceRelatedPoint, simpleMarkerSymbol)); m_absoluteOverlay->graphics()->append(new Graphic(surfaceRelatedPoint, absoluteText));}
void SetSurfacePlacementMode::changeDrapedVisibility(){ m_drapedFlatOverlay->setVisible(!m_drapedFlatOverlay->isVisible()); m_drapedBillboardedOverlay->setVisible(!m_drapedBillboardedOverlay->isVisible());}
void SetSurfacePlacementMode::changeZValue(double zValue){ for (GraphicsOverlay* overlay : *m_sceneView->graphicsOverlays()) { for (Graphic* graphic : *overlay->graphics()) { // create new graphic with the same existing information but a new Z-value const Point graphicPoint{graphic->geometry()}; const Point point{graphicPoint.x(), graphicPoint.y(), zValue, graphicPoint.spatialReference()}; graphic->setGeometry(point); } }}// [WriteFile Name=SetSurfacePlacementMode, Category=Scenes]// [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 SETSURFACEPLACEMENTMODE_H#define SETSURFACEPLACEMENTMODE_H
// Qt headers#include <QQuickItem>
namespace Esri::ArcGISRuntime{ class SceneQuickView; class GraphicsOverlay;}
class SetSurfacePlacementMode : public QQuickItem{ Q_OBJECT
public: explicit SetSurfacePlacementMode(QQuickItem* parent = nullptr); ~SetSurfacePlacementMode() override;
void componentComplete() override; static void init(); Q_INVOKABLE void changeDrapedVisibility(); Q_INVOKABLE void changeZValue(double zValue);
private: void addGraphics(); void addGraphicsOverlays();
private: Esri::ArcGISRuntime::SceneQuickView* m_sceneView = nullptr; Esri::ArcGISRuntime::GraphicsOverlay* m_drapedFlatOverlay = nullptr; Esri::ArcGISRuntime::GraphicsOverlay* m_drapedBillboardedOverlay = nullptr; Esri::ArcGISRuntime::GraphicsOverlay* m_relativeOverlay = nullptr; Esri::ArcGISRuntime::GraphicsOverlay* m_relativeToSceneOverlay = nullptr; Esri::ArcGISRuntime::GraphicsOverlay* m_absoluteOverlay = nullptr;};
#endif // SETSURFACEPLACEMENTMODE_H// [WriteFile Name=SetSurfacePlacementMode, Category=Scenes]// [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.Samplesimport QtQuick.Layouts
SetSurfacePlacementModeSample { width: 800 height: 600
// Create SceneView here, and create its Scene and GraphicsOverlay in C++ code //! [add a sceneView component] SceneView { objectName: "sceneView" anchors.fill: parent
Component.onCompleted: { // Set the focus on SceneView to initially enable keyboard navigation forceActiveFocus(); } }
Rectangle { anchors { top: parent.top left: parent.left margins: 5 } width: childrenRect.width height: childrenRect.height color: "#000000" opacity: .8 radius: 5
MouseArea { anchors.fill: parent onClicked: mouse => mouse.accepted = true onWheel: wheel => wheel.accepted = true }
ColumnLayout { Text { Layout.margins: 2 Layout.alignment: Qt.AlignHCenter text: qsTr("Draped mode") color: "white" }
Switch { id: surfacePlacementMode Layout.alignment: Qt.AlignHCenter Layout.margins: 2
onCheckedChanged: changeDrapedVisibility(); } } }
Rectangle { anchors { top: parent.top right: parent.right margins: 5 } width: childrenRect.width height: childrenRect.height color: "#000000" opacity: .8 radius: 5
MouseArea { anchors.fill: parent onClicked: mouse => mouse.accepted = true onWheel: wheel => wheel.accepted = true }
ColumnLayout { Text { id: zValueSliderLabel text: qsTr("Z-Value") color: "white" Layout.margins: 2 Layout.alignment: Qt.AlignHCenter }
Slider { id: zValueSlider from: 0 to: 140 value: 70 Layout.alignment: Qt.AlignHCenter Layout.margins: 2 orientation: Qt.Vertical
onMoved: changeZValue(value);
// Custom slider handle that displays the current value handle: Item { x: parent.leftPadding + parent.availableWidth / 2 - headingHandleNub.width / 2 y: parent.topPadding + parent.visualPosition * (parent.availableHeight - headingHandleNub.height)
Rectangle { id: headingHandleNub color: headingHandleRect.color radius: width * 0.5 width: 20 height: width } Rectangle { id: headingHandleRect height: childrenRect.height width: childrenRect.width radius: 3 x: headingHandleNub.x - width y: headingHandleNub.y - height / 2 + headingHandleNub.height / 2
Text { id: headingValue font.pixelSize: 14 padding: 3 horizontalAlignment: Qt.AlignHCenter verticalAlignment: Qt.AlignVCenter text: (zValueSlider.value).toFixed(0) color: "white" } } } } } } //! [add a sceneView component]}// [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 "SetSurfacePlacementMode.h"
// ArcGIS Maps SDK headers#include "ArcGISRuntimeEnvironment.h"
// Qt headers#include <QCommandLineParser>#include <QDir>#include <QGuiApplication>#include <QQmlEngine>#include <QQuickView>#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("SurfacePlacement"));
// 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 SetSurfacePlacementMode::init();
/* Leaving here for purpose of snippet //! [Register the scene view for QML] qmlRegisterType<SceneQuickView>("Esri.Samples", 1, 0, "SceneView"); //! [Register the scene view for QML] */
// 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/Scenes/SetSurfacePlacementMode/SetSurfacePlacementMode.qml"));
view.show();
return app.exec();}