skmap.io.base.read_rasters_cpp#
- read_rasters_cpp(raster_files=[], band=1, window=None, n_jobs=8, out_data=None, out_idx=None, dtype=<class 'numpy.float32'>, gdal_opts={}, verbose=False)[source]#
Read rasters in parallel using the C++ backend, aggregating them into a single array.
- Parameters:
raster_files (
Union[List[Union[str,Path]],str,Path]) – A list with the raster paths.band (
Union[List[int],int]) – The band to be read from each raster filewindow (
Optional[Window]) – The window (if any) to read from the rastern_jobs (
int) – The number threads to read in parallelout_data (
Optional[ndarray[tuple[Any,...],dtype[float32]]]) – a pre-allocated array to write intodtype (
type) – Datatype (currently only np.float32 is supported)gdal_opts (
dict) – additional options to be passed to GDALverbose – Whether to print extra output
- Returns:
A 2D array of
n_bandsbyn_pixels- Return type:
NDArray[np.float32]
Examples
>>> import rasterio >>> import tempfile >>> import numpy as np >>> from skmap.io.base import read_rasters_cpp >>> from pathlib import Path >>> # Create a dummy raster >>> with tempfile.TemporaryDirectory() as tempdir: ... band_1 = np.random.rand(100,100).astype(np.float32) ... band_2 = np.random.rand(100,100).astype(np.float32) ... transform = rasterio.transform.from_origin(0,0,1,1) ... raster_name = Path(tempdir)/"example.tif" ... with rasterio.open( ... raster_name, ... "w", ... height=100, ... width=100, ... count=2, ... dtype=np.float32, ... crs="EPSG:4326", ... transform=transform ... ) as raster: ... raster.write(band_1, 1) ... raster.write(band_2, 2) ... # read the raster bands in parallel ... bands = read_rasters_cpp(raster_files=[raster_name, raster_name], band=[1,2]) ... # the shape is bands x n_pix ... assert bands.shape == (2,100*100) ... # bands are selected as above ... np.testing.assert_equal(band_1.reshape(100*100), bands[0,:]) ... np.testing.assert_equal(band_2.reshape(100*100), bands[1,:])