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
|
using NetTopologySuite.Features;
using WhatApi.Tables;
namespace WhatApi.Endpoints;
public class GetPlacesEndpoint(Database db) : BaseEndpoint
{
[HttpGet("~/places")]
public async Task<ActionResult> HandleAsync(double w, double s, double e, double n, CancellationToken ct = default) {
IQueryable<Place> 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);
}
}
|