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.

AddCustomDynamicEntityDataSource

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.

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 Geometry object and a Map<String, Object> containing the attributes.
  5. Use DynamicEntityDataSource.addObservation(geometry, 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.

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 dynamic entity data source to process any data that contains observations which can be translated into Geometry objects with associated Map<String, Object> attributes.

Tags

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

Sample Code

AddCustomDynamicEntityDataSourceSample.javaAddCustomDynamicEntityDataSourceSample.javaSimulatedDataSource.java
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
/*
 * 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.
 */

package com.esri.samples.add_custom_dynamic_entity_data_source;

import java.io.File;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.arcgisservices.LabelDefinition;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.SpatialReferences;
import com.esri.arcgisruntime.layers.DynamicEntityLayer;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.BasemapStyle;
import com.esri.arcgisruntime.mapping.Viewpoint;
import com.esri.arcgisruntime.mapping.labeling.SimpleLabelExpression;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.realtime.ConnectionStatus;
import com.esri.arcgisruntime.symbology.TextSymbol;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class AddCustomDynamicEntityDataSourceSample extends Application {

  private MapView mapView;

  private SimulatedDataSource dynamicEntityDataSource;

  @Override
  public void start(Stage stage) {
    StackPane stackPane = new StackPane();
    Scene scene = new Scene(stackPane);

    // set title, size, and add scene to stage
    stage.setTitle("Add custom dynamic entity data source");
    stage.setWidth(800);
    stage.setHeight(700);
    stage.setScene(scene);
    stage.show();

    // authentication with an API key or named user is required to access basemaps and other location services
    String yourAPIKey = System.getProperty("apiKey");
    ArcGISRuntimeEnvironment.setApiKey(yourAPIKey);

    // create a new map with the oceans basemap style
    ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_OCEANS);

    // create a map view and set the map to it
    mapView = new MapView();
    mapView.setMap(map);

    // set the initial viewpoint
    mapView.setViewpoint(new Viewpoint(new Point(-123.657, 47.984, SpatialReferences.getWgs84()), 3e6));

    // a JSON file with observations for use as the custom data source
    var resource = new File(System.getProperty("data.dir"), "./samples-data/real_time/AIS_MarineCadastre_SelectedVessels_CustomDataSource.json").getPath();

    // create a custom data source implementation of a DynamicEntityDataSource with a data source,
    // an entity id field name (a unique identifier for each entity), and an update delay
    dynamicEntityDataSource = new SimulatedDataSource(resource, "MMSI", 10);

    dynamicEntityDataSource.connectionStatusProperty().addListener((property, oldValue, newValue) -> {
      if (newValue == ConnectionStatus.FAILED) {
        System.err.println("The connection failed");
      }
    });

    dynamicEntityDataSource.connectionErrorProperty().addListener((property, oldValue, newValue) -> {
      if (newValue != null) {
        System.err.println("The connection failed: " + newValue.getMessage());
      }
    });

    // create the dynamic entity layer using the custom data source
    var dynamicEntityLayer = new DynamicEntityLayer(dynamicEntityDataSource);

    // set up the track display properties
    setupTrackDisplayProperties(dynamicEntityLayer);

    // set up the dynamic entity labeling
    setupLabeling(dynamicEntityLayer);

    // add the dynamic entity layer to the map
    map.getOperationalLayers().add(dynamicEntityLayer);

    // add the map view to the stack pane
    stackPane.getChildren().addAll(mapView);
  }

  /**
   * Set up the track display properties, these properties will be used to configure the appearance of the track line
   * and previous observations.
   *
   * @param layer the DynamicEntityLayer to be configured
   */
  private void setupTrackDisplayProperties(DynamicEntityLayer layer) {
    var trackDisplayProperties = layer.getTrackDisplayProperties();
    trackDisplayProperties.setShowPreviousObservations(true);
    trackDisplayProperties.setShowTrackLine(true);
    trackDisplayProperties.setMaximumDuration(200);
  }

  /**
   * Configure labeling on the layer to use a red label using the VesselName attribute.
   *
   * @param layer the DynamicEntityLayer for the labels
   */
  private void setupLabeling(DynamicEntityLayer layer) {
    // define the label expression to be used, in this case we will use the "VesselName" for each of the dynamic entities
    var simpleLabelExpression = new SimpleLabelExpression("[VesselName]");

    // set the text symbol color and size for the labels
    var labelSymbol = new TextSymbol(12, "", Color.RED, TextSymbol.HorizontalAlignment.CENTER, TextSymbol.VerticalAlignment.BOTTOM);

    // set the label position
    var labelDef = new LabelDefinition(simpleLabelExpression, labelSymbol);

    // add the label definition to the dynamic entity layer and enable labels
    layer.getLabelDefinitions().add(labelDef);
    layer.labelsEnabledProperty().set(true);
  }

  /**
   * Stops and releases all resources used in application.
   */
  @Override
  public void stop() {
    // notify the observations processing to stop
    if (dynamicEntityDataSource != null) {
      try {
        dynamicEntityDataSource.disconnectAsync().get(2, TimeUnit.SECONDS);
      } catch (InterruptedException | TimeoutException | ExecutionException e) {
        System.err.println("Exception when disconnecting from data source: " + e);
      }
    }

    // release resources
    if (mapView != null) {
      mapView.dispose();
    }
  }

  /**
   * Opens and runs application.
   *
   * @param args arguments passed to this application
   */
  public static void main(String[] args) {
    launch(args);
  }
}

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