Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions mapchete_eo/io/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@


class STACRasterBandProperties(BaseModel):
nodata: NodataVal = None
nodata: Optional[NodataVal] = None
data_type: Optional[str] = None
scale: float = 1.0
offset: float = 0.0

@staticmethod
def from_asset(
asset: pystac.Asset,
nodataval: NodataVal = None,
nodataval: Optional[NodataVal] = None,
) -> STACRasterBandProperties:
if asset.extra_fields.get("raster:offset") is not None:
if asset.extra_fields.get("raster:offset", {}):
properties = dict(
offset=asset.extra_fields.get("raster:offset"),
scale=asset.extra_fields.get("raster:scale"),
Expand Down Expand Up @@ -87,16 +87,21 @@ def asset_to_np_array(
)

logger.debug("reading asset %s and indexes %s ...", asset, indexes)
data = read_raster(
array = read_raster(
inp=path,
indexes=indexes,
grid=grid,
resampling=resampling.name,
dst_nodata=band_properties.nodata,
).data

).array
if apply_offset and band_properties.offset:
data_type = band_properties.data_type or data.dtype
logger.debug(
"apply offset %s and scale %s to asset %s",
band_properties.offset,
band_properties.scale,
asset,
)
data_type = band_properties.data_type or array.dtype

# determine value range for the target data_type
clip_min, clip_max = dtype_ranges[str(data_type)]
Expand All @@ -105,18 +110,17 @@ def asset_to_np_array(
if clip_min == band_properties.nodata:
clip_min += 1

data[:] = (
array[~array.mask] = (
(
((data * band_properties.scale) + band_properties.offset)
((array[~array.mask] * band_properties.scale) + band_properties.offset)
/ band_properties.scale
)
.round()
.clip(clip_min, clip_max)
.astype(data_type, copy=False)
.data
)

return data
return array


def get_assets(
Expand Down
Loading