Add custom dynamic entity data source

View on GitHubSample viewer app

Create a custom dynamic entity data source and display it using a dynamic entity layer.

screenshot

Use case

Developers can create a custom DynamicEntityDataSource to be able to visualize data from a variety of different feeds as dynamic entities using a DynamicEntityLayer. An example of this is in a mobile situational awareness app, where a custom DynamicEntityDataSource can be used to connect to peer-to-peer feeds in order to visualize real-time location tracks from teammates in the field.

How to use the sample

Run the sample to view the map and the dynamic entity layer displaying the latest observation from the custom data source. Tap on a dynamic entity to view its attributes in a callout.

How it works

Configure the custom data source:

  1. Create a custom data source implementation of a DynamicEntityDataSource.
  2. Override OnLoadAsync() to specify the DynamicEntityDataSourceInfo for a given unique entity ID field and a list of Field objects matching the fields in the data source.
  3. Override OnConnectAsync() to begin processing observations from the custom data source.
  4. Loop through the observations and deserialize each observation into a Point object and a QVariantMap containing the attributes.
  5. Use DynamicEntityDataSource::addObservation(mapPoint, attributes) to add each observation to the custom data source.

Configure the map view:

  1. Create a DynamicEntityLayer using the custom data source implementation.
  2. Update values in the layer's TrackDisplayProperties to customize the layer's appearance.
  3. Set up the layer's LabelDefinitions to display labels for each dynamic entity.
  4. Use MapView::identifyLayerAsync to select a dynamic entity and display the entity's attributes in a Callout.

Relevant API

  • DynamicEntity
  • DynamicEntityDataSource
  • DynamicEntityLayer
  • LabelDefinition
  • TrackDisplayProperties

About the data

This sample uses a .json file containing observations of marine vessels in the Pacific North West hosted on ArcGIS Online .

Additional information

In this sample, we iterate through features in a GeoJSON file to mimic messages coming from a real-time feed. You can create a custom dyamic entity data source to process any data that contains observations which can be translated into Point objects with associated QVariantMap attributes.

Tags

data, dynamic, entity, label, labeling, live, real-time, stream, track

Sample Code

AddCustomDynamicEntityDataSource.cppAddCustomDynamicEntityDataSource.cppAddCustomDynamicEntityDataSource.hCustomDataSource.cppCustomDataSource.hAddCustomDynamicEntityDataSource.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
// [WriteFile Name=AddCustomDynamicEntityDataSource, 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 "AddCustomDynamicEntityDataSource.h"

#include "CustomDataSource.h"
#include "ServiceTypes.h"
#include "DynamicEntityDataSource.h"
#include "DynamicEntityLayer.h"
#include "Map.h"
#include "MapQuickView.h"
#include "MapTypes.h"
#include "Viewpoint.h"
#include "LayerListModel.h"
#include "Basemap.h"
#include "ArcGISMapImageLayer.h"
#include "TrackDisplayProperties.h"
#include "SimpleLabelExpression.h"
#include "TextSymbol.h"
#include "LabelDefinition.h"
#include "ServiceTypes.h"
#include "LabelDefinitionListModel.h"
#include "CalloutData.h"
#include "IdentifyLayerResult.h"
#include "DynamicEntityObservation.h"
#include "DynamicEntity.h"
#include "AttributeListModel.h"
#include "DynamicEntityChangedInfo.h"
#include "DynamicEntityDataSourcePurgeOptions.h"

#include <QFuture>
#include <QtConcurrent/QtConcurrent>

using namespace Esri::ArcGISRuntime;

AddCustomDynamicEntityDataSource::AddCustomDynamicEntityDataSource(QObject* parent /* = nullptr */):
  QObject(parent),
  m_map(new Map(BasemapStyle::ArcGISOceans, this))
{
  // This is a custom class that inherits from DynamicEntityDataSource. It was written specifically for this sample.
  CustomDataSource* dataSource = new CustomDataSource(":/Samples/Layers/AddCustomDynamicEntityDataSource/AIS_MarineCadastre_SelectedVessels_CustomDataSource.json", "MMSI", 10, this);
  m_dynamicEntityLayer = new DynamicEntityLayer(dataSource, this);

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

  // Set previous observation properties
  m_dynamicEntityLayer->trackDisplayProperties()->setShowPreviousObservations(true);
  m_dynamicEntityLayer->trackDisplayProperties()->setShowTrackLine(true);
  m_dynamicEntityLayer->trackDisplayProperties()->setMaximumObservations(20);

  // Even when not displayed, previous observations are stored until purged
  m_dynamicEntityLayer->dataSource()->purgeOptions()->setMaximumObservationsPerTrack(20);

  // Show the vessel names as labels above the observation points
  SimpleLabelExpression* simpleLabelExpression = new SimpleLabelExpression("[VesselName]", this);
  TextSymbol* labelSymbol = new TextSymbol(this);
  labelSymbol->setColor(Qt::red);
  labelSymbol->setSize(12);
  LabelDefinition* labelDef = new LabelDefinition(simpleLabelExpression, labelSymbol, this);
  labelDef->setPlacement(LabelingPlacement::PointAboveCenter);
  m_dynamicEntityLayer->labelDefinitions()->append(labelDef);
  m_dynamicEntityLayer->setLabelsEnabled(true);
}

AddCustomDynamicEntityDataSource::~AddCustomDynamicEntityDataSource() = default;

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

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

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

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

  m_mapView->setViewpointAsync(Viewpoint(47.984, -123.657, 3e6));

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

  emit mapViewChanged();
}

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

  // Reseting m_tempParent gives it a new parent and cleans up any previously owned children like IdentifyLayerResult and DynamicEntity objects
  m_tempParent.reset(new QObject(this));

  m_mapView->identifyLayerAsync(m_map->operationalLayers()->first(), e.position(), 10, false, m_tempParent.get())
      .then(this, [this](IdentifyLayerResult* result)
  {
    if (!result || result->geoElements().empty())
      return;

    if (DynamicEntityObservation* observation = dynamic_cast<DynamicEntityObservation*>(result->geoElements().constFirst()))
    {
      DynamicEntity* dynamicEntity = observation->dynamicEntity();

      if (!dynamicEntity)
        return;

      // Get updated info about the dynamic entity every time it updates
      connect(dynamicEntity, &DynamicEntity::dynamicEntityChanged, this, [this](DynamicEntityChangedInfo* changedInfo)
      {
        // changedInfo will be cleaned up when m_tempParent is reset, but they will persist until then
        // a new DynamicEntityChangedInfo object will be created every time the dynamic entity updates
        // we can call deleteLater() to delete the object sooner when we leave this event loop
        changedInfo->deleteLater();

        // Get the current/most recent observation and populate a popup with its information
        DynamicEntityObservation* currentObservation = changedInfo->receivedObservation();
        QString calloutText =
        {
          "Vessel Name: " + currentObservation->attributes()->attributeValue("VesselName").toString() + "\n" +
          "Call Sign: " + currentObservation->attributes()->attributeValue("CallSign").toString() + "\n" +
          "Course over Ground: " + currentObservation->attributes()->attributeValue("COG").toString() + "º\n" +
          "Speed over Ground: " + currentObservation->attributes()->attributeValue("SOG").toString() + " knots"
        };
        m_mapView->calloutData()->setDetail(calloutText);
        m_mapView->calloutData()->setGeoElement(currentObservation);
        m_mapView->calloutData()->setVisible(true);
      });
    }
  });
}

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