Skip to content

Commit

Permalink
refactor: move odc-geo import to top, add test for missing odc-geo.
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Rösner committed Nov 7, 2023
1 parent c043b97 commit 09c35fd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
15 changes: 9 additions & 6 deletions pyresample/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@
except ImportError:
da = None

try:
import odc.geo as odc_geo
except ModuleNotFoundError:
odc_geo = None

Check warning on line 62 in pyresample/geometry.py

View check run for this annotation

Codecov / codecov/patch

pyresample/geometry.py#L61-L62

Added lines #L61 - L62 were not covered by tests

from pyproj import CRS
from pyproj.enums import TransformDirection

Expand Down Expand Up @@ -1977,14 +1982,12 @@ def to_odc_geobox(self):
See: https://odc-geo.readthedocs.io/en/latest/
"""
try:
from odc.geo import Resolution
from odc.geo.geobox import GeoBox
except ModuleNotFoundError:
if odc_geo is None:
raise ModuleNotFoundError("Please install 'odc-geo' to use this method.")

return GeoBox.from_bbox(bbox=self.area_extent, crs=self.crs,
resolution=Resolution(x=self.pixel_size_x, y=-self.pixel_size_y), tight=True)
return odc_geo.geobox.GeoBox.from_bbox(bbox=self.area_extent, crs=self.crs,
resolution=odc_geo.Resolution(x=self.pixel_size_x, y=-self.pixel_size_y),
tight=True)

def create_areas_def(self):
"""Generate YAML formatted representation of this area.
Expand Down
10 changes: 10 additions & 0 deletions pyresample/test/test_geometry/test_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,16 @@ def test_cartopy_crs_latlon_bounds(self, create_test_area):
latlong_crs = area_def.to_cartopy_crs()
np.testing.assert_allclose(latlong_crs.bounds, [-180, 180, -90, 90])

def test_to_odc_geobox_odc_missing(self, monkeypatch, stere_area):
"""Test odc-geo not installed."""
area = stere_area

with monkeypatch.context() as m:
m.setattr(pyresample.geometry, "odc_geo", None)

with pytest.raises(ModuleNotFoundError):
area.to_odc_geobox()

def test_to_odc_geobox(self, stere_area, create_test_area):
"""Test conversion from area definition to odc GeoBox."""
from odc.geo.geobox import GeoBox
Expand Down

0 comments on commit 09c35fd

Please sign in to comment.