Joining arrays, where boundary is not along an axis #8615
-
I am looking for a solution to mosaic arrays - by this I mean if I start with three arrays a, b, c, every value in my result will be taken either from a, b, or c. The starting arrays overlap each other, and I want to define a boundary between where the result chooses one array over the other inside this region, based on some geometries that are not aligned to the x-y coordinates of the arrays. Problem Illustration
It is not important exactly where the boundary lies, as long as every position in the final array is given a value from one of the starting arrays. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
@harryC-space-intelligence thanks for the nice illustrations. If you're lucky enough to have satellite images with STAC metadata, the libraries stackstac and odc.stac have built-in mosaicing functions that take care of lots of details like ensuring your images are in the same CRS as well as using algorithms that work well with dask. If you don't have STAC metadata you might consider making it in order to work with these tools - it's pretty easy to create with rio-stac . That said, if you already have a dataset you can create a mosaic using Xarray.reduce with something like the following: def first_valid_value(arr, axis=None):
idx = np.nanargmax(np.isfinite(arr), axis=axis, keepdims=True)
vals = np.take_along_axis(arr, idx, axis=axis).squeeze()
return vals
mosaic = ds.value.reduce(first_valid_value, dim='landsat:scene_id') I uploaded some code that walks through this with public data, and am also including just a couple plots below that show what this reduction does https://gist.github.com/scottyhq/ed8247f3ae1d42543f7bbfb02a5fa8ad |
Beta Was this translation helpful? Give feedback.
Thanks @scottyhq for your answer.
Unfortunately my problem is slightly different (i.e. point 3: I want to join them such that the boundaries keep well away from the edges of the image footprints). If you look at my final illustration you can see how the join is equidistant from the edges of the images.
I found a way to implement this by looking for the indexes of null values along the rows of the data, something like this: