Add dynamic entity layer

View on GitHubSample viewer app

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

AddDynamicEntityLayer

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 Java allows you to connect to a stream service and manage the information as dynamic entities within the application, 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 ArcGIStreamService using the service URL.
  2. Set a DynamicEntityFilter on the stream service to limit the amount of data coming from the server.
  3. Set the MaximumDuration property of the stream service PurgeOptions to limit the amount of data managed by the application.
  4. Create a DynamicEntityLayer using the stream service.
  5. Update values in the layer's TrackDisplayProperties to customize the layer's appearance.
  6. Add the DynamicEntityLayer to the map.

Relevant API

  • ArcGISStreamService
  • ConnectionStatus
  • DynamicEntity
  • DynamicEntityFilter
  • 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.

Tags

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

Sample Code

AddDynamicEntityLayerSample.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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
 * 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_dynamic_entity_layer;

import java.util.List;

import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.geometry.Envelope;
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.view.Graphic;
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.realtime.ArcGISStreamService;
import com.esri.arcgisruntime.realtime.ArcGISStreamServiceFilter;
import com.esri.arcgisruntime.realtime.ConnectionStatus;
import com.esri.arcgisruntime.symbology.SimpleLineSymbol;
import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol;
import com.esri.arcgisruntime.symbology.SimpleRenderer;
import com.esri.arcgisruntime.symbology.UniqueValueRenderer;

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class AddDynamicEntityLayerSample extends Application {

  private MapView mapView;
  private VBox controlsVBox;
  private Label connectionStatusLabel;
  private Label observationSliderLabel;
  private Button connectionButton;
  private Button purgeButton;
  private CheckBox trackLinesCheckBox;
  private CheckBox observationsCheckBox;
  private Slider observationsSlider;

  @Override
  public void start(Stage stage) {

    try {
      // create stack pane and application scene
      StackPane stackPane = new StackPane();
      Scene scene = new Scene(stackPane);

      // set title, size, and add scene to stage
      stage.setTitle("Add dynamic entity layer sample");
      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 map with the dark gray basemap style
      ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_DARK_GRAY_BASE);

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

      // create an envelope around Sandy, Utah to be the extent used by the dynamic entity filter and viewpoint
      var utahSandyEnvelope = new Envelope(new Point(-112.110052, 40.718083, SpatialReferences.getWgs84()),
        new Point(-111.814782, 40.535247, SpatialReferences.getWgs84()));

      // set the viewpoint to the map view
      mapView.setViewpoint(new Viewpoint(utahSandyEnvelope));

      // create a graphics overlay and add a graphic to show the envelope's extent using a simple line symbol
      var borderLineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.DASH, Color.RED, 2);
      var graphicsOverlay = new GraphicsOverlay();
      var graphic = new Graphic(utahSandyEnvelope, borderLineSymbol);
      graphicsOverlay.getGraphics().add(graphic);

      // add the graphics overlay to the map view
      mapView.getGraphicsOverlays().add(graphicsOverlay);

      // create a controls vbox containing the UI components
      setUpControlsVBox();

      // create an ArcGIS stream service using a stream service URL
      var streamServiceUrl = "https://realtimegis2016.esri.com:6443/arcgis/rest/services/SandyVehicles/StreamServer";
      var streamService = new ArcGISStreamService(streamServiceUrl);

      // add a filter for the data source to limit the amount of data received by the application
      var streamServiceFilter = new ArcGISStreamServiceFilter();
      streamServiceFilter.setGeometry(utahSandyEnvelope);
      streamServiceFilter.setWhereClause("speed > 0");
      streamService.setFilter(streamServiceFilter);

      // set a duration of five minutes for how long observation data is stored in the data source
      streamService.getPurgeOptions().setMaximumDuration(300);

      // create a dynamic entity layer to display the data from the stream service
      var dynamicEntityLayer = new DynamicEntityLayer(streamService);

      // create unique values for the different snow plow operating agencies represented in the data
      // create a blue symbol to use as the default
      var entityBlueSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CIRCLE, Color.BLUE, 8);

      // create a unique value with a pink marker symbol for the agency represented by value "3"
      var entityPinkSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CIRCLE, Color.PINK, 8);
      var entityPinkValue = new UniqueValueRenderer.UniqueValue(null, null, entityPinkSymbol,
        List.of(3), List.of(entityBlueSymbol));

      // create a unique value with a lime marker symbol for the agency represented by value "4"
      var entityLimeSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CIRCLE, Color.LIME, 8);
      var entityLimeValue = new UniqueValueRenderer.UniqueValue(null, null, entityLimeSymbol,
        List.of(4), List.of(entityBlueSymbol));

      // create a unique value renderer using the configured unique values
      var entityRenderer = new UniqueValueRenderer(List.of("agency"),
        List.of(entityPinkValue, entityLimeValue), null, entityBlueSymbol);

      // set the renderer to the dynamic entity layer
      dynamicEntityLayer.setRenderer(entityRenderer);

      // create unique values to represent observation tracks for the different snow plow operating agencies represented
      // in the data. Create a blue symbol to use as the default
      var previousObservationBlueSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CIRCLE, Color.BLUE, 3);

      // create a unique value with a pink marker symbol for the agency represented by value "3"
      var previousObservationPinkSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CIRCLE, Color.PINK, 3);
      var previousObservationPinkValue = new UniqueValueRenderer.UniqueValue(null, null, previousObservationPinkSymbol,
        List.of(3), List.of(previousObservationBlueSymbol));

      // create a unique value with a lime marker symbol for the agency represented by value "4"
      var previousObservationLimeSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CIRCLE, Color.LIME, 3);
      var previousObservationLimeValue = new UniqueValueRenderer.UniqueValue(null, null, previousObservationLimeSymbol,
        List.of(4), List.of(previousObservationBlueSymbol));

      // create a unique value renderer for the previous observations using the unique values for the different snow plow
      // operating agencies represented in the data
      var previousObservationsRenderer = new UniqueValueRenderer(List.of("agency"),
        List.of(previousObservationPinkValue, previousObservationLimeValue), null, previousObservationBlueSymbol);

      // set the renderer to the dynamic entity layer display properties
      dynamicEntityLayer.getTrackDisplayProperties().setPreviousObservationRenderer(previousObservationsRenderer);

      // create a simple renderer to represent track lines and set it as the track line renderer on the dynamic entity
      // layer display properties
      var trackLineRenderer = new SimpleRenderer(new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.LIGHTGRAY, 2));
      dynamicEntityLayer.getTrackDisplayProperties().setTrackLineRenderer(trackLineRenderer);

      // bind the label text property to the connection status property value
      connectionStatusLabel.textProperty().bind(Bindings.createStringBinding(() ->
        "Status: " + streamService.connectionStatusProperty().getValue(), streamService.connectionStatusProperty()));

      // update the button text depending on the connection status value
      connectionButton.textProperty().bind(Bindings.createStringBinding(() -> {
        if (streamService.getConnectionStatus() == ConnectionStatus.CONNECTED) {
          return "Disconnect";
        } else {
          return "Connect";
        }
      }, streamService.connectionStatusProperty()));

      // toggle the service connection on button press
      connectionButton.setOnAction(a -> {
        if (streamService.getConnectionStatus() == ConnectionStatus.CONNECTED) {
          streamService.disconnectAsync();
        } else {
          streamService.connectAsync();
        }
      });

      // get the track display properties property value to control the display of previous observations and track lines
      var layerTrackDisplayProperties = dynamicEntityLayer.getTrackDisplayProperties();

      // configure checkboxes to control the visibility of track lines and previous observations
      trackLinesCheckBox.selectedProperty().bindBidirectional(layerTrackDisplayProperties.showTrackLineProperty());
      observationsCheckBox.selectedProperty().bindBidirectional(layerTrackDisplayProperties.showPreviousObservationsProperty());

      // set checkboxes to be enabled when sample is started
      trackLinesCheckBox.selectedProperty().set(true);
      observationsCheckBox.selectedProperty().set(true);

      // configure the observations label and slider to update and control the maximum number of observations
      observationSliderLabel.textProperty().bind(Bindings.createStringBinding(() ->
        "Observations per track (" + layerTrackDisplayProperties.maximumObservationsProperty().getValue() + ")",
        layerTrackDisplayProperties.maximumObservationsProperty()));

      observationsSlider.valueProperty().bindBidirectional(layerTrackDisplayProperties.maximumObservationsProperty());

      // bind the slider disable property to the selected property of the observations checkbox
      // disable the slider if the observations checkbox is not selected
      observationsSlider.disableProperty().bind(observationsCheckBox.selectedProperty().not());

      // purge all observations on button press
      purgeButton.setOnAction(a -> streamService.purgeAllAsync());

      // add the dynamic entity layer to the map's operational layers
      map.getOperationalLayers().add(dynamicEntityLayer);

      // add the map view and controls vbox to the stack pane
      stackPane.getChildren().addAll(mapView, controlsVBox);
      StackPane.setAlignment(controlsVBox, Pos.TOP_RIGHT);
      StackPane.setMargin(controlsVBox, new Insets(10, 10, 0, 10));
    } catch (Exception e) {
      // on any error, display the stack trace.
      e.printStackTrace();
    }
  }

  /**
   * Configure a VBox for the UI, containing labels, buttons, checkbox, and slider.
   */
  private void setUpControlsVBox() {

    // create labels for the connection status and the observations per track slider
    connectionStatusLabel = new Label("Status: ");
    observationSliderLabel = new Label("Observations per track: ");

    // create buttons to toggle the service connection and purge observations
    connectionButton = new Button();
    connectionButton.setMaxWidth(Double.MAX_VALUE);
    purgeButton = new Button("Purge all observations");
    purgeButton.setMaxWidth(Double.MAX_VALUE);

    // create checkbox to toggle visibility of track lines and previous observations
    trackLinesCheckBox = new CheckBox("Track lines");
    observationsCheckBox = new CheckBox("Previous observations");

    // create a slider to update the number of previous observations
    observationsSlider = new Slider(1, 16, 5);

    // create hbox and add the observations label and slider
    var sliderHBox = new HBox();
    sliderHBox.setSpacing(3.0);
    sliderHBox.getChildren().addAll(observationSliderLabel, observationsSlider);

    // create a vbox and add the labels, checkbox, hbox, and buttons
    controlsVBox = new VBox();
    controlsVBox.setPadding(new Insets(15.0));
    controlsVBox.setSpacing(5.0);
    controlsVBox.setMaxSize(325, 120);
    controlsVBox.setBackground(new Background(new BackgroundFill(Color.WHITE, CornerRadii.EMPTY, Insets.EMPTY)));
    controlsVBox.getChildren().addAll(connectionStatusLabel, connectionButton, trackLinesCheckBox,
      observationsCheckBox, sliderHBox, purgeButton);
  }

  /**
   * Stops and releases all resources used in application.
   */
  @Override
  public void stop() {

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

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

    Application.launch(args);
  }
}

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