Exemplo n.º 1
0
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()
Exemplo n.º 2
0
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)
Exemplo n.º 3
0
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)
Exemplo n.º 4
0
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)
Exemplo n.º 5
0
def test_reprojection_with_src_crs():
    src_crs = epsg4326
    dst_crs = sinusoidal
    # Set up test dataset with and without CRS information
    ds = generate_test_dataset(crs=src_crs)
    assert_equal_crs(src_crs, ds.nd.crs)
    ds_nocrs = ds.copy()
    del ds_nocrs.attrs['crs']
    assert ds_nocrs.nd.crs is None

    with assert_raises_regex(
            CRSError,
            "Could not infer projection from input data. "
            "Please provide the parameter `src_crs`."):
        warp.Reprojection(dst_crs=dst_crs).apply(ds_nocrs)

    xr_assert_equal(
        warp.Reprojection(dst_crs=dst_crs).apply(ds),
        warp.Reprojection(src_crs=src_crs, dst_crs=dst_crs).apply(ds_nocrs)
    )
Exemplo n.º 6
0
def test_get_crs_from_variable(crs):
    snap_ds = create_snap_ds(crs=crs)
    assert_equal_crs(crs, get_crs(snap_ds))
Exemplo n.º 7
0
def test_get_crs(name, kwargs):
    ds = generate_test_dataset(**kwargs)
    assert_equal_crs(get_crs(ds), kwargs['crs'])
Exemplo n.º 8
0
def test_parse_crs(crs):
    assert_equal_crs(crs, _parse_crs(crs))
    assert_equal_crs(crs, _parse_crs(crs.to_string()))
    assert_equal_crs(crs, _parse_crs(crs.to_dict()))
    assert_equal_crs(crs, _parse_crs(crs.wkt))
    assert_equal_crs(crs, _parse_crs(crs.to_epsg()))
Exemplo n.º 9
0
def test_reprojection(name, kwargs):
    ds = generate_test_dataset(**kwargs)
    crs = _parse_crs('+init=epsg:4326')
    proj = Reprojection(crs=crs)
    reprojected = proj.apply(ds)
    assert_equal_crs(crs, get_crs(reprojected))
Exemplo n.º 10
0
def test_get_crs_from_variable(crs):
    snap_ds = create_snap_ds(crs=crs)
    parsed_crs = warp.get_crs(snap_ds)
    assert_equal_crs(crs, parsed_crs)
Exemplo n.º 11
0
def test_accessor_nd_crs(generator):
    crs = CRS.from_epsg(4326)
    ds = generator(crs=crs)
    assert_equal_crs(crs, ds.nd.crs)