Skip to content

Commit

Permalink
fix: fix crash in resample_cube_spatial and remove odc-algo dependency (
Browse files Browse the repository at this point in the history
#144)

remove odc-algo dependency
  • Loading branch information
LukeWeidenwalker authored Jul 21, 2023
1 parent 9354225 commit 64380e7
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 21 deletions.
7 changes: 0 additions & 7 deletions openeo_processes_dask/process_implementations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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",
Expand All @@ -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")
Expand All @@ -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}
)
Expand Down
2 changes: 0 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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]
Expand Down

0 comments on commit 64380e7

Please sign in to comment.