From 09c35fd0fe077bd363f05d932d68098550176472 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20R=C3=B6sner?= Date: Tue, 7 Nov 2023 12:55:00 +0100 Subject: [PATCH] refactor: move odc-geo import to top, add test for missing odc-geo. --- pyresample/geometry.py | 15 +++++++++------ pyresample/test/test_geometry/test_area.py | 10 ++++++++++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/pyresample/geometry.py b/pyresample/geometry.py index 40af52458..0a45b3f38 100644 --- a/pyresample/geometry.py +++ b/pyresample/geometry.py @@ -56,6 +56,11 @@ except ImportError: da = None +try: + import odc.geo as odc_geo +except ModuleNotFoundError: + odc_geo = None + from pyproj import CRS from pyproj.enums import TransformDirection @@ -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. diff --git a/pyresample/test/test_geometry/test_area.py b/pyresample/test/test_geometry/test_area.py index 73307cdf4..0f84533ee 100644 --- a/pyresample/test/test_geometry/test_area.py +++ b/pyresample/test/test_geometry/test_area.py @@ -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