Skip to content

Detect whether a coordinate or zone intersects with water layer #1111

@ADARSH-PS123

Description

@ADARSH-PS123

Currently, I am trying to determine whether a coordinate lies on water by querying the rendered features of the water layer using queryRenderedFeatures.
` Future _zoneContainsWater(
double centerLat,
double centerLon,
double radiusNM,
) async {
if (mapboxMap == null) return false;

try {
  // Calculate Bounding Box coordinates for the zone (NW and SE corners)
  final dLat = radiusNM / 60.0;
  final latCos = cos(centerLat * pi / 180.0);
  final dLon = latCos.abs() < 0.0001 ? dLat : dLat / latCos;

  final nLat = centerLat + dLat;
  final sLat = centerLat - dLat;
  final wLon = centerLon - dLon;
  final eLon = centerLon + dLon;

  // Convert those geographic corners to screen pixels
  final nw = await mapboxMap!.pixelForCoordinate(mapBox.Point(coordinates: mapBox.Position(wLon, nLat)));
  final se = await mapboxMap!.pixelForCoordinate(mapBox.Point(coordinates: mapBox.Position(eLon, sLat)));

  // Perform a query across the entire zone's bounding box
  final result = await mapboxMap!.queryRenderedFeatures(
    mapBox.RenderedQueryGeometry.fromScreenBox(mapBox.ScreenBox(
      min: mapBox.ScreenCoordinate(x: min(nw.x, se.x), y: min(nw.y, se.y)),
      max: mapBox.ScreenCoordinate(x: max(nw.x, se.x), y: max(nw.y, se.y)),
    )),
    mapBox.RenderedQueryOptions(
      // Ensure we target our manually added 'water' layer + any style-detected ones
      layerIds: ["water", ..._waterLayerIds],
    ),
  );

  developer.log("Zone query found ${result.length} features in targeted water layers");

  for (var queried in result) {
    if (queried != null) {
      final layers = queried.layers;
      final featureMap = queried.queriedFeature.feature;
      final properties = featureMap["properties"] as Map?;

      print("Water Feature Match: $layers, properties: $properties");

      bool layerMatch = layers.any((layer) =>
          layer != null &&
          (layer.toLowerCase().contains("water") || layer.toLowerCase().contains("ocean") || layer.toLowerCase().contains("lake") || layer.toLowerCase().contains("sea")));

      bool propertyMatch = properties != null && (properties["class"] == "water" || properties["type"] == "water" || properties["water"] == "true");

      if (layerMatch || propertyMatch) {
        developer.log("WATER CONFIRMED inside Zone Box!");
        return true;
      }
    }
  }
} catch (e) {
  developer.log("Error querying zone box for water: $e");
}

return false; // No water features found within the 2*Radius area

}`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions