Add dynamic entity layer

View on GitHubSample viewer app

Display data from an ArcGIS stream service using a dynamic entity layer.

screenshot

Use case

A stream service is a type of service provided by ArcGIS Velocity and GeoEvent Server that allows clients to receive a stream of data observations via a web socket. ArcGIS Maps SDK for Qt allows you to connect to a stream service and manage the information as dynamic entities and display them in a dynamic entity layer. Displaying information from feeds such as a stream service is important in applications like dashboards where users need to visualize and track updates of real-world objects in real-time.

Use ArcGISStreamService to manage the connection to the stream service and purge options to manage how much data is stored and maintained by the application. The dynamic entity layer will display the latest received observation, and you can set track display properties to determine how to display historical information for each dynamic entity. This includes the number of previous observations to show, whether to display track lines in-between previous observations, and setting renderers.

How to use the sample

Use the controls to connect to or disconnect from the stream service, modify display properties in the dynamic entity layer, and purge all observations from the application.

How it works

  1. Create an ArcGISStreamService with a URL
  2. Create and configure an ArcGISStreamServiceFilter then set it to the stream service to limit the amount of data coming from the server.
  3. Configure the DynamicEntityDataSourcePurgeOptions to manage when entities are removed from the application's cache
  4. Add a Renderer to the layer to customize the appearance of the latest dynamic entity observations
  5. Update the values in the layer's TrackDisplayProperties to customize the appearance of previous observations

Relevant API

  • ArcGISStreamService
  • ArcGISStreamServiceFilter
  • ConnectionStatus
  • DynamicEntity
  • DynamicEntityLayer
  • DynamicEntityPurgeOptions
  • TrackDisplayProperties

About the data

This sample uses a stream service that simulates live data coming from snowplows near Sandy, Utah. There are multiple vehicle types and multiple agencies operating the snowplows.

Additional information

More information about dynamic entities can be found in the [guide documentation](link goes here).

Tags

data, dynamic, entity, live, purge, real-time, service, stream, track

Sample Code

AddDynamicEntityLayer.cppAddDynamicEntityLayer.cppAddDynamicEntityLayer.hAddDynamicEntityLayer.qml
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// [WriteFile Name=AddDynamicEntityLayer, Category=Layers]
// [Legal]
// Copyright 2023 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

#include "AddDynamicEntityLayer.h"
#include "ArcGISStreamService.h"
#include "ArcGISStreamServiceFilter.h"
#include "CalloutData.h"
#include "DynamicEntity.h"
#include "DynamicEntityChangedInfo.h"
#include "DynamicEntityDataSourcePurgeOptions.h"
#include "DynamicEntityLayer.h"
#include "DynamicEntityObservation.h"
#include "Envelope.h"
#include "Graphic.h"
#include "GraphicListModel.h"
#include "GraphicsOverlay.h"
#include "GraphicsOverlayListModel.h"
#include "IdentifyLayerResult.h"
#include "LayerListModel.h"
#include "Map.h"
#include "MapQuickView.h"
#include "MapTypes.h"
#include "Point.h"
#include "RealTimeTypes.h"
#include "SimpleLineSymbol.h"
#include "SimpleMarkerSymbol.h"
#include "SimpleRenderer.h"
#include "SpatialReference.h"
#include "SymbolTypes.h"
#include "TrackDisplayProperties.h"
#include "UniqueValue.h"
#include "UniqueValueRenderer.h"
#include "Viewpoint.h"

#include <QFuture>

using namespace Esri::ArcGISRuntime;

namespace
{
// This envelope is a limited region around Sandy, Utah. It will be the extent used by the `DynamicEntityFilter`.
const Envelope utahSandyEnvelope(
    Point(-112.110052, 40.718083, SpatialReference::wgs84()),
    Point(-111.814782, 40.535247, SpatialReference::wgs84()));
}

