def test_xr_geobox():
    xy = (10, 111)
    rxy = (10, -100)
    resolution = rxy[::-1]

    ds = mk_sample_xr_dataset(crs=epsg3857, xy=xy, resolution=resolution)

    assert ds.geobox.crs == epsg3857
    assert ds.band.geobox.crs == epsg3857
    assert ds.band.affine * (0, 0) == xy
    assert ds.band.affine * (1, 1) == tuple(a + b for a, b in zip(xy, rxy))

    assert ds.band[:, :2, :2].affine * (0, 0) == xy
    assert ds.band[:, :2, :2].affine * (1, 1) == tuple(
        a + b for a, b in zip(xy, rxy))
    assert ds.band.isel(time=0, x=0).affine is None

    xx = ds.band + 1000
    assert xx.geobox is not None
    assert xx.geobox == ds.band.geobox

    assert mk_sample_xr_dataset(crs=epsg4326).geobox.crs == epsg4326
    assert mk_sample_xr_dataset(crs=epsg4326).band.geobox.crs == epsg4326

    assert mk_sample_xr_dataset(crs=None).geobox is None
    assert mk_sample_xr_dataset(crs=None).affine is not None
Esempio n. 2
0
def test_xr_geobox_unhappy():
    xx = mk_sample_xr_dataset(crs=None)

    # test duplicates behaviour in get_crs_from_{coord,attrs} are caught
    xx.band.attrs.update(grid_mapping='x')  # force exception in coord
    xx.x.attrs.update(crs='EPSG:4326')  # force exception in attr
    xx.y.attrs.update(crs='EPSG:3857')
    with pytest.warns(UserWarning):
        assert xx.band.geobox is not None

    # test crs not being a string
    xx = mk_sample_xr_dataset(crs=None)
    xx.attrs['crs'] = ['this will not parse']
    with pytest.warns(UserWarning):
        assert xx.geobox is None

    xx.attrs['crs'] = 'this will fail CRS() constructor'
    with pytest.warns(UserWarning):
        assert xx.geobox is None

    xx = mk_sample_xr_dataset(crs=epsg3857)
    xx.attrs.pop('crs', None)
    xx.band.attrs.pop('crs', None)
    xx.spatial_ref.attrs['spatial_ref'] = 'this will fail CRS() constructor'
    with pytest.warns(UserWarning):
        assert xx.geobox is None
Esempio n. 3
0
def test_crs_from_attrs():
    xx_none = mk_sample_xr_dataset(crs=None)
    xx_3857 = mk_sample_xr_dataset(crs=epsg3857)
    xx_4326 = mk_sample_xr_dataset(crs=epsg4326)

    assert _get_crs_from_attrs(xx_none, ('y', 'x')) is None
    assert _get_crs_from_attrs(xx_none.band, ('y', 'x')) is None
    assert _get_crs_from_attrs(xx_3857.band, ('y', 'x')) == epsg3857
    assert _get_crs_from_attrs(xx_3857, ('y', 'x')) == epsg3857
    assert _get_crs_from_attrs(xx_4326.band,
                               ('latitude', 'longitude')) == epsg4326
    assert _get_crs_from_attrs(xx_4326, ('latitude', 'longitude')) == epsg4326

    assert _get_crs_from_attrs(xr.Dataset(), None) is None

    # check inconsistent CRSs
    xx = xx_3857.copy()
    xx.x.attrs['crs'] = xx_4326.attrs['crs']
    with pytest.warns(UserWarning):
        _get_crs_from_attrs(xx, ('y', 'x'))

    xx = xx_none.copy()
    xx.attrs['crs'] = epsg3857
    assert xx.geobox is not None
    assert xx.geobox.crs is epsg3857
def test_xr_geobox_unhappy():
    xx = mk_sample_xr_dataset(crs=None)

    # test that exceptions from get_crs_from_{coord,attrs} are caught
    xx.band.attrs.update(grid_mapping='x')  # force exception in coord
    xx.x.attrs.update(crs='EPSG:4326')  # force exception in attr
    xx.y.attrs.update(crs='EPSG:3857')
    assert xx.band.geobox is None

    # test _norm_crs exception is caught
    xx = mk_sample_xr_dataset(crs=None)
    xx.attrs['crs'] = ['this will not parse']
    assert xx.geobox is None
