def _reproject_block_impl( src: np.ndarray, src_geobox: GeoBox, dst_geobox: GeoBox, resampling: str = "nearest", src_nodata: Optional[NodataType] = None, dst_nodata: Optional[NodataType] = None, axis: int = 0, ) -> np.ndarray: dst_shape = src.shape[:axis] + dst_geobox.shape + src.shape[axis + 2:] dst = np.empty(dst_shape, dtype=src.dtype) if dst.ndim == 2 or (dst.ndim == 3 and axis == 1): rio_reproject(src, dst, src_geobox, dst_geobox, resampling, src_nodata, dst_nodata) else: for prefix in np.ndindex(src.shape[:axis]): rio_reproject( src[prefix], dst[prefix], src_geobox, dst_geobox, resampling, src_nodata, dst_nodata, ) return dst
def reproject_array(src, nodata, s_geobox, d_geobox, resampling): """ Reproject a numpy array. """ dst = numpy.full(d_geobox.shape, fill_value=nodata, dtype=src.dtype) rio_reproject(src=src, dst=dst, s_gbox=s_geobox, d_gbox=d_geobox, resampling=resampling_s2rio(resampling), src_nodata=nodata, dst_nodata=nodata) return dst
def test_rio_reproject(): src = np.zeros((128, 256), dtype='int16') src[10:20, 30:50] = 33 s_gbox = AlbersGS.tile_geobox((15, -40))[:src.shape[0], :src.shape[1]] dst = np.zeros_like(src) dst_ = rio_reproject(src, dst, s_gbox, gbx.translate_pix(s_gbox, 30, 10), resampling='nearest') assert dst_ is dst assert (dst[:10, :20] == 33).all() assert (dst[10:, :] == 0).all() assert (dst[:, 20:] == 0).all() # check GDAL int8 limitation work-around src = src.astype('int8') dst = np.zeros_like(src) dst_ = rio_reproject(src, dst, s_gbox, gbx.translate_pix(s_gbox, 30, 10), resampling='nearest') assert dst_ is dst assert (dst[:10, :20] == 33).all() assert (dst[10:, :] == 0).all() assert (dst[:, 20:] == 0).all() # check GDAL int8 limitation work-around, with no-data src = src.astype('int8') dst = np.zeros_like(src) dst_ = rio_reproject(src, dst, s_gbox, gbx.translate_pix(s_gbox, 30, 10), src_nodata=0, dst_nodata=-3, resampling='nearest') assert dst_ is dst assert (dst[:10, :20] == 33).all() assert (dst[10:, :] == -3).all() assert (dst[:, 20:] == -3).all()