Skip to content

Commit

Permalink
FIX: Fix nodata casting in rasters_rio.rasterize for float values c…
Browse files Browse the repository at this point in the history
…asted into integers
  • Loading branch information
remi-braun committed Feb 17, 2025
1 parent ddea5e2 commit 4eac3da
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- FIX: Add more tolerances in `geometry.simplify_footprint` to allow further simplification, and a warning in case of failure
- FIX: Force 2D geometries in `rasters.crop` as `odc.geo.xr.crop` don't work with 3D geometries
- FIX: Fix nodata casting in `rasters_rio.rasterize` for float values casted into integers
- OPTIM: Only write rasters on disk with `windowed=True` in case of big rasters _(w/*h > 20,000*20,000 pixels)_
- DEPR: Remove mention to DS2 in Unistra (keep only the mounting points for legacy purposes)
- CI: Refactor `.gitlab-ci.yml` file with new GitLab templates
Expand Down
9 changes: 8 additions & 1 deletion sertit/rasters_rio.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,11 @@ def rasterize(
else:
nodata = default_nodata

if not np.can_cast(np.array(nodata, dtype=ds.dtypes[0]), dtype):
# Check if the nodata value can be casted into the new dtype
# Floating point values that can be converted to integers are allowed
if not np.can_cast(np.array(nodata, dtype=ds.dtypes[0]), dtype) and not (
"int" in str(dtype) and abs(nodata - int(nodata)) == 0
):
old_nodata = nodata
nodata = get_nodata_value_from_dtype(dtype)

Expand All @@ -555,6 +559,9 @@ def rasterize(
f"Impossible to cast nodata value ({old_nodata}) into the wanted dtype ({str(dtype)}). "
f"Default nodata value for this current dtype will be used ({nodata})."
)
LOGGER.debug(
f"input nodata: {old_nodata} - nodata coming from dtype: {nodata}."
)

# Rasterize vector
mask = features.rasterize(
Expand Down

0 comments on commit 4eac3da

Please sign in to comment.