using NetTopologySuite.Features; using WhatApi.Tables; namespace WhatApi.Endpoints; public class GetPlacesEndpoint(Database db) : BaseEndpoint { [HttpGet("~/places")] public async Task HandleAsync(double w, double s, double e, double n, CancellationToken ct = default) { IQueryable resultingQuery; if (w > e) { resultingQuery = db.Places .FromSqlInterpolated($""" SELECT * FROM "place" WHERE ST_Intersects( "Location", ST_MakeEnvelope({w}, {s}, 180, {n}, {Constants.Wgs84SpatialReferenceId}) || ST_MakeEnvelope(-180, {n}, {e}, {n}, {Constants.Wgs84SpatialReferenceId}) ) """); } else { resultingQuery = db.Places .FromSqlInterpolated($""" SELECT * FROM "place" WHERE ST_Intersects( "Location", ST_MakeEnvelope({w}, {s}, {e}, {n}, {Constants.Wgs84SpatialReferenceId}) ) """); } var gf = NtsGeometryServices.Instance.CreateGeometryFactory(srid: Constants.Wgs84SpatialReferenceId); var fc = new FeatureCollection(); await foreach (var p in resultingQuery.AsAsyncEnumerable().WithCancellation(ct)) { var point = gf.CreatePoint(new Coordinate(p.Location.X, p.Location.Y)); fc.Add(new Feature(point, new AttributesTable { { "id", p.Id }, { "cid", p.ContentId } })); } return Ok(fc); } }