示例#1
0
def rct_pytorch_ml(udf_data: UdfData):
    """Apply a pre-trained pytorch machine learn model on the first tile

    The model must be a pytorch model that has expects the input data in the constructor
    The prediction method must accept a torch.autograd.Variable as input.

    Args:
        udf_data (UdfData): The UDF data object that contains raster and vector tiles

    Returns:
        This function will not return anything, the UdfData object "udf_data" must be used to store the resulting
        data.

    """
    tile = udf_data.raster_collection_tiles[0]

    # This is the input data of the model.
    input = torch.autograd.Variable(torch.Tensor(tile.data))

    # Get the first model
    mlm = udf_data.get_ml_model_list()[0]
    m = mlm.get_model()
    # Predict the data
    pred = m(input)
    # Create the new raster collection tile
    rct = RasterCollectionTile(id=mlm.name,
                               extent=tile.extent,
                               data=numpy.array(pred.tolist()),
                               start_times=tile.start_times,
                               end_times=tile.end_times)
    # Insert the new tiles as list of raster collection tiles in the input object. The new tiles will
    # replace the original input tiles.
    udf_data.set_raster_collection_tiles([
        rct,
    ])
示例#2
0
def rct_time_sum(udf_data: UdfData):
    """Reduce the time dimension for each tile and compute  sum for each pixel
    over time.

    Each raster tile in the udf data object will be reduced by time. Sum is
    computed for each pixel over time.

    Args:
        udf_data (UdfData): The UDF data object that contains raster and vector tiles

    Returns:
        This function will not return anything, the UdfData object "udf_data" must be used to store the resulting
        data.

    """
    # The list of tiles that were created
    tile_results = []

    # Iterate over each tile
    for tile in udf_data.raster_collection_tiles:
        tile_sum = numpy.sum(tile.data, axis=0)
        # We need to create a new 3D array with the correct shape for the computed aggregate
        rows, cols = tile_sum.shape
        array3d = numpy.ndarray([1, rows, cols])
        array3d[0] = tile_sum
        # Extract the start and end time to set the temporal extent for each tile
        if tile.start_times is not None and tile.end_times is not None:
            starts = pandas.DatetimeIndex([tile.start_times[0]])
            ends = pandas.DatetimeIndex([tile.end_times[-1]])
        else:
            starts = None
            ends = None
        # Create the new raster collection tile
        rct = RasterCollectionTile(id=tile.id + "_sum", extent=tile.extent, data=array3d,
                                   start_times=starts, end_times=ends)
        tile_results.append(rct)
    # Insert the new tiles as list of raster collection tiles in the input object. The new tiles will
    # replace the original input tiles.
    udf_data.set_raster_collection_tiles(tile_results)
def rct_ndvi(udf_data: UdfData):
    """Compute the NDVI based on RED and NIR tiles

    Tiles with ids "red" and "nir" are required. The NDVI computation will be applied
    to all time stamped 2D raster tiles that have equal time stamps.

    Args:
        udf_data (UdfData): The UDF data object that contains raster and vector tiles

    Returns:
        This function will not return anything, the UdfData object "udf_data" must be used to store the resulting
        data.

    """
    red = None
    nir = None

    # Iterate over each tile
    for tile in udf_data.raster_collection_tiles:
        if "red" in tile.id.lower():
            red = tile
        if "nir" in tile.id.lower():
            nir = tile
    if red is None:
        raise Exception("Red raster collection tile is missing in input")
    if nir is None:
        raise Exception("Nir raster collection tile is missing in input")
    if red.start_times is None or red.start_times.tolist() == nir.start_times.tolist():
        # Compute the NDVI
        ndvi = (nir.data - red.data) / (nir.data + red.data)
        # Create the new raster collection tile
        rct = RasterCollectionTile(id="ndvi", extent=red.extent, data=ndvi,
                                   start_times=red.start_times, end_times=red.end_times)
        # Insert the new tiles as list of raster collection tiles in the input object. The new tiles will
        # replace the original input tiles.
        udf_data.set_raster_collection_tiles([rct,])
    else:
        raise Exception("Time stamps are not equal")