jgrss / geowombat

GeoWombat: Utilities for geospatial data
https://geowombat.readthedocs.io
MIT License
182 stars 10 forks source link

Stack & Mosaic Advice? #307

Closed mmann1123 closed 3 months ago

mmann1123 commented 3 months ago

@jgrss Just wondering if you have some advice for stacking and mosaicing. I tried both of the following and am getting errors. Is there a way to do this?

with gw.open(bgrn1, stack_dim="band") as src1:
            with gw.open(bgrn2, stack_dim="band") as src2:

                with gw.open(
                    [bgrn1, bgrn2],
                    mosaic=True,
                    overlap="max",
                ) as out:
with gw.open(bgrn1, stack_dim="band") as src1:
                src1.gw.to_vrt("../lat_lon_file1.vrt")
            with gw.open(bgrn2, stack_dim="band") as src2:
                src1.gw.to_vrt("lat_lon_file2.vrt")

                with gw.open(
                    ["../lat_lon_file1.vrt", "lat_lon_file2.vrt"],
                    mosaic=True,
                    overlap="max",
                ) as out:

error for stack & mosaic

TypeError                                 Traceback (most recent call last)
File [/home/mmann1123/Documents/github/WB-spatial-features/1_create_mosaics.py:98](https://file+.vscode-resource.vscode-cdn.net/home/mmann1123/Documents/github/WB-spatial-features/1_create_mosaics.py:98)
     [93](https://file+.vscode-resource.vscode-cdn.net/home/mmann1123/Documents/github/WB-spatial-features/1_create_mosaics.py:93) with gw.open(part1, stack_dim="band") as src1:
     [94](https://file+.vscode-resource.vscode-cdn.net/home/mmann1123/Documents/github/WB-spatial-features/1_create_mosaics.py:94)     # src1.gw.to_vrt('../lat_lon_file1.vrt')
     [95](https://file+.vscode-resource.vscode-cdn.net/home/mmann1123/Documents/github/WB-spatial-features/1_create_mosaics.py:95)     with gw.open(part2, stack_dim="band") as src2:
     [96](https://file+.vscode-resource.vscode-cdn.net/home/mmann1123/Documents/github/WB-spatial-features/1_create_mosaics.py:96)         # src1.gw.to_vrt('lat_lon_file2.vrt')
---> [98](https://file+.vscode-resource.vscode-cdn.net/home/mmann1123/Documents/github/WB-spatial-features/1_create_mosaics.py:98)         with gw.open([src1, src2], mosaic=True, overlap="max") as out:
    [100](https://file+.vscode-resource.vscode-cdn.net/home/mmann1123/Documents/github/WB-spatial-features/1_create_mosaics.py:100)             gw.save(
    [101](https://file+.vscode-resource.vscode-cdn.net/home/mmann1123/Documents/github/WB-spatial-features/1_create_mosaics.py:101)                 out,
    [102](https://file+.vscode-resource.vscode-cdn.net/home/mmann1123/Documents/github/WB-spatial-features/1_create_mosaics.py:102)                 filename=f"../mosaic/S2_SR_{quarter}_south.tif",
   (...)
    [106](https://file+.vscode-resource.vscode-cdn.net/home/mmann1123/Documents/github/WB-spatial-features/1_create_mosaics.py:106)                 compress="lzw",
    [107](https://file+.vscode-resource.vscode-cdn.net/home/mmann1123/Documents/github/WB-spatial-features/1_create_mosaics.py:107)             )

File [~/miniconda3/envs/xr_fresh/lib/python3.9/site-packages/geowombat/core/api.py:510](https://file+.vscode-resource.vscode-cdn.net/home/mmann1123/Documents/github/WB-spatial-features/~/miniconda3/envs/xr_fresh/lib/python3.9/site-packages/geowombat/core/api.py:510), in open.__init__(self, filename, band_names, time_names, stack_dim, bounds, bounds_by, resampling, persist_filenames, netcdf_vars, mosaic, overlap, nodata, scale_factor, offset, dtype, scale_data, num_workers, **kwargs)
    [507](https://file+.vscode-resource.vscode-cdn.net/home/mmann1123/Documents/github/WB-spatial-features/~/miniconda3/envs/xr_fresh/lib/python3.9/site-packages/geowombat/core/api.py:507)     filename = parse_wildcard(filename)
    [509](https://file+.vscode-resource.vscode-cdn.net/home/mmann1123/Documents/github/WB-spatial-features/~/miniconda3/envs/xr_fresh/lib/python3.9/site-packages/geowombat/core/api.py:509) if "chunks" not in kwargs:
--> [510](https://file+.vscode-resource.vscode-cdn.net/home/mmann1123/Documents/github/WB-spatial-features/~/miniconda3/envs/xr_fresh/lib/python3.9/site-packages/geowombat/core/api.py:510)     with rio.open(filename[0]) as src:
    [511](https://file+.vscode-resource.vscode-cdn.net/home/mmann1123/Documents/github/WB-spatial-features/~/miniconda3/envs/xr_fresh/lib/python3.9/site-packages/geowombat/core/api.py:511)         w = src.block_window(1, 0, 0)
    [512](https://file+.vscode-resource.vscode-cdn.net/home/mmann1123/Documents/github/WB-spatial-features/~/miniconda3/envs/xr_fresh/lib/python3.9/site-packages/geowombat/core/api.py:512)         kwargs["chunks"] = (band_chunks, w.height, w.width)

File [~/miniconda3/envs/xr_fresh/lib/python3.9/site-packages/rasterio/env.py:444](https://file+.vscode-resource.vscode-cdn.net/home/mmann1123/Documents/github/WB-spatial-features/~/miniconda3/envs/xr_fresh/lib/python3.9/site-packages/rasterio/env.py:444), in ensure_env_with_credentials.<locals>.wrapper(*args, **kwds)
    [441](https://file+.vscode-resource.vscode-cdn.net/home/mmann1123/Documents/github/WB-spatial-features/~/miniconda3/envs/xr_fresh/lib/python3.9/site-packages/rasterio/env.py:441)     session = DummySession()
    [443](https://file+.vscode-resource.vscode-cdn.net/home/mmann1123/Documents/github/WB-spatial-features/~/miniconda3/envs/xr_fresh/lib/python3.9/site-packages/rasterio/env.py:443) with env_ctor(session=session):
...
    AREA_OR_POINT:       Area
    filename:            ['B2_S2_SR_2020_Q01-linear_south-0000000000-00000000...
    resampling:          nearest
    _data_are_separate:  1
    _data_are_stacked:   1

Error for vrt:

KeyError                                  Traceback (most recent call last)
File [/home/mmann1123/Documents/github/WB-spatial-features/1_create_mosaics.py:94](https://file+.vscode-resource.vscode-cdn.net/home/mmann1123/Documents/github/WB-spatial-features/1_create_mosaics.py:94)
     [92](https://file+.vscode-resource.vscode-cdn.net/home/mmann1123/Documents/github/WB-spatial-features/1_create_mosaics.py:92) # with gw.config.update(ref_bounds=bounds):
     [93](https://file+.vscode-resource.vscode-cdn.net/home/mmann1123/Documents/github/WB-spatial-features/1_create_mosaics.py:93) with gw.open(part1, stack_dim="band") as src1:
---> [94](https://file+.vscode-resource.vscode-cdn.net/home/mmann1123/Documents/github/WB-spatial-features/1_create_mosaics.py:94)     src1.gw.to_vrt("../lat_lon_file1.vrt")
     [95](https://file+.vscode-resource.vscode-cdn.net/home/mmann1123/Documents/github/WB-spatial-features/1_create_mosaics.py:95) with gw.open(part2, stack_dim="band") as src2:
     [96](https://file+.vscode-resource.vscode-cdn.net/home/mmann1123/Documents/github/WB-spatial-features/1_create_mosaics.py:96)     src1.gw.to_vrt("lat_lon_file2.vrt")

File [~/miniconda3/envs/xr_fresh/lib/python3.9/site-packages/geowombat/core/geoxarray.py:977](https://file+.vscode-resource.vscode-cdn.net/home/mmann1123/Documents/github/WB-spatial-features/~/miniconda3/envs/xr_fresh/lib/python3.9/site-packages/geowombat/core/geoxarray.py:977), in GeoWombatAccessor.to_vrt(self, filename, overwrite, resampling, nodata, init_dest_nodata, warp_mem_limit)
    [936](https://file+.vscode-resource.vscode-cdn.net/home/mmann1123/Documents/github/WB-spatial-features/~/miniconda3/envs/xr_fresh/lib/python3.9/site-packages/geowombat/core/geoxarray.py:936) def to_vrt(
    [937](https://file+.vscode-resource.vscode-cdn.net/home/mmann1123/Documents/github/WB-spatial-features/~/miniconda3/envs/xr_fresh/lib/python3.9/site-packages/geowombat/core/geoxarray.py:937)     self,
    [938](https://file+.vscode-resource.vscode-cdn.net/home/mmann1123/Documents/github/WB-spatial-features/~/miniconda3/envs/xr_fresh/lib/python3.9/site-packages/geowombat/core/geoxarray.py:938)     filename: T.Union[str, _Path],
   (...)
    [943](https://file+.vscode-resource.vscode-cdn.net/home/mmann1123/Documents/github/WB-spatial-features/~/miniconda3/envs/xr_fresh/lib/python3.9/site-packages/geowombat/core/geoxarray.py:943)     warp_mem_limit: int = 128,
    [944](https://file+.vscode-resource.vscode-cdn.net/home/mmann1123/Documents/github/WB-spatial-features/~/miniconda3/envs/xr_fresh/lib/python3.9/site-packages/geowombat/core/geoxarray.py:944) ) -> None:
    [945](https://file+.vscode-resource.vscode-cdn.net/home/mmann1123/Documents/github/WB-spatial-features/~/miniconda3/envs/xr_fresh/lib/python3.9/site-packages/geowombat/core/geoxarray.py:945)     """Writes a file to a VRT file.
    [946](https://file+.vscode-resource.vscode-cdn.net/home/mmann1123/Documents/github/WB-spatial-features/~/miniconda3/envs/xr_fresh/lib/python3.9/site-packages/geowombat/core/geoxarray.py:946) 
    [947](https://file+.vscode-resource.vscode-cdn.net/home/mmann1123/Documents/github/WB-spatial-features/~/miniconda3/envs/xr_fresh/lib/python3.9/site-packages/geowombat/core/geoxarray.py:947)     Args:
   (...)
    [975](https://file+.vscode-resource.vscode-cdn.net/home/mmann1123/Documents/github/WB-spatial-features/~/miniconda3/envs/xr_fresh/lib/python3.9/site-packages/geowombat/core/geoxarray.py:975)         >>>         src.gw.to_vrt('output.vrt')
    [976](https://file+.vscode-resource.vscode-cdn.net/home/mmann1123/Documents/github/WB-spatial-features/~/miniconda3/envs/xr_fresh/lib/python3.9/site-packages/geowombat/core/geoxarray.py:976)     """
--> [977](https://file+.vscode-resource.vscode-cdn.net/home/mmann1123/Documents/github/WB-spatial-features/~/miniconda3/envs/xr_fresh/lib/python3.9/site-packages/geowombat/core/geoxarray.py:977)     to_vrt(
    [978](https://file+.vscode-resource.vscode-cdn.net/home/mmann1123/Documents/github/WB-spatial-features/~/miniconda3/envs/xr_fresh/lib/python3.9/site-packages/geowombat/core/geoxarray.py:978)         self._obj,
    [979](https://file+.vscode-resource.vscode-cdn.net/home/mmann1123/Documents/github/WB-spatial-features/~/miniconda3/envs/xr_fresh/lib/python3.9/site-packages/geowombat/core/geoxarray.py:979)         filename,
...
   (...)
    [549](https://file+.vscode-resource.vscode-cdn.net/home/mmann1123/Documents/github/WB-spatial-features/~/miniconda3/envs/xr_fresh/lib/python3.9/site-packages/geowombat/core/io.py:549)     outputSRS=data.crs,
    [550](https://file+.vscode-resource.vscode-cdn.net/home/mmann1123/Documents/github/WB-spatial-features/~/miniconda3/envs/xr_fresh/lib/python3.9/site-packages/geowombat/core/io.py:550) )

KeyError:
jgrss commented 3 months ago

Hey, can you post the input data so I can better see what you're trying to do?

mmann1123 commented 3 months ago

@jgrss Here's an example:

from geowombat.data import (
    l8_224077_20200518_B2,
    l8_224078_20200518_B2,
    l8_224077_20200518_B3,
    l8_224078_20200518_B3,
)

with gw.open([l8_224077_20200518_B2, l8_224077_20200518_B3], stack_dim="band") as src1:
    with gw.open(
        [l8_224078_20200518_B2, l8_224078_20200518_B3], stack_dim="band"
    ) as src2:

        with gw.open([src1, src2], mosaic=True) as src:
            print(src)
jgrss commented 3 months ago

I think you can do the first approach if you set the overall bounds.

import dask.array as da
from geowombat.backends.rasterio_ import get_file_bounds

# Get the union of all bounding boxes
# Note that you probably want to pass `crs` in this method.
bounds = get_file_bounds([l8_224077_20200518_B2, l8_224078_20200518_B2], bounds_by="union", return_bounds=True)

with gw.config.update(ref_bounds=bounds):
    with gw.open([l8_224077_20200518_B2, l8_224077_20200518_B3], band_names=[1, 2], stack_dim="band") as src1:
        with gw.open([l8_224078_20200518_B2, l8_224078_20200518_B3], band_names=[1, 2], stack_dim="band") as src2:
            darray = da.maximum(src1, src2)
            print(darray)
jgrss commented 3 months ago

As for the VRT approach, that looks like it could work. However, there is a bug when passing the default integer EPSG -- gdal.BuildVRTOptions outputSRS does not like EPSG integers, so you could try setting the DataArray CRS attribute as a WKT string like src1.attrs["crs"] = src1.gw.crs_to_pyproj.to_wkt().

jgrss commented 3 months ago

Follow-up on the first option. In the case of more than two arrays, you might want to use numpy.

import numpy as np

with gw.config.update(ref_bounds=bounds):
    with gw.open([l8_224077_20200518_B2, l8_224077_20200518_B3], band_names=[1, 2], stack_dim="band") as src1:
        with gw.open([l8_224078_20200518_B2, l8_224078_20200518_B3], band_names=[1, 2], stack_dim="band") as src2:
            darray = np.maximum.reduce(src1, src2, src3, src4, ...)
            print(darray)
mmann1123 commented 3 months ago

ok thanks I will play around with it. Looks like the VRT option is still throwing the error.