Skip to content

Commit

Permalink
backend: srid on multipolygon
Browse files Browse the repository at this point in the history
  • Loading branch information
ElysaSrc committed Jun 29, 2024
1 parent 26f9f58 commit c246a7d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
27 changes: 24 additions & 3 deletions backend/src/helpers/postgis_polygons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type Polygon = Vec<Coordinates>;
pub struct MultiPolygon(Vec<Polygon>);

impl MultiPolygon {
pub fn to_polygon_string(&self) -> String {
pub fn to_polygon_string(&self, srid: Option<u32>) -> String {
let polygons_str = self
.0
.iter()
Expand All @@ -22,7 +22,10 @@ impl MultiPolygon {
.collect::<Vec<_>>()
.join("),(");

format!("MULTIPOLYGON(({}))", polygons_str)
match srid {
Some(srid) => format!("SRID={};MULTIPOLYGON(({}))", srid, polygons_str),
None => format!("MULTIPOLYGON(({}))", polygons_str),
}
}
}

Expand All @@ -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);
}
}
4 changes: 2 additions & 2 deletions backend/src/models/entity_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit c246a7d

Please sign in to comment.