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
49
50
51
52
53
54
55
56
57
|
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using NetTopologySuite;
using NetTopologySuite.Features;
using NetTopologySuite.Geometries;
using WhatApi.Tables;
namespace WhatApi.Endpoints;
public class GetPlacesEndpoint(Database db) : BaseEndpoint
{
[HttpGet("~/places")]
public async Task<ActionResult> HandleAsync(string w, string s, string e, string n, CancellationToken ct = default) {
var north = double.Parse(n);
var east = double.Parse(e);
var south = double.Parse(s);
var west = double.Parse(w);
IQueryable<Place> resultingQuery;
if (west > east) {
resultingQuery = db.Places
.FromSqlInterpolated($"""
SELECT * FROM "Place"
WHERE ST_Intersects(
"Location",
ST_MakeEnvelope({west}, {south}, 180, {north}, {Constants.Wgs84SpatialReferenceId}) || ST_MakeEnvelope(-180, {south}, {east}, {north}, {Constants.Wgs84SpatialReferenceId})
)
""");
} else {
resultingQuery = db.Places
.FromSqlInterpolated($"""
SELECT * FROM "Place"
WHERE ST_Intersects(
"Location",
ST_MakeEnvelope({west}, {south}, {east}, {north}, {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);
}
}
|