def test_alignment(tmpdir, extent, from_files): datapath = tmpdir.mkdir('data') path = tmpdir.mkdir('aligned') 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] if extent is None: common_bounds = warp.get_common_bounds(datasets) else: common_bounds = extent files = [str(datapath.join('data_%d.nc' % i)) for i in range(len(datasets))] if from_files: for ds, f in zip(datasets, files): to_netcdf(ds, f) datasets = files warp.Alignment(extent=extent).apply(datasets, path=str(path)) aligned = [open_dataset(str(f)) for f in path.listdir()] for ds in aligned: assert_equal(warp.get_bounds(ds), common_bounds) assert_equal( warp.get_transform(ds), warp.get_transform(aligned[0]) ) xr_assert_equal(ds['x'], aligned[0]['x']) xr_assert_equal(ds['y'], aligned[0]['y'])
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_get_transform(name, kwargs): ds = generate_test_dataset(**kwargs) bounds = get_bounds(ds) resx = (bounds.right - bounds.left) / (ds.dims['x'] - 1) resy = (bounds.bottom - bounds.top) / (ds.dims['y'] - 1) xoff = bounds.left yoff = bounds.top transform = Affine(resx, 0, xoff, 0, resy, yoff) assert_equal(get_transform(ds), transform)
def test_rasterize(tmpdir): path = str(tmpdir.join('polygons.shp')) ds = generate_test_dataset(dims=dict(x=100, y=100, time=5)) df = generate_test_geodataframe() schema = gpd.io.file.infer_schema(df) schema['properties']['date'] = 'date' df.to_file(path, schema=schema) # Rasterize raster = vector.rasterize(path, ds) # Check that the raster contains all fields as variables assert set(raster.data_vars).union({'geometry'}) == set(df.columns) # Check dtypes assert np.issubdtype(raster.float.dtype, np.floating) assert np.issubdtype(raster.integer.dtype, np.signedinteger) assert np.issubdtype(raster.category.dtype, np.signedinteger) # Check that extent, projection etc. are identical to the reference raster assert_equal(warp.get_bounds(raster), warp.get_bounds(ds)) assert_equal_crs(warp.get_crs(raster), warp.get_crs(ds)) assert_equal(warp.get_transform(raster), warp.get_transform(ds)) # Check raster content shape = (ds.dims['y'], ds.dims['x']) transform = warp.get_transform(ds) for i, row in df.iterrows(): poly = row['geometry'] mask = rasterio.features.rasterize([poly], out_shape=shape, transform=transform) # Erode mask to avoid edge effects mask = ndimage.morphology.binary_erosion(mask) == 1 for v in raster.data_vars: if 'legend' in raster[v].attrs: expected = sorted(raster[v].attrs['legend'], key=lambda x: x[1] == str(row[v]))[-1][0] else: expected = row[v] values = raster[v].isel(time=0).values values[mask] assert_allclose(values[mask], expected)
def test_reprojection_failure(): ds = generate_test_dataset() transform = get_transform(ds) extent = get_extent(ds) with assert_raises_regex( ValueError, ".* must also specify the `width` and `height`.*"): proj = Reprojection(crs=epsg4326, transform=transform) with assert_raises_regex(ValueError, "Need to provide either `width` and `height` .*"): proj = Reprojection(crs=epsg4326, extent=extent)
def create_snap_ds(*args, **kwargs): ds = generate_test_dataset(*args, **kwargs) crs = get_crs(ds) t = get_transform(ds) i2m_string = ','.join(map(str, [t.a, t.d, t.b, t.e, t.c, t.f])) del ds.attrs['crs'] del ds.attrs['transform'] ds['crs'] = ((), 1) attrs = {'crs': crs.wkt, 'i2m': i2m_string} ds['crs'].attrs = attrs return ds
def test_get_transform(name, kwargs): ds = generate_test_dataset(**kwargs) x = ds.coords['x'].values y = ds.coords['y'].values resx = (x[-1] - x[0]) / (ds.dims['x'] - 1) resy = (y[-1] - y[0]) / (ds.dims['y'] - 1) xoff = x[0] yoff = y[0] transform = Affine(resx, 0, xoff, 0, resy, yoff) assert_equal( warp.get_transform(ds), transform )
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_get_transform_from_variable(crs): ds = generate_test_dataset(crs=crs) snap_ds = create_snap_ds(crs=crs) assert_equal(get_transform(ds), get_transform(snap_ds))
def test_get_transform_from_metadata(): ds = generate_test_dataset() assert_equal( warp._get_transform_from_metadata(ds), warp.get_transform(ds) )
def test_accessor_nd_transform(generator): ds = generator() transf = warp.get_transform(ds) assert_equal(transf, ds.nd.transform)