Esempio n. 1
0
def _to_pgnode_data(value: Any) -> Union[PGNode, dict, Any]:
    """Convert given value to valid process graph material"""
    if isinstance(value, ProcessBuilderBase):
        return value.pgnode
    elif isinstance(value, list):
        return [_to_pgnode_data(item) for item in value]
    elif isinstance(value, Callable):
        pg = convert_callable_to_pgnode(value)
        return PGNode.to_process_graph_argument(pg)
    else:
        # Fallback: assume value is valid process graph material already.
        return value
Esempio n. 2
0
    def apply_dimension(
            self, code: str, runtime=None, version="latest", dimension='t', target_dimension=None
    ) -> 'DataCube':
        """
        Applies a user defined process to all pixel values along a dimension of a raster data cube. For example,
        if the temporal dimension is specified the process will work on a time series of pixel values.

        The process reduce_dimension also applies a process to pixel values along a dimension, but drops
        the dimension afterwards. The process apply applies a process to each pixel value in the data cube.

        The target dimension is the source dimension if not specified otherwise in the target_dimension parameter.
        The pixel values in the target dimension get replaced by the computed pixel values. The name, type and
        reference system are preserved.

        The dimension labels are preserved when the target dimension is the source dimension and the number of
        pixel values in the source dimension is equal to the number of values computed by the process. Otherwise,
        the dimension labels will be incrementing integers starting from zero, which can be changed using
        rename_labels afterwards. The number of labels will equal to the number of values computed by the process.

        :param code: UDF code or process identifier
        :param runtime: UDF runtime to use
        :param version: Version of the UDF runtime to use
        :param dimension: The name of the source dimension to apply the process on. Fails with a DimensionNotAvailable error if the specified dimension does not exist.
        :param target_dimension: The name of the target dimension or null (the default) to use the source dimension
        specified in the parameter dimension. By specifying a target dimension, the source dimension is removed.
        The target dimension with the specified name and the type other (see add_dimension) is created, if it doesn't exist yet.

        :return: A datacube with the UDF applied to the given dimension.
        :raises: DimensionNotAvailable
        """
        if runtime:
            process = self._create_run_udf(code, runtime, version)
        else:
            process = PGNode(
                process_id=code,
                arguments={"data": {"from_parameter": "data"}},
            )
        arguments = {
            "data": self._pg,
            "process": PGNode.to_process_graph_argument(process),
            "dimension": self.metadata.assert_valid_dimension(dimension),
            # TODO #125 arguments: context
        }
        if target_dimension is not None:
            arguments["target_dimension"] = target_dimension
        return self.process_with_node(PGNode(
            process_id="apply_dimension",
            arguments=arguments
        ))
Esempio n. 3
0
    def load_collection(
            cls, collection_id: str, connection: 'Connection' = None,
            spatial_extent: Union[Dict[str, float], None] = None,
            temporal_extent: Union[List[Union[str, datetime.datetime, datetime.date]], None] = None,
            bands: Union[List[str], None] = None,
            fetch_metadata=True,
            properties: Dict[str, PGNode] = None
    ):
        """
        Create a new Raster Data cube.

        :param collection_id: A collection id, should exist in the backend.
        :param connection: The connection to use to connect with the backend.
        :param spatial_extent: limit data to specified bounding box or polygons
        :param temporal_extent: limit data to specified temporal interval
        :param bands: only add the specified bands
        :param properties: limit data by metadata property predicates
        :return:
        """
        normalized_temporal_extent = list(
            get_temporal_extent(extent=temporal_extent)) if temporal_extent is not None else None
        arguments = {
            'id': collection_id,
            'spatial_extent': spatial_extent,
            'temporal_extent': normalized_temporal_extent,
        }
        metadata = connection.collection_metadata(collection_id) if fetch_metadata else None
        if bands:
            if isinstance(bands, str):
                bands = [bands]
            if metadata:
                bands = [metadata.band_dimension.band_name(b) for b in bands]
            arguments['bands'] = bands
        if properties:
            arguments['properties'] = {
                prop: PGNode.to_process_graph_argument(pred)
                for prop, pred in properties.items()
            }
        pg = PGNode(
            process_id='load_collection',
            arguments=arguments
        )
        if bands:
            metadata = metadata.filter_bands(bands)
        return cls(graph=pg, connection=connection, metadata=metadata)