View in MAUI WPF WinUI View on GitHub

Find dynamic entities from a data source that match a query.

Image of Query Dynamic Entities

Use case

Developers can query a DynamicEntityDataSource to find dynamic entities that meet spatial and/or attribute criteria. The query returns a collection of dynamic entities matching the DynamicEntityQueryParameters or Track IDs at the moment the query is executed. An example of this is a flight tracking app that monitors airspace near a particular airport, allowing the user to monitor flights based on different criteria such as arrival airport or flight number.

How to use the sample

Tap the “Query Flights” button and select a query to perform from the menu. Once the query is complete, a list of the resulting flights will be displayed. Tap on a flight to see its latest attributes in real-time.

How it works

  1. Create a DynamicEntityDataSource to stream dynamic entity events.
  2. Create a DynamicEntityLayer using the data source and add it to the map’s operational layers.
  3. Create DynamicEntityQueryParameters and set its properties to specify the parameters for the query:
    1. To spatially filter results, set the geometry and spatialRelationship. The spatial relationship is intersects by default.
    2. To query entities with certain attribute values, set the whereClause.
    3. To get entities with specific track IDs, modify the trackIDs collection.
  4. To perform a dynamic entities query, use DynamicEntityDataSource.QueryDynamicEntitiesAsync() to query with multiple criteria (such as track IDs, spatial, and/or attribute filters), or use DynamicEntityDataSource.QueryDynamicEntitiesAsync(trackIDs) if you want to query only by track IDs.
  5. When complete, iterate through the returned collection of DynamicEntity objects from the result.
  6. Use the DynamicEntity.DynamicEntityChanged event to get real-time updates when entity attributes change.
  7. Get the new observation from the resulting DynamicEntityChangedEventArgs using the ReceivedObservation property to update the UI with current information.

Relevant API

  • DynamicEntity
  • DynamicEntityChangedInfo
  • DynamicEntityDataSource
  • DynamicEntityDataSourceInfo
  • DynamicEntityLayer
  • DynamicEntityObservation
  • DynamicEntityQueryParameters
  • DynamicEntityQueryResult

About the data

This sample uses the PHX Air Traffic JSON portal item, which is hosted on ArcGIS Online and downloaded automatically. The file contains JSON data for mock air traffic around the Phoenix Sky Harbor International Airport in Phoenix, AZ, USA. The decoded data is used to simulate dynamic entity events through a CustomDynamicEntityDataSource, which is displayed on the map with a DynamicEntityLayer.

Additional information

A dynamic entities query is performed on the most recent observation of each dynamic entity in the data source at the time the query is executed. As the dynamic entities change, they may no longer match the query parameters.

Tags

data, dynamic, entity, live, query, real-time, search, stream, track

Sample Code

CustomStreamService.cs CustomStreamService.cs FlightInfo.cs QueryDynamicEntities.xaml QueryDynamicEntities.xaml.cs
// Copyright 2025 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.
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.RealTime;
using System.Text.Json;
namespace ArcGIS.Samples.QueryDynamicEntities
{
public class CustomStreamService : DynamicEntityDataSource
{
private readonly DynamicEntityDataSourceInfo _info;
private readonly string _dataPath;
private readonly TimeSpan _delay;
private CancellationTokenSource _cancellationTokenSource;
private Task _dataFeedTask;
// Initializes the custom stream service with data source info and file path.
public CustomStreamService(DynamicEntityDataSourceInfo info, string dataPath, TimeSpan delay)
{
_info = info;
_dataPath = dataPath;
_delay = delay;
}
// Returns the data source information while loading the service.
protected override Task<DynamicEntityDataSourceInfo> OnLoadAsync() =>
Task.FromResult(_info);
// Starts the data feed processing task when connecting to the service.
protected override Task OnConnectAsync(CancellationToken cancellationToken)
{
_cancellationTokenSource = new CancellationTokenSource();
_dataFeedTask = ProcessDataFeedAsync(_cancellationTokenSource.Token);
return Task.CompletedTask;
}
// Cancels the data feed processing task and cleans up resources when disconnecting from the service.
protected override async Task OnDisconnectAsync()
{
_cancellationTokenSource?.Cancel();
if (_dataFeedTask != null)
{
try
{
await _dataFeedTask;
}
catch (OperationCanceledException)
{
}
}
_cancellationTokenSource?.Dispose();
}
// Reads and processes flight data from JSON file line by line with delays.
private async Task ProcessDataFeedAsync(CancellationToken cancellationToken)
{
try
{
if (!File.Exists(_dataPath))
throw new FileNotFoundException("Flight data file not found.", _dataPath);
// Read all lines off the UI thread. Each line should contain one JSON object.
var lines = await File.ReadAllLinesAsync(_dataPath, cancellationToken);
foreach (var line in lines)
{
if (cancellationToken.IsCancellationRequested) break;
try
{
// Parse a single JSON line (newline-delimited JSON format).
using (JsonDocument document = JsonDocument.Parse(line))
{
var root = document.RootElement;
// Geometry section (expected: { "geometry": { "x": <double>, "y": <double> } })
if (root.TryGetProperty("geometry", out JsonElement geometryElement))
{
double x = 0, y = 0;
// Extract X coordinate (defaults remain 0 if missing or invalid).
if (geometryElement.TryGetProperty("x", out JsonElement xElement))
x = xElement.GetDouble();
// Extract Y coordinate.
if (geometryElement.TryGetProperty("y", out JsonElement yElement))
y = yElement.GetDouble();
// Create geometry in WGS84.
var point = new MapPoint(x, y, SpatialReferences.Wgs84);
// Collect attribute key/value pairs.
var attributes = new Dictionary<string, object>();
if (root.TryGetProperty("attributes", out JsonElement attributesElement))
{
foreach (var property in attributesElement.EnumerateObject())
{
object value = property.Value.ValueKind switch
{
JsonValueKind.String => property.Value.GetString(),
JsonValueKind.Number when property.Value.TryGetInt32(out int intValue) => intValue,
JsonValueKind.Number => property.Value.GetDouble(),
JsonValueKind.True => true,
JsonValueKind.False => false,
JsonValueKind.Null => null,
_ => property.Value.ToString()
};
attributes[property.Name] = value;
}
}
// Add the observation to the data source.
AddObservation(point, attributes);
}
}
// Introduce a delay to simulate real-time data feed.
await Task.Delay(_delay, cancellationToken);
}
catch (JsonException ex)
{
System.Diagnostics.Debug.WriteLine($"Error parsing JSON line: {ex.Message}");
continue;
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Data feed error: {ex.Message}");
}
}
}
}