PixelsTransform¶
- class geodesic.boson.PixelsTransform(*args, **kwargs)[source]¶
Bases:
geodesic.boson.middleware._Middleware
- static colormap(*, asset, band, colormap_name='magma', lookup_table=None, min=None, max=None, rescale=False, no_data_value=None, asset_name='colormap')[source]¶
apply a colormap to the pixels data from an existing asset in the dataset
- Parameters
asset (str) – asset to apply the colormap to
colormap_name (str) – name of the colormap to apply. Defaults to “magma”.
lookup_table (Optional[List[List[int]]]) – lookup table to apply. Defaults to None. This can be used to provide a custom colormap via a lookup table. In this case, this should be a list of lists where each sublist is a color, including the alpha channel. For instance, [[0, 0, 0, 255], [255, 255, 255, 255]] would create a colormap that goes from black to white in two steps, splitting values below 0,5 to black and above to white.
min (Optional[float]) – minimum value of the colormap. Valid only if rescale is True. Defaults to None.
max (Optional[float]) – maximum value of the colormap. Valid only if rescale is True. Defaults to None.
rescale (bool) – whether to rescale the colormap to the min and max values. If min/max are None, statistics of the dataset (if available) will be used, otherwise the values of the current response will be used. Defaults to False.
no_data_value (Optional[float]) – value to use for pixels with no data. These values will be transparent. Defaults to None.
asset_name (str) – name of the asset to create. Defaults to “colormap”.
Examples
>>> # Apply the magma colormap to the rasterized band of the asset "rasterized" >>> colormap_transform = PixelsTransform.colormap(asset="rasterized", band="rasterized", min=0, max=1000)
- static rescale_shift(asset, bands=None, scale=None, shift=None, clamp=None, asset_name='rescaled')[source]¶
rescale the pixel values of an asset in a dataset
Rescales the pixel values of an asset in a dataset by subtracting a shift from the pixel values and then dividing by a scale. This is useful for things like normalizing the pixel values of an image to be between 0 and 1 or 0 and 255, or recalibration pixels values under a linear transformation.
If scale/shift is not specified, will rescale by the local min/max of the pixel values across all bands. Do not use this with tiled services as each tile will be rescaled based on its min/max and the result will be a checkerboard pattern. Instead, use the scale/shift parameters to rescale the entire image tile set.
- Parameters
asset (str) – asset to rescale/shift
bands (Optional[List[Union[int, str]]]) – bands to rescale/shift. If not specified, will use all bands for the specified asset.
scale (Optional[List[float]]) – scale values for each band. This will divide the shifted pixel values by this value.
shift (Optional[List[float]]) – shift values for each band. This will subtract this value from the pixel values prior to scaling.
clamp (Optional[List[float]]) – If specified, must be 2 numbers to clamp the values between ranges. Values less than clamp[0] will be set to clamp[0] and values greater than clamp[1] will be set to clamp[1]. Defaults to None (no clamping)
asset_name (str) – name of the asset to create. Defaults to “rescaled”.
Examples
>>> # Rescale the pixel values of an asset with values in uint8 to be between 0 and 1 >>> transform = PixelsTransform.rescale_shift( ... asset="my_asset", ... bands=[0, 1, 2], ... scale=[255], ... shift=[0], ... asset_name="rescaled" ... )
>>> # Rescale all bands for "my_asset" to be between 0 and 255 >>> transform = PixelsTransform.rescale_shift( ... asset="my_asset", ... asset_name="rescaled" ... )
>>> # Rescale "red", "green", and "blue" bands for "image" to be between 0 and 1 using a known min/max >>> transform = PixelsTransform.rescale_shift( ... asset="image", ... bands=["red", "green", "blue"], ... scale=[12345, 12345, 12345], ... shift=[123, 456, 789], ... asset_name="rescaled-rgb" ... )
- static default_asset_bands(default_asset_bands)[source]¶
sets the default value of asset_bands in a pixels request on a Dataset.
This is useful for when you are creating a static dataset and want to set the default bands that will be used in the pixels request or for when you have a dataset that has multiple bands and you want to set the default bands that will be used in the pixels request.
- Parameters
default_asset_bands (List[geodesic.boson.asset_bands.AssetBands]) – list of AssetBands objects that will be used as the default bands in the pixels request
Example
>>> # Set the default asset bands to be the first band of the rasterized asset >>> default_asset_transform = PixelsTransform.default_asset_bands([AssetBands(asset="rasterized", bands=[0])])
- static default_datetime_range(start=None, end=None, before_now=None)[source]¶
sets the default datetime range in a pixels request on a Dataset.
This is useful for when you are creating a dataset that points to a very long time history, but by default wish to show a specific time range. This can be useful for things like showing the last 24 hours of data.
- Parameters
start (Optional[Union[datetime.datetime, str]]) – start of the default datetime range. Defaults to None.
end (Optional[Union[datetime.datetime, str]]) – end of the default datetime range. Defaults to None.
before_now (Optional[datetime.timedelta]) – time delta before now to set the start of the default datetime range. If provided, will be used instead of start/end and will dynamically be used based on the current time whenever there is a new request. Defaults to None.
Example
>>> # Set the default datetime range to be the last 24 hours >>> default_datetime_transform = PixelsTransform.default_datetime_range(before_now=datetime.timedelta(days=1))
>>> # Set the default datetime range to be from 2021-01-01 to 2021-01-02 >>> default_datetime_transform = PixelsTransform.default_datetime_range(start="2021-01-01", end="2021-01-02")
- static rasterize(attribute_name=None, value=None, use_z=False, initialize_value=None, invert=False, all_touched=False, add=False, collections=[], asset_name='rasterized', band_name='rasterized', feature_limit=25000)[source]¶
creates a rasterized image from a feature collection as a new raster asset
Rasterize middleware is useful for performing simple aggregations on a feature collection. This can be useful for things like creating a population density raster from a feature collection of population counts or creating a binary raster from a feature collection of labels in a segmentation task.
- Parameters
attribute_name (Optional[str]) – attribute name to rasterize. Defaults to None.
value (Optional[Any]) – value to rasterize. Defaults to None.
use_z (bool) – whether to use the z value of the feature. Defaults to False.
initialize_value (Optional[Any]) – value to initialize the raster with. Defaults to None.
invert (bool) – invert which pixels are rasterize. Defaults to False.
all_touched (bool) – whether to rasterize all pixels touched by the feature. Defaults to False.
add (bool) – whether to add the raster to the asset. Defaults to False.
collections (List[str]) – collections to rasterize. Defaults to [] (all collections).
asset_name (str) – name of the asset to create. Defaults to “rasterized”.
band_name (str) – name of the band to create. Defaults to “rasterized”.
feature_limit (int) – maximum number of features to rasterize. Defaults to 25000.
Examples
>>> # Rasterize the population attribute by summing the values in the attribute for each pixel >>> transform = PixelsTransform.rasterize( ... attribute_name="population", ... add=True, ... asset_name="population_raster", ... band_name="population" ... )
>>> # Rasterize by object by setting the value to 1 wherever there is an object >>> transform = PixelsTransform.rasterize( ... value=1 ... )
>>> # Rasterize by object by setting the value to 1 wherever there is NOT an object >>> transform = PixelsTransform.rasterize( ... value=1, ... invert=True ... )