From c246a7d17a545e45304d440016fc77d18b4f3d1a Mon Sep 17 00:00:00 2001 From: ElysaSrc <101974839+ElysaSrc@users.noreply.github.com> Date: Sat, 29 Jun 2024 23:58:21 +0200 Subject: [PATCH] backend: srid on multipolygon --- backend/src/helpers/postgis_polygons.rs | 27 ++++++++++++++++++++++--- backend/src/models/entity_cache.rs | 4 ++-- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/backend/src/helpers/postgis_polygons.rs b/backend/src/helpers/postgis_polygons.rs index 8d24baa..c39627b 100644 --- a/backend/src/helpers/postgis_polygons.rs +++ b/backend/src/helpers/postgis_polygons.rs @@ -7,7 +7,7 @@ type Polygon = Vec; pub struct MultiPolygon(Vec); impl MultiPolygon { - pub fn to_polygon_string(&self) -> String { + pub fn to_polygon_string(&self, srid: Option) -> String { let polygons_str = self .0 .iter() @@ -22,7 +22,10 @@ impl MultiPolygon { .collect::>() .join("),("); - format!("MULTIPOLYGON(({}))", polygons_str) + match srid { + Some(srid) => format!("SRID={};MULTIPOLYGON(({}))", srid, polygons_str), + None => format!("MULTIPOLYGON(({}))", polygons_str), + } } } @@ -45,6 +48,24 @@ mod tests { let expected_string = "MULTIPOLYGON(((30 20, 45 40, 10 40, 30 20)),((15 5, 40 10, 10 20, 5 10, 15 5)))"; - assert_eq!(multi_polygon.to_polygon_string(), expected_string); + assert_eq!(multi_polygon.to_polygon_string(None), expected_string); + } + + #[test] + fn test_to_polygon_string_with_srid() { + let multi_polygon = MultiPolygon(vec![ + vec![(30.0, 20.0), (45.0, 40.0), (10.0, 40.0), (30.0, 20.0)], + vec![ + (15.0, 5.0), + (40.0, 10.0), + (10.0, 20.0), + (5.0, 10.0), + (15.0, 5.0), + ], + ]); + + let expected_string = + "SRID=3857;MULTIPOLYGON(((30 20, 45 40, 10 40, 30 20)),((15 5, 40 10, 10 20, 5 10, 15 5)))"; + assert_eq!(multi_polygon.to_polygon_string(Some(3857)), expected_string); } } diff --git a/backend/src/models/entity_cache.rs b/backend/src/models/entity_cache.rs index 4e746b0..11781d1 100644 --- a/backend/src/models/entity_cache.rs +++ b/backend/src/models/entity_cache.rs @@ -306,7 +306,7 @@ impl ViewerCachedEntity { request.ymax, request .geographic_restriction - .map(|g| g.to_polygon_string()), + .map(|g| g.to_polygon_string(Some(3857))), request.family_id, request.allow_all_categories, request.allow_all_tags, @@ -421,7 +421,7 @@ impl ViewerCachedEntity { request.search_query, request .geographic_restriction - .map(|g| g.to_polygon_string()), + .map(|g| g.to_polygon_string(Some(3857))), request.family_id, request.allow_all_categories, request.allow_all_tags,