Replies: 3 comments 10 replies
-
Sorry for the delay! I was sidetracked by gjoseph92/stackstac#113, and forgot to return to this answer. One opening question: can you confirm that the desired bounding box is correct? If I plot the bounds on a map (yellow) with the STAC Item footprints (blue), I get The bounding box in red is the bounding box for the item footprints, and is what I use below. Next, you might want to confirm the expected data types. The source data are bounds = rasterio.warp.transform_bounds(4326, epsg, *df.unary_union.bounds)
data = stackstac.stack(
signed_items,
assets=["data"],
chunksize=2000,
resolution=1000,
epsg=epsg,
resampling=Resampling.average,
dtype="int16",
fill_value=0,
bounds=bounds,
).drop('band').squeeze()
data We get something that should behave a bit better in terms of memory. Notice that I also increased Next, we hit gjoseph92/stackstac#92. I'll put up a PR to fix that shortly, but in the meantime, we can define our own mosaic: from typing import Hashable, Sequence, Union
import numpy as np
import xarray as xr
# TODO `fill_value`s besides NaN
def _mosaic(arr, axis, reverse: bool = False, fill_value=np.nan):
ax_length = arr.shape[axis]
# "normal" means last -> first, "reversed" means first -> last,
# so we apply `reversed` in the opposite case you'd expect
indices = iter(range(ax_length) if reverse else reversed(range(ax_length)))
out = np.take(arr, next(indices), axis=axis)
for i in indices:
layer = np.take(arr, i, axis=axis)
if np.isnan(fill_value):
out = np.where(np.isnan(out), layer, out)
else:
out = np.where(out == fill_value, layer, out)
return out
def mosaic(
arr: xr.DataArray,
dim: Union[None, Hashable, Sequence[Hashable]] = None,
axis: Union[None, int, Sequence[int]] = 0,
reverse: bool = False,
fill_value=np.nan
):
return arr.reduce(_mosaic, dim=dim, axis=axis, keep_attrs=True, reverse=reverse, fill_value=fill_value) Which we can use as result = mosaic(data, fill_value=0)
result But we still run into memory issues :/ I'll post an update later with some more investigation. You might want to keep an eye on gjoseph92/stackstac#66. There might be some performance issues with |
Beta Was this translation helpful? Give feedback.
-
I just tested this again with 0.3.1 on the Planetary Comptuer hub and it is still not working. If I set dtype='uint16' and fill_value=0, I get the error:
If I remove the dtype and fill_value options, it runs and then it seems to get into memory issues and I get a Killedworker error. |
Beta Was this translation helpful? Give feedback.
-
Try setting |
Beta Was this translation helpful? Give feedback.
-
Hi, I'm trying to create a mosaic of DEM data at 1km resolution that I then want to save to disk. The mosaic resulting from the code below is mostly empty, with a few blocks where there is data. I've tried with alos-dem and cop-dem-glo-90. Any hints on how I can fix this?
Beta Was this translation helpful? Give feedback.
All reactions