コード例 #1
0
def test_geobox_xr_coords():
    A = mkA(0, scale=(10, -10),
            translation=(-48800, -2983006))

    w, h = 512, 256
    gbox = GeoBox(w, h, A, epsg3577)

    cc = gbox.xr_coords()
    assert list(cc) == ['y', 'x']
    assert cc['y'].shape == (gbox.shape[0],)
    assert cc['x'].shape == (gbox.shape[1],)
    assert 'crs' in cc['y'].attrs
    assert 'crs' in cc['x'].attrs

    cc = gbox.xr_coords(with_crs=True)
    assert list(cc) == ['y', 'x', 'spatial_ref']
    assert cc['spatial_ref'].shape == ()
    assert cc['spatial_ref'].attrs['spatial_ref'] == gbox.crs.wkt
    assert isinstance(cc['spatial_ref'].attrs['grid_mapping_name'], str)

    cc = gbox.xr_coords(with_crs='Albers')
    assert list(cc) == ['y', 'x', 'Albers']

    # geographic CRS
    A = mkA(0, scale=(0.1, -0.1), translation=(10, 30))
    gbox = GeoBox(w, h, A, 'epsg:4326')
    cc = gbox.xr_coords(with_crs=True)
    assert list(cc) == ['latitude', 'longitude', 'spatial_ref']
    assert cc['spatial_ref'].shape == ()
    assert cc['spatial_ref'].attrs['spatial_ref'] == gbox.crs.wkt
    assert isinstance(cc['spatial_ref'].attrs['grid_mapping_name'], str)

    # missing CRS for GeoBox
    gbox = GeoBox(w, h, A, None)
    cc = gbox.xr_coords(with_crs=True)
    assert list(cc) == ['y', 'x']

    # check CRS without name
    crs = MagicMock()
    crs.projected = True
    crs.wkt = epsg3577.wkt
    crs.epsg = epsg3577.epsg
    crs._crs = MagicMock()
    crs._crs.to_cf.return_value = {}
    assert _mk_crs_coord(crs).attrs['grid_mapping_name'] == '??'
コード例 #2
0
ファイル: _warp.py プロジェクト: opendatacube/odc-tools
def xr_reproject_array(
    src: xr.DataArray,
    geobox: GeoBox,
    resampling: str = "nearest",
    chunks: Optional[Tuple[int, int]] = None,
    dst_nodata: Optional[NodataType] = None,
) -> xr.DataArray:
    """
    Reproject DataArray to a given GeoBox

    :param src       : Input src[(time,) y,x (, band)]
    :param geobox    : GeoBox of the destination
    :param resampling: Resampling strategy as a string: nearest, bilinear, average, mode ...
    :param chunks    : In Y,X dimensions only, default is to use input chunk size
    :param dst_nodata: nodata marker for dst image (default is to use src.nodata)
    """
    src_nodata = getattr(src, "nodata", None)
    if dst_nodata is None:
        dst_nodata = src_nodata

    src_geobox = src.geobox
    assert src_geobox is not None

    yx_dims = spatial_dims(src)
    axis = tuple(src.dims).index(yx_dims[0])

    src_dims = tuple(src.dims)
    dst_dims = src_dims[:axis] + geobox.dims + src_dims[axis + 2:]

    coords = geobox.xr_coords(with_crs=True)

    # copy non-spatial coords from src to dst
    src_non_spatial_dims = src_dims[:axis] + src_dims[axis + 2:]
    for dim in src_non_spatial_dims:
        if dim not in coords:
            coords[dim] = src.coords[dim]

    attrs = {}
    if dst_nodata is not None:
        attrs["nodata"] = dst_nodata

    if is_dask_collection(src):
        data = dask_reproject(
            src.data,
            src_geobox,
            geobox,
            resampling=resampling,
            chunks=chunks,
            src_nodata=src_nodata,
            dst_nodata=dst_nodata,
            axis=axis,
        )
    else:
        data = _reproject_block_impl(
            src.data,
            src_geobox,
            geobox,
            resampling=resampling,
            src_nodata=src_nodata,
            dst_nodata=dst_nodata,
            axis=axis,
        )

    return xr.DataArray(data,
                        name=src.name,
                        coords=coords,
                        dims=dst_dims,
                        attrs=attrs)
コード例 #3
0
ファイル: _load.py プロジェクト: zs856/datacube-core
def _mk_empty_ds(coords: DataArrayCoordinates, geobox: GeoBox) -> XrDataset:
    cc = OrderedDict(coords.items())
    cc.update(geobox.xr_coords())
    return XrDataset(coords=cast(Mapping[Hashable, Any], cc),
                     attrs={'crs': geobox.crs})