Skip to content

Commit

Permalink
Add DriverVectorCube.get_bounding_box_area
Browse files Browse the repository at this point in the history
  • Loading branch information
soxofaan committed Oct 12, 2022
1 parent 8e3b58a commit dad6bc9
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 3 deletions.
2 changes: 1 addition & 1 deletion openeo_driver/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.21.1a1"
__version__ = "0.21.2a1"
8 changes: 8 additions & 0 deletions openeo_driver/datacube.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from openeo_driver.datastructs import SarBackscatterArgs, ResolutionMergeArgs, StacAsset
from openeo_driver.errors import FeatureUnsupportedException, InternalException
from openeo_driver.util.ioformats import IOFORMATS
from openeo_driver.util.utm import area_in_square_meters
from openeo_driver.utils import EvalEnv

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -320,6 +321,7 @@ def to_legacy_save_result(self) -> Union["AggregatePolygonResult", "JSONResult"]
)

def get_bounding_box(self) -> Tuple[float, float, float, float]:
# TODO: cache bounding box?
return tuple(self._geometries.total_bounds)

def get_bounding_box_geometry(self) -> shapely.geometry.Polygon:
Expand All @@ -328,6 +330,12 @@ def get_bounding_box_geometry(self) -> shapely.geometry.Polygon:
def get_bounding_box_geojson(self) -> dict:
return shapely.geometry.mapping(self.get_bounding_box_geometry())

def get_bounding_box_area(self) -> float:
"""Bounding box area in square meters"""
return area_in_square_meters(
self.get_bounding_box_geometry(), crs=self.get_crs()
)

def get_geometries(self) -> Sequence[shapely.geometry.base.BaseGeometry]:
return self._geometries.geometry

Expand Down
4 changes: 2 additions & 2 deletions openeo_driver/util/utm.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def auto_utm_crs_for_geometry(geometry: BaseGeometry, crs: str) -> str:
return 'epsg:' + str(epsg)


def geometry_to_crs(geometry, crs_from, crs_to):
def geometry_to_crs(geometry: BaseGeometry, crs_from, crs_to):
# Skip if CRS definitions are exactly the same
if crs_from == crs_to:
return geometry
Expand All @@ -71,7 +71,7 @@ def project(x, y, z=0):
return shapely.ops.transform(project, geometry)


def area_in_square_meters(geometry, crs: Union[str, pyproj.CRS]):
def area_in_square_meters(geometry: BaseGeometry, crs: Union[str, pyproj.CRS]):
if isinstance(crs, str):
crs = "+init=" + crs # TODO: this is deprecated

Expand Down
37 changes: 37 additions & 0 deletions tests/data/geojson/FeatureCollection06.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"type": "FeatureCollection",
"features": [

{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
3.67466926574707,
51.037804967049205
],
[
3.70737075805664,
51.037804967049205
],
[
3.70737075805664,
51.05793176907366
],
[
3.67466926574707,
51.05793176907366
],
[
3.67466926574707,
51.037804967049205
]
]
]
}
}
]
}
11 changes: 11 additions & 0 deletions tests/test_vectorcube.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import geopandas as gpd
import numpy.testing
import pyproj
import pytest
import xarray
from shapely.geometry import Polygon, MultiPolygon

from openeo_driver.datacube import DriverVectorCube
from openeo_driver.testing import DictSubSet

from .data import get_path


Expand Down Expand Up @@ -254,3 +256,12 @@ def test_get_bounding_box(self, gdf):
((1.0, 1.0), (1.0, 4.0), (5.0, 4.0), (5.0, 1.0), (1.0, 1.0)),
),
}

def test_get_bounding_box_area(self):
path = str(get_path("geojson/FeatureCollection06.json"))
vc = DriverVectorCube(gpd.read_file(path))
area = vc.get_bounding_box_area()
# TODO: area of FeatureCollection06 (square with side of approx 2.3km, based on length of Watersportbaan Gent)
# is roughly 2.3 km * 2.3 km = 5.29 km2,
# but current implementation gives result that is quite a bit larger than that
numpy.testing.assert_allclose(area, 8e6, rtol=0.1)

0 comments on commit dad6bc9

Please sign in to comment.