raise ValueError("Must specify save_dir")
def rxr_reproject_tiff_to_target(
src_tiff_file: Union[str, Path],
target_tiff_file: Union[str, Path],
dest_path: Union[str, Path] = None,
verbose=False,
):
# f"shape: {raster.rio.shape}\n"
# f"resolution: {raster.rio.resolution()}\n"
# f"bounds: {raster.rio.bounds()}\n"
# f"sum: {raster.sum().item()}\n"
# f"CRS: {raster.rio.crs}\n"
with rxr.open_rasterio(src_tiff_file, masked=True) as src_file, rxr.open_rasterio(
target_tiff_file, masked=True
) as target_file:
if src_file.rio.crs is None:
src_file.rio.write_crs(4326, inplace=True)
print("Warning, no CRS present in src, assuming EPSG:4326")
# NB by default uses nearest-neighbour resampling
# - simplest but often worse. Chosen bilinear here
# instead as that's what GEE uses, so at least
# now consistent for all data.
# TODO: Consider if alternative resampling scheme
# to bilinear is better for this task
rxr_match = src_file.rio.reproject_match(
target_file, resampling=rasterio.enums.Resampling(1)
)
if verbose:
print_tif_metadata(rxr_match)
print_tif_metadata(target_file)
# fig, ax = plt.subplots()
# rxr_match.plot(ax=ax)
# fig2, ax2 = plt.subplots()
# target_file.plot(ax=ax2)
if dest_path is not None:
dest_path = Path(dest_path)
rxr_match.rio.to_raster(dest_path)
else:
return rxr_match
def geotiff_to_df(
geotiff_filepath: str, spec_band_names: List[str] = None, verbose=False
):
"""Convert a geotiff file to a pandas dataframe,
and print some additional info.
:param geotiff_filepath: path to a geotiff file
:type geotiff_filepath: str
:param spec_band_names: Specified band names - only used
if these are not specified in
the GeoTIFF itself, at which
point they are mandatory
:type spec_band_names: List[str], optional
:param verbose: verbose output, defaults to False
:type verbose: bool, optional
:returns: pandas dataframe
"""
# # NB quadkeys are defined on Mercator projection, so must reproject
to bilinear is better for this task
rxr_match.plot(ax=ax)
fig2, ax2 = plt.subplots()
target_file.plot(ax=ax2)
https://github.com/fitzgeraldja/stc_unicef_cpi/blob/59645f669b9067592510e76f173959b29011486b/src/stc_unicef_cpi/data/process_geotiff.py#L102