Esempio n. 5
0
def test_write_dataset_with_time_dimension_to_netcdf(tmpnetcdf_filename):
    xx = mk_sample_xr_dataset(name='B10', time='2020-01-01')
    assert 'time' in xx.coords
    assert 'units' not in xx.time.attrs

    write_dataset_to_netcdf(xx,
                            tmpnetcdf_filename,
                            global_attributes={'foo': 'bar'},
                            variable_params={'B10': {
                                'attrs': {
                                    'abc': 'xyz'
                                }
                            }})

    with netCDF4.Dataset(tmpnetcdf_filename) as nco:
        nco.set_auto_mask(False)
        assert 'B10' in nco.variables
        var = nco.variables['B10']
        assert (var[:] == xx['B10'].values).all()

        assert 'foo' in nco.ncattrs()
        assert nco.getncattr('foo') == 'bar'

        assert 'abc' in var.ncattrs()
        assert var.getncattr('abc') == 'xyz'

    with pytest.raises(RuntimeError):
        write_dataset_to_netcdf(xx, tmpnetcdf_filename)

    # Check grid_mapping is a coordinate
    yy = xr.open_dataset(tmpnetcdf_filename)
    assert crs_var in yy.coords
    assert crs_var not in yy.data_vars

    assert 'time' in yy.coords
def test_crs_from_attrs():
    xx_none = mk_sample_xr_dataset(crs=None)
    xx_3857 = mk_sample_xr_dataset(crs=epsg3857)
    xx_4326 = mk_sample_xr_dataset(crs=epsg4326)

    assert _get_crs_from_attrs(xx_none) is None
    assert _get_crs_from_attrs(xx_none.band) is None
    assert _get_crs_from_attrs(xx_3857.band) == epsg3857
    assert _get_crs_from_attrs(xx_3857) == epsg3857
    assert _get_crs_from_attrs(xx_4326.band) == epsg4326
    assert _get_crs_from_attrs(xx_4326) == epsg4326

    assert _get_crs_from_attrs(xr.Dataset()) is None

    # check inconsistent CRSs
    xx = xx_3857.copy()
    xx.x.attrs['crs'] = xx_4326.attrs['crs']
    with pytest.raises(ValueError):
        _get_crs_from_attrs(xx)
def test_crs_from_coord():
    xx_none = mk_sample_xr_dataset(crs=None)
    xx_3857 = mk_sample_xr_dataset(crs=epsg3857)
    xx_4326 = mk_sample_xr_dataset(crs=epsg4326)
    cc_4326 = xx_4326.geobox.xr_coords(with_crs='epsg_4326')['epsg_4326']
    cc_3857 = xx_3857.geobox.xr_coords(with_crs='epsg_3857')['epsg_3857']

    assert _get_crs_from_coord(xx_none.band) is None
    assert _get_crs_from_coord(xx_none) is None
    assert _get_crs_from_coord(xx_3857.band) == epsg3857
    assert _get_crs_from_coord(xx_3857) == epsg3857
    assert _get_crs_from_coord(xx_4326.band) == epsg4326
    assert _get_crs_from_coord(xx_4326) == epsg4326

    xx = xx_none.band.assign_attrs(grid_mapping='x')
    with pytest.raises(ValueError):
        _get_crs_from_coord(xx)
    xx = xx_none.band.assign_attrs(grid_mapping='no_such_coord')
    assert _get_crs_from_coord(xx) is None

    xx_2crs = xx_none.assign_coords(cc_4326=cc_4326, cc_3857=cc_3857)
    assert xx_2crs.geobox is None

    # two coords, no grid mapping, strict mode
    with pytest.raises(ValueError):
        _get_crs_from_coord(xx_2crs)
    with pytest.raises(ValueError):
        _get_crs_from_coord(xx_2crs.band)

    # any should just return "first" guess, we not sure which one
    crs = _get_crs_from_coord(xx_2crs, 'any')
    assert epsg4326 == crs or epsg3857 == crs

    # all should return a list of length 2
    crss = _get_crs_from_coord(xx_2crs, 'all')
    assert len(crss) == 2
    assert any(crs == epsg3857 for crs in crss)
    assert any(crs == epsg4326 for crs in crss)

    with pytest.raises(ValueError):
        _get_crs_from_coord(xx_2crs, 'no-such-mode')
Esempio n. 8
0
def test_mk_sample_xr():
    xx = mk_sample_xr_dataset()
    assert 'band' in xx.data_vars
    assert list(xx.coords) == ['time', 'y', 'x', 'spatial_ref']
    assert xx.band.dims == ('time', 'y', 'x')
    assert xx.geobox is not None

    assert mk_sample_xr_dataset(name='xx', shape=(3, 7)).xx.shape == (1, 3, 7)
    assert mk_sample_xr_dataset(name='xx', time=None,
                                shape=(3, 7)).xx.shape == (3, 7)
    assert mk_sample_xr_dataset(name='xx', time=None).xx.dims == ('y', 'x')

    assert mk_sample_xr_dataset(resolution=(1, 100)).geobox.resolution == (1,
                                                                           100)
    assert mk_sample_xr_dataset(
        resolution=(1, 100), xy=(3, 55)).geobox.transform * (0, 0) == (3, 55)
    assert mk_sample_xr_dataset(crs=None).geobox is None