Skip to content
Snippets Groups Projects
Commit 5d0c7556 authored by paul.tresson_ird.fr's avatar paul.tresson_ird.fr
Browse files

redo merge function to pass list of paths rather than list of rasterio dataset

parent 2e542dba
No related branches found
No related tags found
No related merge requests found
...@@ -24,6 +24,13 @@ def custom_method_avg(merged_data, new_data, merged_mask, new_mask, **kwargs): ...@@ -24,6 +24,13 @@ def custom_method_avg(merged_data, new_data, merged_mask, new_mask, **kwargs):
np.logical_and(merged_mask, mask, out=mask) np.logical_and(merged_mask, mask, out=mask)
np.copyto(merged_data, new_data, where=mask, casting="unsafe") np.copyto(merged_data, new_data, where=mask, casting="unsafe")
def get_extents(raster_files):
extents = []
for file in raster_files:
with rasterio.open(file, 'r') as src:
bounds = src.bounds
extents.append(bounds)
return extents
def merge_tiles( def merge_tiles(
tiles: list, tiles: list,
...@@ -37,8 +44,9 @@ def merge_tiles( ...@@ -37,8 +44,9 @@ def merge_tiles(
cf. https://amanbagrecha.github.io/posts/2022-07-31-merge-rasters-the-modern-way-using-python/index.html cf. https://amanbagrecha.github.io/posts/2022-07-31-merge-rasters-the-modern-way-using-python/index.html
""" """
file_handler = [rasterio.open(ds) for ds in tiles] # file_handler = [rasterio.open(ds) for ds in tiles]
extents = [ds.bounds for ds in file_handler] # extents = [ds.bounds for ds in file_handler]
extents = get_extents(tiles)
# Extract individual bounds # Extract individual bounds
lefts, bottoms, rights, tops = zip(*extents) lefts, bottoms, rights, tops = zip(*extents)
union_extent = ( union_extent = (
...@@ -52,30 +60,18 @@ def merge_tiles( ...@@ -52,30 +60,18 @@ def merge_tiles(
method = custom_method_avg method = custom_method_avg
# memfile = MemoryFile() # memfile = MemoryFile()
try: merge(
merge( # sources=file_handler, # list of dataset objects opened in 'r' mode
sources=file_handler, # list of dataset objects opened in 'r' mode sources=tiles, # try without rasterio.open
bounds=union_extent, # tuple bounds=union_extent, # tuple
nodata=nodata, # float nodata=nodata, # float
dtype=dtype, # dtype dtype=dtype, # dtype
# resampling=Resampling.nearest, # resampling=Resampling.nearest,
method=method, # strategy to combine overlapping rasters method=method, # strategy to combine overlapping rasters
# dst_path=memfile.name, # str or PathLike to save raster # dst_path=memfile.name, # str or PathLike to save raster
dst_path=dst_path, dst_path=dst_path,
# dst_kwds={'blockysize':512, 'blockxsize':512} # Dictionary # dst_kwds={'blockysize':512, 'blockxsize':512} # Dictionary
) )
except TypeError:
merge(
datasets=file_handler, # list of dataset objects opened in 'r' mode
bounds=union_extent, # tuple
nodata=nodata, # float
dtype=dtype, # dtype
# resampling=Resampling.nearest,
method=method, # strategy to combine overlapping rasters
# dst_path=memfile.name, # str or PathLike to save raster
dst_path=dst_path,
# dst_kwds={'blockysize':512, 'blockxsize':512} # Dictionary
)
def get_mean_sd_by_band(path, force_compute=True, ignore_zeros=True, subset=1_000): def get_mean_sd_by_band(path, force_compute=True, ignore_zeros=True, subset=1_000):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment