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.empty(dst_shape, dtype=dst_dtype) with datacube.set_options(reproject_threads=1): read_from_source(source, result, dst_transform=dst_transform, dst_nodata=dst_nodata, dst_projection=dst_projection, resampling=resampling) assert np.isclose(result, expected, atol=0, rtol=0.05, equal_nan=True).all() return result
def _test_helper(source, dst_shape, dst_dtype, dst_transform, dst_nodata, dst_projection, resampling, **kwargs): result = numpy.empty(dst_shape, dtype=dst_dtype) read_from_source(source, result, dst_transform=dst_transform, dst_nodata=dst_nodata, dst_projection=dst_projection, resampling=resampling, **kwargs) expected = numpy.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, **kwargs) assert numpy.isclose(result, expected, atol=0, rtol=0.03, equal_nan=True).all() return result
def test_read_data_from_outside_file_region(self, make_sample_netcdf, dst_transform): sample_nc, geobox, written_data = make_sample_netcdf source = RasterFileDataSource(sample_nc, 1) dest = np.zeros((200, 1000)) dst_nodata = -999 dst_projection = geometry.CRS('EPSG:3577') dst_resampling = Resampling.nearest # Read exactly the hunk of data that we wrote read_from_source(source, dest, dst_transform, dst_nodata, dst_projection, dst_resampling) assert np.all(dest == -999)
def xtest_failed_data_read(self, make_sample_geotiff, dst_nodata): sample_geotiff_path, geobox, written_data = make_sample_geotiff(dst_nodata) src_transform = Affine(25.0, 0.0, 1200000.0, 0.0, -25.0, -4200000.0) source = RasterFileDataSource(sample_geotiff_path, 1, transform=src_transform) dest = np.zeros((20, 100)) dst_nodata = -999 dst_projection = geometry.CRS('EPSG:3577') dst_resampling = Resampling.nearest # Read exactly the hunk of data that we wrote dst_transform = Affine(25.0, 0.0, 127327.0, 0.0, -25.0, -417232.0) read_from_source(source, dest, dst_transform, dst_nodata, dst_projection, dst_resampling) assert np.all(written_data == dest)
def test_read_with_rasterfiledatasource(self, make_sample_geotiff): sample_geotiff_path, geobox, written_data = make_sample_geotiff source = RasterFileDataSource(str(sample_geotiff_path), 1) dest = np.zeros_like(written_data) dst_transform = geobox.transform dst_nodata = -999 dst_projection = geometry.CRS('EPSG:3577') dst_resampling = Resampling.nearest # Read exactly the hunk of data that we wrote read_from_source(source, dest, dst_transform, dst_nodata, dst_projection, dst_resampling) assert np.all(written_data == dest) # Try reading from partially outside of our area xoff = 50 offset_transform = dst_transform * Affine.translation(xoff, 0) dest = np.zeros_like(written_data) read_from_source(source, dest, offset_transform, dst_nodata, dst_projection, dst_resampling) assert np.all(written_data[:, xoff:] == dest[:, :xoff]) # Try reading from complete outside of our area, should return nodata xoff = 300 offset_transform = dst_transform * Affine.translation(xoff, 0) dest = np.zeros_like(written_data) read_from_source(source, dest, offset_transform, dst_nodata, dst_projection, dst_resampling) assert np.all(dst_nodata == dest)