transforms.aggregate.spatial
Attributes
Functions
|
Rasterize a list of geometries onto the given xarray coordinates. |
|
Return a mask array for the spatial points of data that lie within shapes in shape_list. |
|
Method which creates a list of masked dataarrays. |
|
Method which creates a single masked dataarray based on all features in shapes. |
|
|
|
Apply shape mask to some gridded data. |
|
Apply multiple shape masks to some gridded data. |
|
Apply a shape object to an xarray.DataArray object using the specified 'how' method. |
Module Contents
- transforms.aggregate.spatial.logger
- transforms.aggregate.spatial.rasterize(shape_list, coords, lat_key='latitude', lon_key='longitude', dtype=int, **kwargs)
Rasterize a list of geometries onto the given xarray coordinates. This only works for regular and contiguous latitude and longitude grids.
- Parameters:
(affine.Affine) (shape_list)
(xarray.coords) (coords)
lat_key/lon_key (
nameofthe latitude/longitude variables in the coordinates object)fill (
valuetofill points which are not within the shape_list, defaultis 0)dtype (
datatypeofthe returned mask, defaultis `int`)kwargs (
Any other kwargs accepted by rasterio.features.rasterize)
- Return type:
xr.DataArray mask where points not inside the shape_list are setto fill value
- transforms.aggregate.spatial.mask_contains_points(shape_list, coords, lat_key='lat', lon_key='lon', **kwargs)
Return a mask array for the spatial points of data that lie within shapes in shape_list.
Function uses matplotlib.Path so can accept a list of points, this is much faster than shapely. It was initially included for use with irregular data but has been constructed to also accept regular data and return in the same format as the rasterize function.
- transforms.aggregate.spatial.shapes_to_masks(shapes, target, regular=True, **kwargs)
Method which creates a list of masked dataarrays.
If possible use the shape_mask_iterator.
- transforms.aggregate.spatial.shapes_to_mask(shapes, target, regular=True, **kwargs)
Method which creates a single masked dataarray based on all features in shapes.
If possible use the shape_mask_iterator.
- transforms.aggregate.spatial.get_mask_dim_index(mask_dim, geodataframe, default_index_name='index')
- transforms.aggregate.spatial.mask(dataarray, geodataframe, lat_key=None, lon_key=None, **mask_kwargs)
Apply shape mask to some gridded data.
The geodataframe object is treated as a single mask, any points that lie outside of any of the features are masked
- Parameters:
dataarray – Xarray data object (must have geospatial coordinates).
geodataframe – Geopandas Dataframe containing the polygons for aggregations
lat_key/lon_key – key for latitude/longitude variable, default behaviour is to detect variable keys.
- Returns:
A masked data array/dataset with same dimensions as the input dataarray/dataset. Any point thatdoes not lie in anyofthe featuresofgeodataframe is masked.
- transforms.aggregate.spatial.masks(dataarray, geodataframe, mask_dim=None, lat_key=None, lon_key=None, chunk=True, **mask_kwargs)
Apply multiple shape masks to some gridded data.
Each feauture in shape is treated as an individual mask to apply to data. The data provided is returned with an additional dimension equal in length to the number of features in the shape object, this can result in very large files which will slow down your script. It may be better to loop over individual features, or directly apply the mask with the shapes.reduce.
- Parameters:
dataarray – Xarray data object (must have geospatial coordinates).
geodataframe – Geopandas Dataframe containing the polygons for aggregations
mask_dim – dimension that will be created to accomodate the masked arrays, default is the index of the geodataframe
lat_key/lon_key – key for latitude/longitude variable, default behaviour is to detect variable keys.
chunk (
(optional) bool) – Boolean to indicate whether to use chunking, default = True. This is advised as spatial.masks can create large results. If you are working with small arrays, or you have implemented you own chunking rules you may wish to disable it.
- Returns:
A masked data array with dimensions [feautre_id] + [data.dims].Each sliceoflayer correspondstoa feature in layer.
- transforms.aggregate.spatial.reduce(dataarray, geodataframe=None, *args, **kwargs)
Apply a shape object to an xarray.DataArray object using the specified ‘how’ method.
Geospatial coordinates are reduced to a dimension representing the list of features in the shape object.
- Parameters:
dataarray – Xarray data object (must have geospatial coordinates).
geodataframe – Geopandas Dataframe containing the polygons for aggregations
how – method used to apply mask. Default=’mean’, which calls np.nanmean
weights – Provide weights for aggregation, also accepts recognised keys for weights, e.g. ‘latitude’
lat_key/lon_key – key for latitude/longitude variable, default behaviour is to detect variable keys.
extra_reduce_dims – any additional dimensions to aggregate over when reducing over spatial dimensions
mask_dim – dimension that will be created after the reduction of the spatial dimensions, default is the index of the dataframe
return_as – what format to return the data object, pandas or xarray. Work In Progress
how_label – label to append to variable name in returned object, default is not to append
kwargs – kwargs recognised by the how function
- Returns:
A data array with dimensions features + data.dims not in `’lat’
:class:`,`’lon’:class:.`Each sliceoflayer correspondstoa feature in layer.