geodata.Mask#

class geodata.Mask(name: str, layer_path: str | list | dict | None = None, layer_name: str | list | None = None, mask_dir: str = MASK_DIR, **kwargs)#

A class to create, manipulate, and load geodata mask object that takes GeoTIFF or shp files as input.

Creating a new mask object. Layer_path will take the file name(s) without extension as the default layer name. If layer name(s) is/are specified, it will take the corresponding layer_name(s) as the name for new layer(s). Layer path can also be a dictionary of {layer_name: layer_path(str)} key-value pairs.

Parameters:
  • name (str) – Name for the new mask object.

  • layer_path (str or dict) – Path(s) of new layers to beadded to the mask object. It can be None. In that case, nothing will be added.

  • layer_name (str or list) – Names for the incoming layers. If layer_path is a dictionary, layer_name will be ignored.

  • mask_dir (str) – The path to where the mask object would be saved/stored. By default, it should be the MASK_PATH constant in config.py.

  • **kwargs – Additional arguments passed to the add_layer method, only used if layers need to be added.

MergingMethods#
name#
layers: dict[str, rasterio.DatasetReader]#
merged_mask = None#
shape_mask#
saved = False#
mask_dir#

Methods#

add_layer(layer_path[, layer_name, replace, trim, ...])

Add a layer to the mask from given path(s) by calling the _add_layer() method.

remove_layer(name)

Remove a layer in the mask given layer name.

rename_layer(layer_name, new_name)

Rename a layer in the mask given name.

get_res(→ dict[str, tuple[float, float]])

Get resolutions of layers.

get_bounds(→ dict[str, tuple[float, float, float, float]])

Get boundary information of layers

find_inner_bound(→ tuple[float, float, float, float])

Find the bounding box that's within specified layers.

crop_layer(layer_name, bounds[, lat_lon_bounds, ...])

Crop a layer of mask object using crop_raster method.

trim_layer(layer_name[, dest_layer_name])

Trim a layer of mask object using trim_raster method.

filter_layer(layer_name[, dest_layer_name, values, ...])

Filter a layer of mask object using filter_raster method.

merge_layer(→ rasterio.DatasetReader)

Merge multiple and flatten multiple layers from self.layers using either 'and'

remove_merge_layer()

Remove the saved merged mask.

add_shape_layer(shapes[, reference_layer, resolution, ...])

Add shapes to the mask layers. This is different from shape extractions,

extract_shapes(→ dict[str, rasterio.DatasetReader])

Extract the shapes on the (by default) merged_mask layer, and save the result

remove_shapes(names)

Remove a shape from shape_mask dictionary

load_merged_xr(→ xarray.DataArray)

Using xarray to load the merged_layer masks.

load_shape_xr(→ dict[str, xarray.DataArray])

Using xarray to load the shape masks.

close_files()

Close all the opened rasters. This method will disable further save_mask() call.

save_mask([name, mask_dir, close_files])

Save a mask object to the directory. By default, the directory should be the

from_name(name[, mask_dir])

Load a previously saved mask object by name.