예제 #1
0
    def from_vector(self, vector_data):
        """Get the geobox to use for the grid.

        Parameters
        ----------
        vector_data: str or :obj:`geopandas.GeoDataFrame`
            A file path to an OGR supported source or GeoDataFrame
            containing the vector data.

        Returns
        -------
        :obj:`datacube.utils.geometry.GeoBox`
            The geobox for the grid to be generated from the vector data.

        """
        vector_data = load_vector_data(vector_data)

        if self.like is not None:
            assert (self.output_crs is
                    None), "'like' and 'output_crs' are not supported together"
            assert (self.resolution is
                    None), "'like' and 'resolution' are not supported together"
            assert self.align is None, "'like' and 'align' are not supported together"
            try:
                geobox = self.like.geobox
            except AttributeError:
                geobox = geobox_from_rio(self.like)
            return geobox

        if self.resolution is None:
            raise RuntimeError(
                "Must specify 'resolution' if 'like' not specified.")

        if self.output_crs:
            crs = geometry.CRS(self.output_crs)
        else:
            crs = geometry.CRS(crs_to_wkt(CRS.from_user_input(
                vector_data.crs)))

        if self.geom is None:
            geopoly = geometry.Geometry(
                mapping(box(*vector_data.total_bounds)),
                crs=geometry.CRS(
                    crs_to_wkt(CRS.from_user_input(vector_data.crs))),
            )

        else:
            geom_json = json.loads(self.geom)
            geom_crs = geometry.CRS(
                "+init={}".format(geom_json["crs"]["properties"]["name"].lower(
                ) if "crs" in geom_json else "epsg:4326"))

            geopoly = geometry.Geometry(geom_json, crs=geom_crs)

        return geometry.GeoBox.from_geopolygon(geopoly, self.resolution, crs,
                                               self.align)
예제 #2
0
def geobox_from_rio(xds):
    """This function retrieves the geobox using rioxarray extension.

    Parameters
    ----------
    xds: :obj:`xarray.DataArray` or :obj:`xarray.Dataset`
        The xarray dataset to get the geobox from.

    Returns
    -------
    :obj:`datacube.utils.geometry.GeoBox`

    """
    width, height = xds.rio.shape
    try:
        transform = xds.rio.transform()
    except AttributeError:
        transform = xds[xds.rio.vars[0]].rio.transform()
    return geometry.GeoBox(
        width=width,
        height=height,
        affine=transform,
        crs=geometry.CRS(crs_to_wkt(xds.rio.crs)),
    )