Create simple geometry types.

Use case
Geometries are used to represent real world features as vector GIS data. Points are used to mark specific XY locations, such as landmarks and other points of interest. Polylines are made up of 2 or more XY vertices and can be used to mark roads, flight paths, or boundaries. Polygons are made up of 3 or more XY vertices and can be used to represent a lake, country, or a park. Geometries can be stored as features in a database, displayed as Graphics in a map, or used for performing spatial analysis with GeometryEngine or a GeoprocessingTask.
How to use the sample
Pan and zoom freely to see the different types of geometries placed onto the map.
How it works
- Use the constructors for the various simple
Geometrytypes includingPoint,Polyline,Multipoint,Polygon, andEnvelope. - To display the geometry, create a
Graphicpassing in the geometry, and aSymbolappropriate for the geometry type. - Add the graphic to a graphics overlay and add the overlay to a map view.
Relevant API
- Envelope
- Multipoint
- Point
- PointCollection
- Polygon
- Polyline
Additional information
A geometry made of multiple points usually takes a PointCollection as an argument or is created through a builder.
Tags
area, boundary, line, marker, path, shape
Sample Code
// [WriteFile Name=CreateGeometries, Category=Geometry]// [Legal]// Copyright 2018 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 "CreateGeometries.h"
// ArcGIS Maps SDK headers#include "Envelope.h"#include "Error.h"#include "Geometry.h"#include "Graphic.h"#include "GraphicListModel.h"#include "GraphicsOverlay.h"#include "GraphicsOverlayListModel.h"#include "Map.h"#include "MapQuickView.h"#include "MapTypes.h"#include "MultipointBuilder.h"#include "Point.h"#include "PointCollection.h"#include "PolygonBuilder.h"#include "PolylineBuilder.h"#include "SimpleFillSymbol.h"#include "SimpleLineSymbol.h"#include "SimpleMarkerSymbol.h"#include "SpatialReference.h"#include "SymbolTypes.h"
// Qt headers#include <QFuture>
using namespace Esri::ArcGISRuntime;
CreateGeometries::CreateGeometries(QQuickItem* parent /* = nullptr */): QQuickItem(parent){}
void CreateGeometries::init(){ // Register the map view for QML qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView"); qmlRegisterType<CreateGeometries>("Esri.Samples", 1, 0, "CreateGeometriesSample");}
void CreateGeometries::componentComplete(){ QQuickItem::componentComplete();
// find QML MapView component m_mapView = findChild<MapQuickView*>("mapView");
// Create a map using the topographic basemap m_map = new Map(BasemapStyle::ArcGISTopographic, this);
// Add a Graphics Overlay to the map view m_graphicsOverlay = new GraphicsOverlay(this); m_mapView->graphicsOverlays()->append(m_graphicsOverlay);
// Add graphics to the overlay once the map loads connect(m_map, &Map::doneLoading, this, [this](const Error& e) { if (!e.isEmpty()) return;
addGraphics(); });
// Set map to map view m_mapView->setMap(m_map);}
// function to add Graphics to a Graphics Overlayvoid CreateGeometries::addGraphics(){ // Create a simple fill symbol - used to render a polygon covering Colorado. SimpleFillSymbol* simpleFillSymbol = new SimpleFillSymbol(SimpleFillSymbolStyle::Cross, QColor("blue"), this);
// Create a simple line symbol - used to render a polyline border between California and Nevada. SimpleLineSymbol* simpleLineSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle::Solid, QColor("blue"), 3.0f /*width*/, this);
// Create a simple marker symbol - used to render multi-points for various state capital locations in the Western United States. SimpleMarkerSymbol* simpleMarkerSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle::Triangle, QColor("blue"), 14.0f /*size*/, this);
// Create a simple marker symbol - used to render a map point where the Esri headquarters is located. SimpleMarkerSymbol* simpleMarkerSymbol2 = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle::Diamond, QColor("red"), 18.0f /*size*/, this);
// Add a graphic to the graphic collection - polygon with a simple fill symbol. m_graphicsOverlay->graphics()->append(new Graphic(createPolygon(), simpleFillSymbol, this));
// Add a graphic to the graphic collection - polyline with a simple line symbol. m_graphicsOverlay->graphics()->append(new Graphic(createPolyline(), simpleLineSymbol, this));
// Add a graphic to the graphic collection - map point with a simple marker symbol. m_graphicsOverlay->graphics()->append(new Graphic(createPoint(), simpleMarkerSymbol2, this));
// Add a graphic to the graphic collection - multi-point with a simple marker symbol. m_graphicsOverlay->graphics()->append(new Graphic(createMultipoint(), simpleMarkerSymbol, this));
// Zoom to the extent of an envelope with some padding (10 device-independent pixels). m_mapView->setViewpointGeometryAsync(createEnvelope(), 10);}
// Return an envelope that covers the extent of the western United States.Geometry CreateGeometries::createEnvelope() const{ constexpr double xMin = -123.0; constexpr double yMin = 33.5; constexpr double xMax = -101.0; constexpr double yMax = 48.0; const SpatialReference spatialRef(4326);
return Envelope(xMin, yMin, xMax, yMax, spatialRef);}
Geometry CreateGeometries::createPolygon() const{ // Create a polygon builder const SpatialReference spatialRef(4326); PolygonBuilder polygonBuilder(spatialRef);
// add points to the builder that approximates the boundary of Colorado. polygonBuilder.addPoint(-109.048, 40.998); polygonBuilder.addPoint(-102.047, 40.998); polygonBuilder.addPoint(-102.037, 36.989); polygonBuilder.addPoint(-109.048, 36.998);
// Return the geometry. return polygonBuilder.toGeometry();}
Geometry CreateGeometries::createPolyline() const{ // Create a polyline builder const SpatialReference spatialRef(4326); PolylineBuilder polylineBuilder(spatialRef);
// add points to the builder that approximates the border between California and Nevada. polylineBuilder.addPoint(-119.992, 41.989); polylineBuilder.addPoint(-119.994, 38.994); polylineBuilder.addPoint(-114.620, 35.0);
// Return the geometry. return polylineBuilder.toGeometry();}
Geometry CreateGeometries::createPoint() const{ constexpr double x = -117.195800; constexpr double y = 34.056295; const SpatialReference spatialRef(4326);
// Return a map point where the Esri headquarters is located. return Point(x, y, spatialRef);}
Geometry CreateGeometries::createMultipoint() const{ // Create a polygon builder const SpatialReference spatialRef(4326); MultipointBuilder multipointBuilder(spatialRef);
// add points to the builder representing various state capital locations in the Western United States. multipointBuilder.points()->addPoint(-121.491014, 38.579065); // - Sacramento, CA multipointBuilder.points()->addPoint(-122.891366, 47.039231); // - Olympia, WA multipointBuilder.points()->addPoint(-123.043814, 44.93326); // - Salem, OR multipointBuilder.points()->addPoint(-119.766999, 39.164885); // - Carson City, NV
// Return the geometry. return multipointBuilder.toGeometry();}// [WriteFile Name=CreateGeometries, Category=Geometry]// [Legal]// Copyright 2018 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 CREATEGEOMETRIES_H#define CREATEGEOMETRIES_H
// ArcGIS Maps SDK headers#include "Geometry.h"
// Qt headers#include <QQuickItem>
namespace Esri::ArcGISRuntime{class Map;class MapQuickView;class GraphicsOverlay;}
class CreateGeometries : public QQuickItem{ Q_OBJECT
public: explicit CreateGeometries(QQuickItem* parent = nullptr); ~CreateGeometries() override = default;
void componentComplete() override; static void init();
private: Esri::ArcGISRuntime::Map* m_map = nullptr; Esri::ArcGISRuntime::MapQuickView* m_mapView = nullptr; Esri::ArcGISRuntime::GraphicsOverlay* m_graphicsOverlay = nullptr;
private: void addGraphics(); Esri::ArcGISRuntime::Geometry createEnvelope() const; Esri::ArcGISRuntime::Geometry createPoint() const; Esri::ArcGISRuntime::Geometry createMultipoint() const; Esri::ArcGISRuntime::Geometry createPolyline() const; Esri::ArcGISRuntime::Geometry createPolygon() const;};
#endif // CREATEGEOMETRIES_H// [WriteFile Name=CreateGeometries, Category=Geometry]// [Legal]// Copyright 2018 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
CreateGeometriesSample { 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(); } }}// [Legal]// Copyright 2018 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 "CreateGeometries.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("CreateGeometries"));
// 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 CreateGeometries::init();
// Initialize application view QQuickView view; view.setResizeMode(QQuickView::SizeRootObjectToView);
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 import Path view.engine()->addImportPath(QDir(QCoreApplication::applicationDirPath()).filePath("qml")); // Add the Runtime and Extras path view.engine()->addImportPath(arcGISRuntimeImportPath);
// Set the source view.setSource(QUrl("qrc:/Samples/Geometry/CreateGeometries/CreateGeometries.qml"));
view.show();
return app.exec();}