Skip to content

Commit 387c167

Browse files
feat: add GeoJSON to bounding box helper
1 parent 623ac3d commit 387c167

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed
Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,38 @@
1+
from dataclasses import dataclass
2+
13
from geojson import Feature
24
from geojson.utils import coords
35
from pyproj import Geod
46

57

6-
def calculate_area(feature: Feature) -> float:
7-
"""Calculate area in meter."""
8-
geod = Geod(ellps="WGS84")
8+
@dataclass
9+
class BoundingBox:
10+
lon_min: float # left
11+
lat_min: float # bottom
12+
lon_max: float # right
13+
lat_max: float # top
14+
15+
16+
def get_coordinates(feature: Feature) -> tuple:
917
coordinates = tuple(coords(feature))
1018
lons = tuple(c[0] for c in coordinates)
1119
lats = tuple(c[1] for c in coordinates)
20+
return lons, lats
21+
22+
23+
def calculate_area(feature: Feature) -> float:
24+
"""Calculate area in meter."""
25+
lons, lats = get_coordinates(feature)
26+
geod = Geod(ellps="WGS84")
1227
area, _ = geod.polygon_area_perimeter(lons=lons, lats=lats)
1328
return area
29+
30+
31+
def get_bounding_box(feature: Feature) -> BoundingBox:
32+
lons, lats = get_coordinates(feature)
33+
return BoundingBox(
34+
lon_min=min(lons),
35+
lat_min=min(lats),
36+
lon_max=max(lons),
37+
lat_max=max(lats),
38+
)

tests/unittests/test_helper_geo.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1+
from dataclasses import astuple
2+
13
import pytest
24

3-
from ohsome_quality_api.utils.helper_geo import calculate_area
5+
from ohsome_quality_api.utils.helper_geo import calculate_area, get_bounding_box
46

57

68
def test_calculate_area(feature_germany_heidelberg):
79
expected = 108852960.62891776 # derived from PostGIS ST_AREA
810
result = calculate_area(feature_germany_heidelberg)
911
assert result == pytest.approx(expected, abs=1e-3)
12+
13+
14+
def test_get_bounding_box(feature_germany_heidelberg):
15+
result = get_bounding_box(feature_germany_heidelberg)
16+
expected = (8.5731788, 49.3520029, 8.7940496, 49.4596927)
17+
assert astuple(result) == pytest.approx(expected, abs=1e-6)

0 commit comments

Comments
 (0)