def assert_same_read_results(source, dst_shape, dst_dtype, dst_transform, dst_nodata, dst_projection, resampling): expected = np.empty(dst_shape, dtype=dst_dtype) with source.open() as src: rasterio.warp.reproject(src.data, expected, src_transform=src.transform, src_crs=str(src.crs), src_nodata=src.nodata, dst_transform=dst_transform, dst_crs=str(dst_projection), dst_nodata=dst_nodata, resampling=resampling) result = np.full(dst_shape, dst_nodata, dtype=dst_dtype) H, W = dst_shape dst_gbox = GeoBox(W, H, dst_transform, dst_projection) with datacube.set_options(reproject_threads=1): with source.open() as rdr: read_time_slice(rdr, result, dst_gbox, dst_nodata=dst_nodata, resampling=resampling) assert np.isclose(result, expected, atol=0, rtol=0.05, equal_nan=True).all() return result
def _read_from_source(source, dest, dst_transform, dst_nodata, dst_projection, resampling): """ Adapt old signature to new function, so that we can keep old tests at least for now """ H, W = dest.shape gbox = GeoBox(W, H, dst_transform, dst_projection) dest[:] = dst_nodata # new code assumes pre-populated image with source.open() as rdr: read_time_slice(rdr, dest, gbox, resampling=resampling, dst_nodata=dst_nodata)
def reproject_and_fuse(datasources: List[DataSource], destination: np.ndarray, dst_gbox: GeoBox, dst_nodata: Optional[Union[int, float]], resampling: str = 'nearest', fuse_func: Optional[FuserFunction] = None, skip_broken_datasets: bool = False): """ Reproject and fuse `sources` into a 2D numpy array `destination`. :param datasources: Data sources to open and read from :param destination: ndarray of appropriate size to read data into :param dst_gbox: GeoBox defining destination region :param skip_broken_datasets: Carry on in the face of adversity and failing reads. """ # pylint: disable=too-many-locals assert len(destination.shape) == 2 def copyto_fuser(dest: np.ndarray, src: np.ndarray) -> None: where_nodata = ( dest == dst_nodata) if not np.isnan(dst_nodata) else np.isnan(dest) np.copyto(dest, src, where=where_nodata) fuse_func = fuse_func or copyto_fuser destination.fill(dst_nodata) if len(datasources) == 0: return destination elif len(datasources) == 1: with ignore_exceptions_if(skip_broken_datasets): with datasources[0].open() as rdr: read_time_slice(rdr, destination, dst_gbox, resampling, dst_nodata) return destination else: # Multiple sources, we need to fuse them together into a single array buffer_ = np.full(destination.shape, dst_nodata, dtype=destination.dtype) for source in datasources: with ignore_exceptions_if(skip_broken_datasets): with source.open() as rdr: roi = read_time_slice(rdr, buffer_, dst_gbox, resampling, dst_nodata) if not roi_is_empty(roi): fuse_func(destination[roi], buffer_[roi]) buffer_[roi] = dst_nodata # clean up for next read return destination
def _read(gbox, resampling='nearest', fallback_nodata=None, dst_nodata=-999): with RasterFileDataSource(mm.path, 1, nodata=fallback_nodata).open() as rdr: yy = np.full(gbox.shape, dst_nodata, dtype=rdr.dtype) roi = read_time_slice(rdr, yy, gbox, resampling, dst_nodata) return yy, roi
def _read(gbox, resampling='nearest', fallback_nodata=-999, dst_nodata=-999, check_paste=False): with RasterFileDataSource(mm.path, 1, nodata=fallback_nodata).open() as rdr: if check_paste: # check that we are using paste paste_ok, reason = can_paste(compute_reproject_roi(rdr_geobox(rdr), gbox)) assert paste_ok is True, reason yy = np.full(gbox.shape, dst_nodata, dtype=rdr.dtype) roi = read_time_slice(rdr, yy, gbox, resampling, dst_nodata) return yy, roi