AddDynamicEntityLayer::AddDynamicEntityLayer(QObject* parent /* = nullptr */):
  QObject(parent),
  m_map(new Map(BasemapStyle::ArcGISDarkGray, this))
{
  // Create a dynamic entity data source from a given URL
  const QUrl streamServiceUrl("https://realtimegis2016.esri.com:6443/arcgis/rest/services/SandyVehicles/StreamServer");
  m_dynamicEntityDataSource = new ArcGISStreamService(streamServiceUrl, this);

  // Create and set an ArcGISStreamServiceFilter to filter what data is received from the server
  ArcGISStreamServiceFilter* streamServiceFilter = new ArcGISStreamServiceFilter(this);
  streamServiceFilter->setGeometry(utahSandyEnvelope);
  streamServiceFilter->setWhereClause("speed > 0");
  m_dynamicEntityDataSource->setFilter(streamServiceFilter);

  // Set purge options to manage when entities are removed from the cache
  m_dynamicEntityDataSource->purgeOptions()->setMaximumDuration(300.0 /*seconds*/);

  // Handle signals emitted when connection status changes to update the UI
  connect(m_dynamicEntityDataSource, &ArcGISStreamService::connectionStatusChanged, this, &AddDynamicEntityLayer::connectionStatusChanged);

  // Create a dynamic entity layer to display on the map and set the properties
  m_dynamicEntityLayer = new DynamicEntityLayer(m_dynamicEntityDataSource, this);

  // Set the track display properties to change what is displayed to the user
  m_dynamicEntityLayer->trackDisplayProperties()->setShowTrackLine(true);
  m_dynamicEntityLayer->trackDisplayProperties()->setShowPreviousObservations(true);

  // Create renderers for observations and their tracklines to change how they are styled

  // Create a unique value renderer for the latest observations
  QList<UniqueValue*> entityValues;
  entityValues.append(new UniqueValue("","", {3}, new SimpleMarkerSymbol(SimpleMarkerSymbolStyle::Circle, Qt::magenta, 8, this), this));
  entityValues.append(new UniqueValue("","", {4}, new SimpleMarkerSymbol(SimpleMarkerSymbolStyle::Circle, Qt::green, 8, this), this));

  UniqueValueRenderer* entityRenderer = new UniqueValueRenderer("", new SimpleMarkerSymbol(SimpleMarkerSymbolStyle::Circle, Qt::blue, 8, this), {"agency"}, entityValues, this);
  m_dynamicEntityLayer->setRenderer(entityRenderer);

  // Create a unique value renderer for the previous observations
  QList<UniqueValue*> previousObservationValues;

  previousObservationValues.append(new UniqueValue("","", {3}, new SimpleMarkerSymbol(SimpleMarkerSymbolStyle::Circle, Qt::magenta, 3, this), this));
  previousObservationValues.append(new UniqueValue("","", {4}, new SimpleMarkerSymbol(SimpleMarkerSymbolStyle::Circle, Qt::green, 3, this), this));

  UniqueValueRenderer* trackRenderer = new UniqueValueRenderer("", new SimpleMarkerSymbol(SimpleMarkerSymbolStyle::Circle, Qt::blue, 3, this), {"agency"}, previousObservationValues);
  m_dynamicEntityLayer->trackDisplayProperties()->setPreviousObservationRenderer(trackRenderer);

  // Use a simple renderer to change the style of the trackline
  m_dynamicEntityLayer->trackDisplayProperties()->setTrackLineRenderer(new SimpleRenderer(new SimpleLineSymbol(SimpleLineSymbolStyle::Solid, Qt::lightGray, 2, this)));

  m_map->operationalLayers()->append(m_dynamicEntityLayer);
}

AddDynamicEntityLayer::~AddDynamicEntityLayer() = default;

void AddDynamicEntityLayer::init()
{
  // Register the map view for QML
  qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView");
  qmlRegisterType<AddDynamicEntityLayer>("Esri.Samples", 1, 0, "AddDynamicEntityLayerSample");
}

MapQuickView* AddDynamicEntityLayer::mapView() const
{
  return m_mapView;
}

// Set the view (created in QML)
void AddDynamicEntityLayer::setMapView(MapQuickView* mapView)
{
  if (!mapView || mapView == m_mapView)
    return;

  m_mapView = mapView;
  m_mapView->setMap(m_map);

  // Create and show a dashed line around the filter area
  GraphicsOverlay* borderOverlay = new GraphicsOverlay(this);
  borderOverlay->graphics()->append(new Graphic(utahSandyEnvelope, new SimpleLineSymbol(SimpleLineSymbolStyle::Dash, Qt::red, 2, this), this));
  m_mapView->graphicsOverlays()->append(borderOverlay);

  // Set the initial viewpoint to this area
  m_mapView->setViewpointAndWait(Viewpoint(utahSandyEnvelope));

  // Create a slot to listen for mouse clicks
  connect(m_mapView, &MapQuickView::mouseClicked, this, &AddDynamicEntityLayer::identifyLayerAtMouseClick);

  emit mapViewChanged();
}

