Skip to content

Commit

Permalink
Merge pull request #40 from digitalearthpacific/fix-odc-stac-load-nodata
Browse files Browse the repository at this point in the history
Pass nodata value onto odc-stac load
  • Loading branch information
alexgleith authored Jan 4, 2024
2 parents 7d51a7e + 250a25b commit 04ece54
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
10 changes: 10 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
hooks:
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/psf/black
rev: 22.10.0
hooks:
- id: black
17 changes: 4 additions & 13 deletions dep_tools/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,25 +167,16 @@ def _get_xr(
items,
areas: GeoDataFrame,
) -> DataArray | Dataset:
# For most EO data native dtype is int. Loading as such saves space but
# the only more-or-less universally accepted nodata value is nan,
# which is not available for int types. So we need to load as float and
# then replace existing nodata values (usually 0) with nan. At least
# I _think_ all this is necessary and there's not an easier way I didn't
# see in the docs.
areas_proj = areas.to_crs(self._current_epsg)
bounds = areas_proj.total_bounds.tolist()

data_type = "uint16" if self.keep_ints else "float32"

xr = load(
items,
geopolygon=areas,
crs=self._current_epsg,
chunks=self.dask_chunksize,
x=(bounds[0], bounds[2]),
y=(bounds[1], bounds[3]),
**self.odc_load_kwargs,
dtype=data_type,
nodata=self.nodata,
**self.odc_load_kwargs,
)

if self.nodata is not None:
Expand All @@ -212,7 +203,7 @@ def _get_xr(
.rio.write_crs(self._current_epsg)
.rio.write_nodata(float("nan"))
.rio.clip(
areas_proj.geometry,
areas.to_crs(self._current_epsg).geometry,
all_touched=True,
from_disk=True,
)
Expand Down

3 comments on commit 04ece54

@jessjaco
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexgleith what if areas is in a different projection than self._current_epsg?

@alexgleith
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if areas is in a different projection than self._current_epsg?

I'm pretty sure that odc-stac handles it. If we find a case where this doesn't work, we can fix it there.

@jessjaco
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, Fwiw that scenario happens with the coastline stuff (since projections are tile specific), so I will let you know

Please sign in to comment.