From 64380e76545f39d5efcd6e5c13ebdd2eeb38e181 Mon Sep 17 00:00:00 2001 From: Lukas Weidenholzer <17790923+LukeWeidenwalker@users.noreply.github.com> Date: Fri, 21 Jul 2023 14:41:35 +0200 Subject: [PATCH] fix: fix crash in resample_cube_spatial and remove odc-algo dependency (#144) remove odc-algo dependency --- .../process_implementations/__init__.py | 7 ------- .../experimental/resample.py | 21 ++++++++----------- pyproject.toml | 2 -- 3 files changed, 9 insertions(+), 21 deletions(-) diff --git a/openeo_processes_dask/process_implementations/__init__.py b/openeo_processes_dask/process_implementations/__init__.py index 614826e4..1129749d 100644 --- a/openeo_processes_dask/process_implementations/__init__.py +++ b/openeo_processes_dask/process_implementations/__init__.py @@ -15,13 +15,6 @@ "Did not load machine learning processes due to missing dependencies: Install them like this: `pip install openeo-processes-dask[implementations, ml]`" ) -try: - from .experimental import * -except ImportError as e: - logger.warning( - "Did not load experimental processes due to missing dependencies. Install them like this: `pip install openeo-processes-dask[implementations, experimental]`" - ) - import rioxarray as rio # Required for the .rio accessor on xarrays. import openeo_processes_dask.process_implementations.cubes._xr_interop diff --git a/openeo_processes_dask/process_implementations/experimental/resample.py b/openeo_processes_dask/process_implementations/experimental/resample.py index cf273d0f..e349f23e 100644 --- a/openeo_processes_dask/process_implementations/experimental/resample.py +++ b/openeo_processes_dask/process_implementations/experimental/resample.py @@ -1,4 +1,3 @@ -import odc.algo import rioxarray # needs to be imported to set .rio accessor on xarray objects. from openeo_processes_dask.process_implementations.data_model import RasterCube @@ -9,13 +8,6 @@ def resample_cube_spatial( data: RasterCube, target: RasterCube, method="near", options=None ) -> RasterCube: - # NOTE: Using the odc-algo library is not great, because it is only a random collection of experimental opendatacube features - # but we've investigated all other available alternatives for resampling that are currently available with dask and none do the job. - # We've tested pyresample (did not work at all and requires loads of helper code), - # rasterio.reproject_match (loads all the data into memory to do gdal.warp) and odc-geo (doesn't support dask yet). - # Github issue tracking this feature in rioxarray: https://github.com/corteva/rioxarray/issues/119 - # Github issue tracking this feature in odc-geo: https://github.com/opendatacube/odc-geo/issues/26 - methods_list = [ "near", "bilinear", @@ -31,8 +23,12 @@ def resample_cube_spatial( "q3", ] + # ODC reproject requires y to be before x required_dim_order = ( - data.openeo.band_dims + data.openeo.temporal_dims + data.openeo.spatial_dims + data.openeo.band_dims + + data.openeo.temporal_dims + + tuple(data.openeo.y_dim) + + tuple(data.openeo.x_dim) ) data_reordered = data.transpose(*required_dim_order, missing_dims="ignore") @@ -47,13 +43,14 @@ def resample_cube_spatial( f"[{', '.join(methods_list)}]" ) - resampled_data = odc.algo._warp.xr_reproject( - data_reordered, target_reordered.geobox, resampling=method + resampled_data = data_reordered.odc.reproject( + target_reordered.odc.geobox, resampling=method ) + resampled_data.rio.write_crs(target_reordered.rio.crs, inplace=True) try: - # xr_reproject renames the coordinates according to the geobox, this undoes that. + # odc.reproject renames the coordinates according to the geobox, this undoes that. resampled_data = resampled_data.rename( {"longitude": data.openeo.x_dim, "latitude": data.openeo.y_dim} ) diff --git a/pyproject.toml b/pyproject.toml index a18e0a2f..f74fee1d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -31,7 +31,6 @@ rasterio = { version = "^1.3.4", optional = true } dask-geopandas = { version = ">=0.2.0,<1", optional = true } xgboost = { version = "^1.5.1", optional = true } rioxarray = { version = ">=0.12.0,<1", optional = true } -odc-algo = { version = "==0.2.3", optional = true } openeo-pg-parser-networkx = { version = ">=2023.5.1", optional = true } odc-geo = { version = ">=0.3.2,<1", optional = true } stac_validator = { version = ">=3.3.1", optional = true } @@ -50,7 +49,6 @@ pytest-cov = "^4.0.0" [tool.poetry.extras] implementations = ["geopandas", "xarray", "dask", "rasterio", "dask-geopandas", "rioxarray", "openeo-pg-parser-networkx", "odc-geo", "stackstac", "planetary_computer", "pystac_client", "stac_validator"] -experimental = ["odc-algo"] ml = ["xgboost"] [tool.pytest.ini_options]