detectTrajectories function

List<DetectedTrajectory> detectTrajectories(
  1. Object? geojson
)

Quét một GeoJSON tìm trajectory path + cursor, gom theo sharemap:entityId. Trả danh sách rỗng khi không có trajectory nào.

Implementation

List<DetectedTrajectory> detectTrajectories(Object? geojson) {
  final List<Map<String, dynamic>> features;
  if (geojson is Map &&
      geojson['type'] == 'FeatureCollection' &&
      geojson['features'] is List) {
    features = (geojson['features'] as List)
        .whereType<Map>()
        .map((f) => Map<String, dynamic>.from(f))
        .toList();
  } else if (geojson is Map && geojson['type'] == 'Feature') {
    features = [Map<String, dynamic>.from(geojson)];
  } else {
    return const [];
  }

  final cursors = <String, TrajectoryCursor>{};
  for (final f in features) {
    final props = f['properties'] is Map
        ? Map<String, dynamic>.from(f['properties'] as Map)
        : <String, dynamic>{};
    final geometry = f['geometry'];
    if (props[kSharemapTypeKey] == kTrajectoryCursorType &&
        geometry is Map &&
        geometry['type'] == 'Point') {
      final entityId = (props[kSharemapEntityIdKey] ?? '').toString();
      final coords = geometry['coordinates'];
      if (entityId.isNotEmpty &&
          coords is List &&
          coords.length >= 2 &&
          coords[0] is num &&
          coords[1] is num) {
        cursors[entityId] = TrajectoryCursor(
          entityId: entityId,
          coord: [(coords[0] as num).toDouble(), (coords[1] as num).toDouble()],
          properties: props,
        );
      }
    }
  }

  final detected = <DetectedTrajectory>[];
  for (final f in features) {
    final props = f['properties'] is Map
        ? Map<String, dynamic>.from(f['properties'] as Map)
        : <String, dynamic>{};
    final geometry = f['geometry'];
    if (props[kSharemapTypeKey] != kTrajectoryType ||
        geometry is! Map ||
        geometry['type'] != 'LineString') {
      continue;
    }
    final entityId = (props[kSharemapEntityIdKey] ?? '').toString();
    if (entityId.isEmpty) continue;

    final path = TrajectoryPath(
      entityId: entityId,
      label: props[kSharemapEntityLabelKey]?.toString(),
      coordinates: _coordList(geometry['coordinates']),
      timestamps: _numList(props[kSharemapTimestampsKey]) ?? const [],
      speeds: _numList(props[kSharemapSpeedsKey]),
      bearings: _numList(props[kSharemapBearingsKey]),
      statuses: _stringList(props[kSharemapStatusesKey]),
      playback: PlaybackConfig.fromJson(props[kSharemapPlaybackKey]),
    );
    detected.add(
      DetectedTrajectory(
        entityId: entityId,
        path: path,
        cursor: cursors[entityId],
      ),
    );
  }
  return detected;
}