raster

utils.raster

Utility functions to handle merging and resampling of raster data.

These were developed to support merging raster files together (e.g., to cover a larger area), and resampling to a different grid size (e.g., 100x100m grids to 200x200m grids). The original design intention is for these to form part of gridded population data pre-processing.

Functions

Name Description
merge_raster_files Merge raster files together.
sum_resample_file Resample raster file (change grid resolution) by summing.

merge_raster_files

utils.raster.merge_raster_files(input_dir, output_dir, output_filename, subset_regex=None)

Merge raster files together.

Takes an input directory and merges all .tif files within it. Writes merged raster to file.

Parameters

Name Type Description Default
input_dir Union[str, pathlib.Path] Directory containing input raster files. required
output_dir Union[str, pathlib.Path] Directory to write output, merged raster file. required
output_filename str Filename of merged raster file (.tif extension required). required
subset_regex str Subset any raster files in the input directory using a regex, by default None which means no subsetting will occur. None

Returns

Type Description
dict A dictionary summarising the boundaries of all input rasters and the merged output. The “inputs” key is a list of the respective input boundaries. The “output” key is a list containing the bounds of the merged result. Useful for checking consistency of merged output.

Raises

Type Description
FileNotFoundError In scenarios where input_dir does not exist and if there are no .tif detectable within input_dir.

Notes

  1. This function does not provide any consistency checking of inputs and merged outputs (e.g., checking for overlapping inputs and ‘gaps’ in the merged outputs). This is primarily because merging is nuanced and numerous in the potential ways inputs can be merged. For this reason, it is down to the function’s user to ensure the merged output is consistent with the respective inputs. To this end, the bounds dict is returned to allow user consistency testing.

  2. The default rioxarry behaviours are assumed when merging inputs, i.e., the CRS, resolution and nodata values will be taken from the first input DataArray.

sum_resample_file

utils.raster.sum_resample_file(input_filepath, output_filepath, resample_factor=2)

Resample raster file (change grid resolution) by summing.

Takes an input raster file, resamples to a different grid resolution by summing constituent cells, and writes the resampled raster to file.

Parameters

Name Type Description Default
input_filepath Union[str, pathlib.Path] Input filpath of GeoTIFF file required
output_filepath Union[str, pathlib.Path] Output filepath for resampled GeoTIFF file required
resample_factor int Factor to resample input raster by, by default 2 which means the resolution will be decreased by 2 (e.g., input=100x100m then output=200 x200m) 2

Raises

Type Description
FileNotFoundError Unable to find input_filepath