def test_get_resolution(name, kwargs): ds = generate_test_dataset(**kwargs) res = get_resolution(ds) bounds = get_bounds(ds) resx = abs(bounds.right - bounds.left) / (ncols(ds) - 1) resy = abs(bounds.bottom - bounds.top) / (nrows(ds) - 1) assert_almost_equal(res, (resx, resy))
def test_get_common_resolution(mode, fn): bounds = [(-10.0, 50.0, 0.0, 60.0), (-12.0, 40.0, -2.0, 52.0), (-13.0, 50.0, -3.0, 60.0), (-9.0, 51.0, 1.0, 61.0)] datasets = [generate_test_dataset(extent=ext) for ext in bounds] res = np.array([get_resolution(ds) for ds in datasets]) common_res = tuple(fn(res, axis=0)) assert_equal(get_common_resolution(datasets, mode=mode), common_res)
def test_get_resolution_from_metadata(): ds = generate_test_dataset() assert_equal( warp._get_resolution_from_metadata(ds), warp.get_resolution(ds) ) del ds.attrs['transform'] assert_equal( warp._get_resolution_from_metadata(ds), warp.get_resolution(ds) ) del ds.attrs['res'] assert_equal( warp._get_resolution_from_metadata(ds), None )
def test_equal_datasets(): ds0 = open_dataset(slc_files[0]) for f in slc_files[1:]: ds = open_dataset(f) assert_equal(ds0['x'].values, ds['x'].values, 'x coordinates are not equal') assert_equal(ds0['y'].values, ds['y'].values, 'y coordinates are not equal') assert_equal(get_transform(ds0), get_transform(ds), 'transforms are not equal') assert_equal_crs(get_crs(ds0), get_crs(ds), 'CRS are not equal') assert_equal(get_resolution(ds0), get_resolution(ds), 'resolutions are not equal') assert_equal(get_bounds(ds0), get_bounds(ds), 'bounds are not equal') assert_equal(get_extent(ds0), get_extent(ds), 'extents are not equal') ds.close() ds0.close()
def test_reproject(generator): src_crs = epsg4326 dst_crs = sinusoidal ds = generator(crs=src_crs) src_bounds = get_bounds(ds) dst_bounds_latlon = BoundingBox( left=src_bounds.left - 1, bottom=src_bounds.bottom - 1, right=src_bounds.right + 1, top=src_bounds.top + 1, ) dst_bounds = BoundingBox(*rasterio.warp.transform_bounds( src_crs, dst_crs, **dst_bounds_latlon._asdict())) dst_width, dst_height = 35, 21 resx = (dst_bounds.right - dst_bounds.left) / (dst_width - 1) resy = (dst_bounds.bottom - dst_bounds.top) / (dst_height - 1) res = (abs(resx), abs(resy)) xoff = dst_bounds.left yoff = dst_bounds.top dst_transform = Affine(resx, 0, xoff, 0, resy, yoff) projected = [ _reproject(ds, dst_crs=dst_crs, dst_transform=dst_transform, width=dst_width, height=dst_height), _reproject(ds, dst_crs=dst_crs, dst_transform=dst_transform, extent=dst_bounds), _reproject(ds, dst_crs=dst_crs, extent=dst_bounds, res=res), _reproject(ds, dst_crs=dst_crs, extent=dst_bounds, width=dst_width, height=dst_height) ] for proj in projected[1:]: xr_assert_equal(proj, projected[0]) assert_almost_equal(get_resolution(proj), res) assert_almost_equal(get_bounds(proj), dst_bounds) assert_almost_equal(get_transform(proj), dst_transform) assert_equal_crs(get_crs(proj), dst_crs)
def test_reprojection_with_target(generator): src_crs = epsg4326 dst_crs = sinusoidal ds = generator(crs=src_crs) src_bounds = warp.get_bounds(ds) dst_bounds_latlon = BoundingBox( left=src_bounds.left - 1, bottom=src_bounds.bottom - 1, right=src_bounds.right + 1, top=src_bounds.top + 1, ) dst_bounds = BoundingBox(*rasterio.warp.transform_bounds( src_crs, dst_crs, **dst_bounds_latlon._asdict() )) dst_width, dst_height = 35, 21 resx = (dst_bounds.right - dst_bounds.left) / (dst_width - 1) resy = (dst_bounds.bottom - dst_bounds.top) / (dst_height - 1) res = (abs(resx), abs(resy)) xoff = dst_bounds.left yoff = dst_bounds.top dst_transform = Affine(resx, 0, xoff, 0, resy, yoff) target = generator( dims={'x': dst_width, 'y': dst_height, 'time': 1}, extent=dst_bounds, crs=dst_crs ) projected = [ warp.Reprojection(crs=dst_crs, transform=dst_transform, width=dst_width, height=dst_height).apply(ds), warp.Reprojection(crs=dst_crs, extent=dst_bounds, res=res).apply(ds), warp.Reprojection(crs=dst_crs, extent=dst_bounds, width=dst_width, height=dst_height).apply(ds), warp.Reprojection(target=target).apply(ds), ] for i, proj in enumerate(projected[1:]): print(i) xr_assert_equal(proj, projected[0]) assert_almost_equal(warp.get_resolution(proj), res) assert_almost_equal(warp.get_bounds(proj), dst_bounds) assert_almost_equal(warp.get_transform(proj), dst_transform) assert_equal_crs(warp.get_crs(proj), dst_crs)
def test_resolution_equal_transform_from_real_data(f): ds = open_dataset(f) res = get_resolution(ds) tf = get_transform(ds) ds.close() assert_almost_equal(res, (tf.a, abs(tf.e)))
def test_resample_to_resolution_float(name, kwargs): res = 0.05 ds = generate_test_dataset(**kwargs) resampled = Resample(res=res).apply(ds) assert_almost_equal((res, res), get_resolution(resampled))
def test_resample_to_resolution_tuple(name, kwargs): res = (0.05, 0.01) ds = generate_test_dataset(**kwargs) resampled = Resample(res=res).apply(ds) assert_almost_equal(res, get_resolution(resampled))
def test_accessor_nd_resolution(generator): ds = generator() res = warp.get_resolution(ds) assert_equal(res, ds.nd.resolution)