Learn how to create and display a scene
This tutorial shows you how to create and display a scene
Prerequisites
Before starting this tutorial:
-
You need an ArcGIS Location Platform or ArcGIS Online account.
-
Your system meets the system requirements.
-
The ArcGIS Maps SDK for Qt, version 200.8.0 or later is installed.
-
The Qt 6.8.2 software development framework or later is installed.
Develop or Download
You have two options for completing this tutorial:
Option 1: Develop the code
To start the tutorial, complete the Display a scene tutorial. This creates a scene to display an area of the Santa Monica Mountains in a scene view.
Open a Qt Creator project
- Open the project you created by completing the Display a scene tutorial.
- Continue with the following instructions to display a scene from a web scene stored in ArcGIS.
Get the web scene item ID
You can use ArcGIS tools
- Go to the LA Trails and Parks web scene in the Scene Viewer in ArcGIS Online
ArcGIS Online is a GIS mapping, analytics, data hosting, and content management software as a service (SaaS) product. It includes applications, tools, APIs, and location services for users and developers. It is subscription-based and requires an ArcGIS Online account. . This web scene displays trails, trailheads and parks in the Santa Monica Mountains. - Make a note of the item ID at the end of the browser’s URL. The item ID should be 579f97b2f3b94d4a8e48a5f140a6639b.
Remove unneeded code
A web scene
-
In the project Sources folder of Qt Creator, open the Display_a_scene.cpp file.
-
Remove the unnecessary
#includestatements for:ArcGISTiledElevationSource.h,Camera.h,ElevationSourceListModel.h,MapTypes.h, andSurface.h. Removing these lines of code will keep you from having compiler warnings.Display_a_scene.cpp#include "Display_a_scene.h"#include "ArcGISTiledElevationSource.h"#include "Camera.h"#include "ElevationSourceListModel.h"#include "MapTypes.h"#include "Scene.h"#include "SceneQuickView.h"#include "Surface.h"#include <QUrl> -
Remove the comma after
QObject(parent)and then modify the constructor to remove initialization withBasemapStyleand theScene.Display_a_scene.cppDisplay_a_scene::Display_a_scene(QObject* parent /* = nullptr */):QObject(parent),m_scene(new Scene(BasemapStyle::ArcGISImagery, this)) -
Continuing to edit the
Display_a_sceneconstructor, remove the following lines since you will not be using anArGISTiledElevationSourcefrom aQUrland applying it to theScene.Display_a_scene.cppDisplay_a_scene::Display_a_scene(QObject* parent /* = nullptr */):QObject(parent){// create a new elevation source from Terrain3D rest serviceArcGISTiledElevationSource* elevationSource = new ArcGISTiledElevationSource(QUrl("https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer"), this);// add the elevation source to the scene to display elevationm_scene->baseSurface()->elevationSources()->append(elevationSource);setupScene(); -
Moving down in the same code file, in the
setSceneView(SceneQuickView* sceneView)method, remove the following lines for theCameraobject and all of its arguments.Display_a_scene.cpp// Set the view (created in QML)void Display_a_scene::setSceneView(SceneQuickView* sceneView){if (!sceneView || sceneView == m_sceneView){return;}m_sceneView = sceneView;m_sceneView->setArcGISScene(m_scene);constexpr double latitude = 33.909;constexpr double longitude = -118.805;constexpr double altitude = 5330.0;constexpr double heading = 355.0;constexpr double pitch = 72.0;constexpr double roll = 0.0;const Camera sceneCamera(latitude, longitude, altitude, heading, pitch, roll);m_sceneView->setViewpointCameraAndWait(sceneCamera);emit sceneViewChanged();} -
Again moving down in the same code file, now in the
setupScene()method, remove all of the existing code within the curly braces.Display_a_scene.cppvoid Display_a_scene::setupScene(){ArcGISTiledElevationSource* elevationSource = new ArcGISTiledElevationSource(QUrl("https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer"), this);Surface* elevationSurface = new Surface(this);elevationSurface->elevationSources()->append(elevationSource);elevationSurface->setElevationExaggeration(2.5);m_scene->setBaseSurface(elevationSurface);}Loading a scene
A scene is a collection of layers that are displayed in 3D. It is typically composed of a basemap layer, data layers, and 3D data. from a web sceneA web scene is a scene stored as a JSON object that defines properties such as the basemap layer, data layers, layer styles, and pop-up styles. Its JSON structure is defined by the web scene specification. usually requires less code and makes it easier to update a scene used by several apps. Furthermore, web scenes can provide a more consistent experience for your user.
Display a web scene using its portal item URL
You can display a web scene
-
Add code to the
setupScene()method to create the web sceneA web scene is a scene stored as a JSON object that defines properties such as the basemap layer, data layers, layer styles, and pop-up styles. Its JSON structure is defined by the web scene specification. portal URL. Concatenate the web scene’s unique item IDAn item ID is a unique identifier representing a single item stored, managed, and accessed in a portal, such as a web map, hosted layer, or file. and the URL for ArcGIS OnlineArcGIS Online is a GIS mapping, analytics, data hosting, and content management software as a service (SaaS) product. It includes applications, tools, APIs, and location services for users and developers. It is subscription-based and requires an ArcGIS Online account. .Display_a_scene.cppvoid Display_a_scene::setupScene(){// Create an item_id to reference an item at a web serverconst QString item_id("579f97b2f3b94d4a8e48a5f140a6639b");// Create a QUrl from the web scene server URL and the item_idconst QUrl portal_url(QString("https://arcgis.com/sharing/rest/content/items/" + item_id));} -
Create the
Sceneusing the web scene’s URL.Display_a_scene.cppvoid Display_a_scene::setupScene(){// Create an item_id to reference an item at a web serverconst QString item_id("579f97b2f3b94d4a8e48a5f140a6639b");// Create a QUrl from the web scene server URL and the item_idconst QUrl portal_url(QString("https://arcgis.com/sharing/rest/content/items/" + item_id));// Create the scene, referencing the QUrlm_scene = new Scene(portal_url, this);}
Set developer credentials
For the final steps of this tutorial, click the tab below that corresponds to the authentication type (API key authentication or User authentication) that you chose when you completed the Display a map tutorial.
Be sure to also provide the same authentication (API key or user authentication Client ID/Redirect URL) that you used for the Display a map tutorial.
Set the API Key
-
In the project Sources folder of Qt Creator, open the main.cpp file.
-
Modify the code to set the
accessTokenusing your API key access token (highlighted in yellow).main.cpp// 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{ArcGISRuntimeEnvironment::setApiKey(accessToken);} -
Save the main.cpp file.
Best Practice: The access token is stored directly in the code as a convenience for this tutorial. Do not store credentials directly in source code in a production environment.
Set path to the Qt Toolkit in the project
In the project Sources folder of Qt Creator, open the Display_a_scene.pro file and locate the following lines and update PATH_TO_TOOLKIT variable with the path of the toolkitcpp.pri file (highlighted in yellow) or the OAuth dialog will not appear to enter your user credentials. Then save the file.
ARCGIS_RUNTIME_VERSION = 200.8.2include($$PWD/arcgisruntime.pri)
# TODO: You need to replace the <path_to_toolkit_repo> with a valid location where the Qt Toolkit# resides on your system, example: C:/arcgis-maps-sdk-toolkit-qt/uitools/toolkitcpp/toolkitcpp.pri# This block determines whether you've cloned your toolkitPATH_TO_TOOLKIT = "<path_to_toolkit_repo>/uitools/toolkitcpp/toolkitcpp.pri"
exists($${PATH_TO_TOOLKIT}) { message("Toolkit found") DEFINES += TOOLKIT_FOUND
# include the toolkitcpp.pri, which contains all the toolkit resources include($${PATH_TO_TOOLKIT})
qtHaveModule(webenginequick) { QT += webenginequick }} else { message("Toolkit not found in provided path. Either set PATH_TO_TOOLKIT or use an API Key")}Set developer credentials in the solution
In the project Sources folder of Qt Creator, open the Display_a_scene.cpp file.
Set your values for the REDIRECT_URL and the CLIENT_ID strings (highlighted in yellow). Then save the file.
#ifdef TOOLKIT_FOUND
// Define the Redirect URL string obtained when creating the OAuth credentials. // TODO: You need to replace the "REDIRECT_URL" with your own valid string, // ex: "urn:ietf:wg:oauth:2.0:oob" const auto qStringRedirectUrl = QString{"REDIRECT_URL"};
// Define the URL of the portal to authenticate with. const QUrl qUrlPortal = QUrl{"https://www.arcgis.com/"};
// Define a unique identifier associated with an application registered with the // portal that assists with client/server OAuth authentication. // TODO: You need to replace the "CLIENT_ID" with your own valid string. const QString qStringClientId = QString{"CLIENT_ID"};
// Create a new OAuth user configuration using: the Url to mapping web service, the Client ID // string, and the Redirect Url string. auto* oAuthUserConfiguration = new OAuthUserConfiguration(qUrlPortal, qStringClientId, qStringRedirectUrl, this);
// Call the Toolkit's OAuthUserConfigurationManager static `addConfiguration` // method to use the OAuth dialog. This will tell the Authenticator to use OAuth for the provided configuration. Toolkit::OAuthUserConfigurationManager::addConfiguration(oAuthUserConfiguration);#endif // TOOLKIT_FOUNDPress Ctrl + R to run the app.
Your app displays the scene that you viewed earlier in the Scene Viewer.
Alternatively, you can download the tutorial solution, as follows.
Option 2: Download the solution
-
Click the
Download solutionlink underSolutionand unzip the file to a location on your machine. -
Open the .pro project file in Qt Creator.
Since the downloaded solution does not contain authentication credentials, you must set up authentication to create the developer credentials and add them to the project.
For the final steps of this tutorial, click the tab below that corresponds to the authentication type (API key authentication or User authentication) that you chose when you completed the Display a map tutorial.
Be sure to also provide the same authentication (API key or user authentication Client ID/Redirect URL) that you used for the Display a map tutorial.
Set developer credentials in the solution
Set the API Key
-
In the project Sources folder of Qt Creator, open the main.cpp file.
-
Modify the code to set the
accessTokenusing your API key access token (highlighted in yellow).main.cpp// 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{ArcGISRuntimeEnvironment::setApiKey(accessToken);} -
Save main.cpp file.
Best Practice: The access token is stored directly in the code as a convenience for this tutorial. Do not store credentials directly in source code in a production environment.
Set path to the Qt Toolkit in the project
In the project Sources folder of Qt Creator, open the Display_a_scene.pro file and locate the following lines and update PATH_TO_TOOLKIT variable with the path of the toolkitcpp.pri file (highlighted in yellow) or the OAuth dialog will not appear to enter your user credentials. Then save the file.
ARCGIS_RUNTIME_VERSION = 200.8.2include($$PWD/arcgisruntime.pri)
# TODO: You need to replace the <path_to_toolkit_repo> with a valid location where the Qt Toolkit# resides on your system, example: C:/arcgis-maps-sdk-toolkit-qt/uitools/toolkitcpp/toolkitcpp.pri# This block determines whether you've cloned your toolkitPATH_TO_TOOLKIT = "<path_to_toolkit_repo>/uitools/toolkitcpp/toolkitcpp.pri"
exists($${PATH_TO_TOOLKIT}) { message("Toolkit found") DEFINES += TOOLKIT_FOUND
# include the toolkitcpp.pri, which contains all the toolkit resources include($${PATH_TO_TOOLKIT})
qtHaveModule(webenginequick) { QT += webenginequick }} else { message("Toolkit not found in provided path. Either set PATH_TO_TOOLKIT or use an API Key")}Set developer credentials in the solution
-
In the project Sources folder of Qt Creator, open the Display_a_scene.cpp file.
-
Set your values for the REDIRECT_URL and the CLIENT_ID strings (highlighted in yellow). Then save the file.
#ifdef TOOLKIT_FOUND
// Define the Redirect URL string obtained when creating the OAuth credentials. // TODO: You need to replace the "REDIRECT_URL" with your own valid string, // ex: "urn:ietf:wg:oauth:2.0:oob" const auto qStringRedirectUrl = QString{"REDIRECT_URL"};
// Define the URL of the portal to authenticate with. const QUrl qUrlPortal = QUrl{"https://www.arcgis.com/"};
// Define a unique identifier associated with an application registered with the // portal that assists with client/server OAuth authentication. // TODO: You need to replace the "CLIENT_ID" with your own valid string. const QString qStringClientId = QString{"CLIENT_ID"};
// Create a new OAuth user configuration using: the Url to mapping web service, the Client ID // string, and the Redirect Url string. auto* oAuthUserConfiguration = new OAuthUserConfiguration(qUrlPortal, qStringClientId, qStringRedirectUrl, this);
// Call the Toolkit's OAuthUserConfigurationManager static `addConfiguration` // method to use the OAuth dialog. This will tell the Authenticator to use OAuth for the provided configuration. Toolkit::OAuthUserConfigurationManager::addConfiguration(oAuthUserConfiguration);#endif // TOOLKIT_FOUNDRun the solution
Press Ctrl + R to run the app.
Your app displays the scene that you viewed earlier in the Scene Viewer.
What’s Next?
Learn how to use additional API features, ArcGIS location services, and ArcGIS tools in these tutorials: