mapchete.io.raster module

Wrapper functions around rasterio and useful raster functions.

class mapchete.io.raster.RasterWindowMemoryFile(in_tile=None, in_data=None, out_profile=None, out_tile=None, tags=None)

Bases: object

Context manager around rasterio.io.MemoryFile.

class mapchete.io.raster.RasterioRemoteWriter(path, *args, fs=None, in_memory=True, **kwargs)

Bases: object

class mapchete.io.raster.ReferencedRaster(data, affine, bounds, crs)

Bases: tuple

affine

Alias for field number 1

bounds

Alias for field number 2

crs

Alias for field number 3

data

Alias for field number 0

mapchete.io.raster.bounds_to_ranges(out_bounds=None, in_affine=None, in_shape=None)

Return bounds range values from geolocated input.

Parameters
out_boundstuple

left, bottom, right, top

in_affineAffine

input geolocation

in_shapetuple

input shape

Returns
minrow, maxrow, mincol, maxcol
mapchete.io.raster.convert_raster(inp, out, overwrite=False, exists_ok=True, **kwargs)

Convert raster file to a differernt format.

When kwargs are given, the operation will be conducted by rasterio, without kwargs, the file is simply copied to the destination using fsspec.

Parameters
inpstr

Path to input file.

outstr

Path to output file.

overwritebool

Overwrite output file. (default: False)

skip_existsbool

Skip conversion if outpu already exists. (default: True)

kwargsmapping

Creation parameters passed on to output file.

mapchete.io.raster.create_mosaic(tiles, nodata=0)

Create a mosaic from tiles.

Tiles must be connected (also possible over Antimeridian), otherwise strange things can happen!

Parameters
tilesiterable

an iterable containing tuples of a BufferedTile and an array

nodatainteger or float

raster nodata value to initialize the mosaic with (default: 0)

Returns
mosaicReferencedRaster
mapchete.io.raster.extract_from_array(in_raster=None, in_affine=None, out_tile=None)

Extract raster data window array.

Parameters
in_rasterarray or ReferencedRaster
in_affineAffine required if in_raster is an array
out_tileBufferedTile
Returns
extracted arrayarray
mapchete.io.raster.memory_file(data=None, profile=None)

Return a rasterio.io.MemoryFile instance from input.

Parameters
dataarray

array to be written

profiledict

rasterio profile for MemoryFile

mapchete.io.raster.prepare_array(data, masked=True, nodata=0, dtype='int16')

Turn input data into a proper array for further usage.

Output array is always 3-dimensional with the given data type. If the output is masked, the fill_value corresponds to the given nodata value and the nodata value will be burned into the data array.

Parameters
dataarray or iterable

array (masked or normal) or iterable containing arrays

nodatainteger or float

nodata value (default: 0) used if input is not a masked array and for output array

maskedbool

return a NumPy Array or a NumPy MaskedArray (default: True)

dtypestring

data type of output array (default: “int16”)

Returns
arrayarray
mapchete.io.raster.rasterio_write(path, mode=None, fs=None, in_memory=True, *args, **kwargs)

Wrap rasterio.open() but handle bucket upload if path is remote.

Parameters
pathstr

Path to write to.

modestr

One of the rasterio.open() modes.

fsfsspec.FileSystem

Target filesystem.

in_memorybool

On remote output store an in-memory file instead of writing to a tempfile.

argslist

Arguments to be passed on to rasterio.open()

kwargsdict

Keyword arguments to be passed on to rasterio.open()

Returns
RasterioRemoteWriter if target is remote, otherwise return rasterio.open().
mapchete.io.raster.read_raster(inp, **kwargs)
mapchete.io.raster.read_raster_no_crs(input_file, indexes=None, gdal_opts=None)

Wrapper function around rasterio.open().read().

Parameters
input_filestr

Path to file

indexesint or list

Band index or list of band indexes to be read.

gdal_optsdict

GDAL options passed on to rasterio.Env()

Returns
MaskedArray
Raises
FileNotFoundError if file cannot be found.
mapchete.io.raster.read_raster_window(input_files, tile, indexes=None, resampling='nearest', src_nodata=None, dst_nodata=None, dst_dtype=None, gdal_opts=None, skip_missing_files=False)

Return NumPy arrays from an input raster.

NumPy arrays are reprojected and resampled to tile properties from input raster. If tile boundaries cross the antimeridian, data on the other side of the antimeridian will be read and concatenated to the numpy array accordingly.

Parameters
input_filesstring or list

path to a raster file or list of paths to multiple raster files readable by rasterio.

tileTile

a Tile object

indexeslist or int

Either a list of band indexes or a single band index. If only a single band index is given, the function returns a 2D array, otherwise a 3D array. None will read all. This behavior mimicks rasterios.

resamplingstring

one of “nearest”, “average”, “bilinear” or “lanczos”

src_nodataint or float, optional

if not set, the nodata value from the source dataset will be used

dst_nodataint or float, optional

if not set, the nodata value from the source dataset will be used

gdal_optsdict

GDAL options passed on to rasterio.Env()

Returns
rasterMaskedArray
mapchete.io.raster.resample_from_array(in_raster=None, in_affine=None, out_tile=None, in_crs=None, resampling='nearest', nodataval=None, nodata=0)

Extract and resample from array to target tile.

Parameters
in_rasterarray
in_affineAffine
out_tileBufferedTile
resamplingstring

one of rasterio’s resampling methods (default: nearest)

nodatainteger or float

raster nodata value (default: 0)

Returns
resampled arrayarray
mapchete.io.raster.tiles_to_affine_shape(tiles)

Return Affine and shape of combined tiles.

Parameters
tilesiterable

an iterable containing BufferedTiles

Returns
Affine, Shape
mapchete.io.raster.write_raster_window(in_tile=None, in_data=None, out_profile=None, out_tile=None, out_path=None, tags=None, fs=None, write_empty=False, **kwargs)

Write a window from a numpy array to an output file.

Parameters
in_tileBufferedTile

BufferedTile with a data attribute holding NumPy data

in_dataarray
out_profiledictionary

metadata dictionary for rasterio

out_tileTile

provides output boundaries; if None, in_tile is used

out_pathstring

output path to write to

tagsoptional tags to be added to GeoTIFF file