feat(geo): bbox spatial filter on GET /{dict}/records

v2 фича: Альтум-задача (заказ съёмки) использует bbox region. До
этого consumers fetch'или ВСЕ записи и filter'или client-side. Теперь
PostGIS ST_Intersects на server-side через GiST index.

API:
  GET /api/v1/{dictionaryName}/records?bbox=west,south,east,north

Coords в SRID 4326 (longitude, latitude). Параметр optional —
отсутствует = старое поведение (full list).

BoundingBox helper (ordinis-read-api/spatial/):
- Парсинг "west,south,east,north" с валидацией
- Range checks: lon ∈ [-180,180], lat ∈ [-90,90]
- west <= east (anti-meridian crossing rejected в v1)
- south <= north
- Понятные error messages → 400 Bad Request через BadBboxException

Repository (ordinis-domain):
- findActiveByBbox native query, ST_MakeEnvelope(:west,:south,:east,
  :north,4326) ST_Intersects на geometry колонке. GiST index hit.
- Записи с geometry IS NULL автоматически отфильтровываются.

Tests (12 в BoundingBoxTest):
- Valid parsing + negative coords + whitespace
- Reject empty/null/wrong-count/non-numeric
- Range validation (lon/lat bounds)
- Anti-meridian + inverted N/S detected
- Area calc sanity

Total project tests: 149 → 161.

Polygon GeoJSON support — отложен (нужен jts-io-common dep). Bbox
покрывает Альтум use case + 80% other consumers.
This commit is contained in:
Zimin A.N.
2026-05-06 15:43:26 +03:00
parent 75967edc9a
commit 0811ad8506
6 changed files with 256 additions and 4 deletions
@@ -85,4 +85,38 @@ public interface DictionaryRecordRepository extends JpaRepository<DictionaryReco
List<Object[]> countActiveGroupedByDictionary(
@Param("allowedScopes") Collection<DataScope> allowedScopes,
@Param("at") OffsetDateTime at);
/**
* Spatial filter: активные записи которые intersect bbox (SRID 4326).
* Используется read-api endpoint {@code GET /api/v1/{dict}/records?bbox=...}.
* PostGIS {@code ST_Intersects} использует GiST index на geometry колонке —
* O(log n) lookup, не sequential scan.
*
* <p>Native query (не JPA) потому что hibernate-spatial 7.x не имеет clean
* mapping для {@code ST_MakeEnvelope}. Native + ST_MakeEnvelope экономит
* один parse vs передачи WKT строки.
*
* <p>{@code data_scope IN (...)} нативно требует постгресовский
* ANY(string_array) — но у нас scopes небольшой enum, ставим IN-list через
* JPA String parameter.
*/
@Query(value = """
SELECT * FROM dictionary_records r
WHERE r.dictionary_id = :dictionaryId
AND r.data_scope = ANY(CAST(:scopesCsv AS TEXT[]))
AND r.valid_from <= :at
AND r.valid_to > :at
AND r.geometry IS NOT NULL
AND ST_Intersects(
r.geometry,
ST_MakeEnvelope(:west, :south, :east, :north, 4326))
""", nativeQuery = true)
List<DictionaryRecord> findActiveByBbox(
@Param("dictionaryId") UUID dictionaryId,
@Param("scopesCsv") String[] scopesCsv,
@Param("at") OffsetDateTime at,
@Param("west") double west,
@Param("south") double south,
@Param("east") double east,
@Param("north") double north);
}