コード例 #1
0
def create_process_chain_entry(input_object: DataObject, options: dict):
    """Create a Actinia command of the process chain that computes the regional statistics based on a
    strds and a polygon.

    :param input_object: The name of the raster layer
    :return: A Actinia process chain description
    """

    rn = randint(0, 1000000)
    pc = []

    output_format = "GTiff"
    if input_object.is_vector():
        output_format = "GML"

    # the actinia exporter currently does not support GDAL creation options
    # parameter options is ignored
    options_list = list()
    for key in options:
        optstr = ("%s=%s" % (key, options[key]))
        options_list.append(optstr)

    exporter = {
        "id":
        "save_result_%i" % rn,
        "module":
        "exporter",
        "outputs": [{
            "export": {
                "type": input_object.datatype.value,
                "format": output_format
            },
            "param": "map",
            "value": input_object.grass_name()
        }]
    }

    pc.append(exporter)

    return pc
コード例 #2
0
def create_process_chain_entry(input_object: DataObject,
                               spatial_extent,
                               temporal_extent,
                               bands,
                               output_object: DataObject):
    """Create a Actinia process description that r.info, v.info, or t.info.

    :param input_object: The input object name
    :param spatial_extent: spatial filter
    :param temporal_extent: temporal filter
    :param bands: bands to extract
    :return: A Actinia process chain description
    """

    rn = randint(0, 1000000)

    pc = []

    if input_object.is_raster():
        importer = {"id": "r_info_%i" % rn, "module": "r.info", "inputs": [
            {"param": "map", "value": input_object.grass_name()}, ],
            "flags": "g"}

    elif input_object.is_vector():
        importer = {"id": "v_info_%i" % rn, "module": "v.info", "inputs": [
            {"param": "map", "value": input_object.grass_name()}, ],
            "flags": "g"}

    elif input_object.is_strds():
        importer = {"id": "t_info_%i" % rn, "module": "t.info", "inputs": [
            {"param": "input", "value": input_object.grass_name()}, ],
            "flags": "g"}

    elif input_object.is_stac():
        instance_id = input_object.mapset
        collection_id = f"stac.{instance_id}.rastercube.{input_object.name}"
        strds_name = (output_object.grass_name()).replace('@', '_')
        # Define the import process of the STAC collection
        stac_input_importer = {
                    "import_descr": {
                        "source": collection_id,
                        "type": "stac"
                    },
                    "param": "map",
                    "value": strds_name
                }
        param_import = _get_stac_importer(stac_input_importer, spatial_extent,
                                          temporal_extent, bands, rn)
        stac_importchain = {
                "id": "importer_1",
                "module": "importer",
                "inputs": [param_import]
            }

        pc.append(stac_importchain)

        importer = {"id": "t_info_%i" % rn, "module": "t.info", "inputs": [
            {"param": "input", "value": strds_name}, ],
            "flags": "g"}
    else:
        raise Exception("Unsupported datatype")

    pc.append(importer)

    # TODO: spatial extent can also be a GeoJSON object
    if spatial_extent is not None:
        north = spatial_extent["north"]
        south = spatial_extent["south"]
        west = spatial_extent["west"]
        east = spatial_extent["east"]
        if "crs" in spatial_extent:
            crs = str(spatial_extent["crs"])
        else:
            crs = "4326"

        if crs.isnumeric():
            crs = "EPSG:" + crs

        if input_object.is_raster():
            region_bbox = {
                "id": "g_region_bbox_%i" %
                rn, "module": "g.region.bbox", "inputs": [
                    {
                        "param": "n", "value": str(north)}, {
                        "param": "s", "value": str(south)}, {
                        "param": "e", "value": str(east)}, {
                        "param": "w", "value": str(west)}, {
                        "param": "crs", "value": str(crs)}, {
                        "param": "raster", "value": input_object.grass_name()},
                    ]}
        elif input_object.is_strds() or input_object.is_stac():
            region_bbox = {
                "id": "g_region_bbox_%i" %
                rn, "module": "g.region.bbox", "inputs": [
                    {
                        "param": "n", "value": str(north)}, {
                        "param": "s", "value": str(south)}, {
                        "param": "e", "value": str(east)}, {
                        "param": "w", "value": str(west)}, {
                        "param": "crs", "value": str(crs)}, {
                        "param": "strds", "value": input_object.grass_name()},
                        ]
                    }
        else:
            region_bbox = {"id": "g_region_bbox_%i" % rn,
                           "module": "g.region.bbox",
                           "inputs": [{"param": "n", "value": str(north)},
                                      {"param": "s", "value": str(south)},
                                      {"param": "e", "value": str(east)},
                                      {"param": "w", "value": str(west)},
                                      {"param": "crs", "value": str(crs)}, ]}

        pc.append(region_bbox)

    if input_object.is_strds() and \
       (temporal_extent is not None or bands is not None):
        wherestring = ""
        if temporal_extent:
            start_time = temporal_extent[0].replace('T', ' ')
            if len(temporal_extent) > 1:
                end_time = temporal_extent[1].replace('T', ' ')
                wherestring = "start_time >= '%(start)s' AND start_time < '%(end)s'" % {
                                                "start": start_time, "end": end_time}
            # end_time can be null, use only start_time for filtering
            else:
                wherestring = "start_time >= '%(start)s'" % {"start": start_time}
            if bands:
                wherestring = wherestring + " AND "
        if bands:
            wherestring = wherestring + \
                "semantic_label in ('%(band_names)s')" % {"band_names": ("', '").join(bands)}

        pc_strdsfilter = {
            "id": "t_rast_extract_%i" % rn,
            "module": "t.rast.extract",
            "inputs": [{"param": "input",
                        "value": input_object.grass_name()},
                       {"param": "where",
                        "value": wherestring},
                       {"param": "output",
                        "value": output_object.grass_name()},
                       {"param": "expression",
                        "value": "1.0 * %s" % input_object.name},
                       {"param": "basename",
                        "value": output_object.name},
                       {"param": "suffix",
                        "value": "num"}]}

        pc.append(pc_strdsfilter)

    return pc