void AddDynamicEntityLayer::setObservationsPerTrack(int observationsPerTrack)
{
  // Update the number of entity observations displayed using the value from the UI slider
  m_dynamicEntityLayer->trackDisplayProperties()->setMaximumObservations(observationsPerTrack);
}

void AddDynamicEntityLayer::showTrackLines(bool showTrackLines)
{
  // Show or hide the lines that connect previous observations
  m_dynamicEntityLayer->trackDisplayProperties()->setShowTrackLine(showTrackLines);
}

void AddDynamicEntityLayer::showPreviousObservations(bool showPreviousObservations)
{
  // Show or hide previous observations (if maximum observations is greater than 1)
  m_dynamicEntityLayer->trackDisplayProperties()->setShowPreviousObservations(showPreviousObservations);
}

QString AddDynamicEntityLayer::connectionStatus() const
{
  // Return the current dynamic entity data source connection status as a string to display in the UI
  switch (m_dynamicEntityDataSource->connectionStatus())
  {
    case ConnectionStatus::Disconnected:
      return "Disconnected";
    case ConnectionStatus::Connecting:
      return "Connecting";
    case ConnectionStatus::Connected:
      return "Connected";
    case ConnectionStatus::Failed:
      return "Failed";
    default:
      return "Unknown";
  }
}

void AddDynamicEntityLayer::enableDisableConnection()
{
  // Handle the UI connection switch and disable or enable connection
  switch (m_dynamicEntityDataSource->connectionStatus())
  {
    case ConnectionStatus::Disconnected:
    {
      auto future = m_dynamicEntityDataSource->connectDataSourceAsync();
      Q_UNUSED(future)
      break;
    }
    case ConnectionStatus::Connecting:
      // Do nothing and allow data source to finish connecting
      break;
    case ConnectionStatus::Connected:
    {
      auto future = m_dynamicEntityDataSource->disconnectDataSourceAsync();
      Q_UNUSED(future)
      break;
    }
    case ConnectionStatus::Failed:
      qWarning() << "Unable to connect to dynamic entity data source";
      break;
    default:
      break;
  }
}

void AddDynamicEntityLayer::purgeAllObservations()
{
  // Remove all current observations from the cache
  auto future = m_dynamicEntityDataSource->purgeAllAsync();
  Q_UNUSED(future)
}

void AddDynamicEntityLayer::identifyLayerAtMouseClick(const QMouseEvent& e)
{
  // Hide the callout (if it is already hidden this will do nothing)
  m_mapView->calloutData()->setVisible(false);

  m_mapView->identifyLayerAsync(m_dynamicEntityLayer, e.position(), 5, false, this)
      .then(this, [this](IdentifyLayerResult* result)
  {
    if (!result || result->geoElements().empty())
    {
      return;
    }

    if (DynamicEntityObservation* observation = dynamic_cast<DynamicEntityObservation*>(result->geoElements().constFirst()); observation)
    {
      DynamicEntity* dynamicEntity = observation->dynamicEntity();
      if (!dynamicEntity)
      {
        return;
      }
      m_mapView->calloutData()->setGeoElement(dynamicEntity);
      // Create a arcade expression for title to display the dynamic entity's attributes in the callout.
      const QString titleExpression = "concatenate($feature.vehiclename, \": \", $feature.speed, \" mph\")";
      m_mapView->calloutData()->setTitleExpression(titleExpression);

      // Create a arcade expression for detail to display the dynamic entity's attributes in the callout.
      const QString detailExpression = "concatenate(Round($feature.point_x,6), \",\", Round($feature.point_y,6),\" Heading: \",$feature.heading,\"°\")";
      m_mapView->calloutData()->setDetailExpression(detailExpression);

      // Show the callout when the title is available.
      connect(m_mapView->calloutData(), &CalloutData::titleChanged, this, [this]()
      {
        m_mapView->calloutData()->setVisible(true);
      }, Qt::SingleShotConnection);
    }
  });
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.