3.2.1. common module

This module contains definitions for global attributes, functions, and classes that might be used anywhere in the package.

Attributes:

HORIZON_DEFAULT (int): Default value for ‘’. PERIOD_LENGTH_DEFAULT (int): Default number of years per period. MIN_AGE_DEFAULT (int): Default value for core.Curve.xmin. MAX_AGE_DEFAULT (int): Default value for core.Curve.xmax. CURVE_EPSILON_DEFAULT (float): Defalut value for core.Curve.epsilon. AREA_EPSILON_DEFAULT = 0.01

common.clean_vector_data(src_path, dst_path, dst_name, prop_names, clean=True, tolerance=0.0, preserve_topology=True, logfn='clean_stand_shapefile.log', max_records=None, theme0=None, prop_types=None, driver='ESRI Shapefile', dst_epsg=None, update_area_prop='')[source]

The function cleans a vector data obtained form shapefile and reprojects to a destination shapefile. The output of the function is the path for cleaned shapefile and uncleaned shapefile.

Parameters:
  • src_path (str) – Path to the source shapefile.

  • dst_path (str) – Path to the destination shapefile.

  • dst_name (str) – The name for the destination shapefile.

  • prop_names (list) – List of property names.

  • clean (bool) – If the value of clean is True, the function will do cleaning; otherwise, it will do only reprojecting.

  • tolerance (float) – This tolerance adjust the level of geometry modifications.

  • preserve_topology (bool) – If the value of preserve_topology is True, it will perserve the topology.

  • logfn (str) – The filename for the log file to store the cleaned info.

  • max_records (int) – If required, the user can define the maximum number of records for processing the source shapefile.

  • theme0 (str) – If required, the user can define theme0 for the cleaned shapefile.

  • prop_types (list) – List of tuples showing the property types for the cleaned shapefile.

  • driver (str) – The driver for writing the shapfiles.

  • dst_epsg (int) – If the user specifies dst_epsg, the geometries will be reprojected to the specific CRS.

  • update_area_prop (str) – The property that includes updated area information.

common.harv_cost(piece_size, is_finalcut, is_toleranthw, partialcut_extracare=False, A=1.97, B=0.405, C=0.169, D=0.164, E=0.202, F=13.6, G=8.83, K=0.0, rv=False)[source]

Returns harvest cost.

Parameters:
  • piece_size (float) – Piece size.

  • is_finalcut (bool) – Treatment type (final cut or not).

  • is_toleranthw (bool) – Stand type (tolerant hardwood or not).

  • partialcut_extracare (bool) – Partialcut “extra care” flag.

  • A (float) – Series of regression coefficients (A, B, C, D, E, F, G, K, all with defaults that are extracted from MERIS technical documentation; also see Sebastien Lacroix, BMMB).

  • rv (bool) – Types of variables (default: Variables are deterministic).

common.harv_cost_rv(tv_mu, tv_sigma, N_mu, N_sigma, psr, is_finalcut, is_toleranthw, partialcut_extracare=False, tv_min=50.0, N_min=200.0, ps_min=0.05, E_fromintegral=False, e=0.01, n=1000)[source]

Returns harvest cost.

Parameters:
  • is_finalcut (bool) – Treatment type (final cut or not).

  • is_toleranthw (bool) – Stand type (tolerant hardwood or not).

  • partialcut_extracare (bool) – Partialcut “extra care” flag.

  • A (float) – Series of regression coefficients (A, B, C, D, E, F, G, K, all with defaults that are extracted from MERIS technical documentation; also see Sebastien Lacroix, BMMB).

  • rv (bool) – Types of variables (default: Variables random variates).

Can use either PaCAL numerical integration (sssslow!), or custom numerical integration using Monte Carlo sampling (default).

common.harv_cost_wec(piece_size, is_finalcut, is_toleranthw, sigma, nsigmas=3, **kwargs)[source]

Estimate harvest cost with error correction.

Parameters:
  • piece_size (float) – Mean piece size.

  • is_finalcut (bool) – True if harvest treatment is final cut, False otherwise.

  • is_toleranthw (bool) – True if tolerant hardwood cover type, False otherwise.

  • sigma (bool) – Standard deviation of piece size estimator.

  • nsigmas (int) – Number of standard deviations to model on either side of the mean (default 3).

  • binw (float) – Width of bins for weighted numerical integration, in multiples of sigma (default 1.0).

common.hash_dt(dt, dtype=<MagicMock id='127900229524528'>, nbytes=4)[source]

The function hashes the development type and returns an integer value.

Parameters:
  • dt (str) – Development type.

  • dtype (rasterio.dtype) – The type of the output file (default type is rasterio.int32).

  • nbytes (int) – The number of bytes to consider from the hash (The default value is 4).

common.hex_id(object, digest_size=10)[source]

This function converts an object to a hexadecimal string.

common.is_num(s)[source]

This function checks if a given input has a numerical value.

common.piece_size_ratio(treatment_type, cover_type, piece_size_ratios)[source]

Returns piece size ratio.

Assume Action.is_harvest in [0, 1, 2, 3]

Assume cover_type in [‘r’, ‘m’, ‘f’]

Return vr/vp ratio, where
  • vr is mean piece size of harvested stems, and

  • vp is mean piece size of stand before harvesting.

common.rasterize_stands(shp_path, tif_path, theme_cols, age_col, blk_col='', age_divisor=1.0, d=100.0, dtype=<MagicMock id='127900229532304'>, compress='lzw', round_coords=True, value_func=<function <lambda>>, cap_age=None, verbose=False)[source]

The function rasterizes stands data and stores the data as TIFF file.

Parameters:
  • shp_path (str) – Path to the source shapefile.

  • tif_path (str) – Path to the resulted TIFF file.

  • theme_cols (list) – List of themes.

  • age_col (int) – Age column.

  • blk_col (str)

  • age_divisor (float) – A number to scale stand age values.

  • d (float) – The pixel size of the raster.

  • dtype (rasterio.dtype) – The type of the output file (default type is rasterio.int32).

  • compress (str) – The compression method (The default one is lzw)

  • round_coords (bool) – If ture, the function rounds the coordinates of the ouput file.

  • value_func (function) – A function that is applied to theme columns (in this case, the function replaces hyphens and spaces with underscores and changes all letters to lowercase)

  • cap_age (int) – Maximum stand age defined by usder that will be considered as a cap age for stands (optional)

  • verbose (bool) – (Optional) Verbosity flag. Defaults to False

common.reproject(f, srs_crs, dst_crs)[source]

Reproject a geometry from a source coordinate reference system (CRS) to a destination CRS.

common.reproject_vector_data(src_path, snk_path, snk_epsg, driver='ESRI Shapefile')[source]

When a specific ESPG is defined, this function reprojects vector data from a source shapefile to a destinaiton shapefile using ESRI shapefile as the default driver.

Parameters:
  • src_path (str) – Path to the source shapefile.

  • snk_path (str) – Path to the destination shapefile.

  • snk_epsg (int) – EPSG code for the destination CRS.

  • driver (str) – The driver for writing the shapfiles.

common.sylv_cred(P, vr, vp, formula)[source]

This function returns sylviculture credit ($ per hectare).

Parameters:
  • P (float) – Volume harvested per hectare.

  • vr (float) – Mean piece size of harvested stems.

  • vp (float) – mean piece size of stand before harvesting.

  • formula – formula index (1 to 7).

common.sylv_cred_formula(treatment_type, cover_type)[source]

Returns sylviculture credit formula index.

Parameters:
  • treatment_type (str) – Treatment type.

  • cover_type (str) – Cover type.

common.sylv_cred_rv(P_mu, P_sigma, tv_mu, tv_sigma, N_mu, N_sigma, psr, treatment_type=None, cover_type=None, formula=None, P_min=20.0, tv_min=50.0, N_min=200.0, ps_min=0.05, E_fromintegral=False, e=0.01, n=1000)[source]

This function returns sylviculture credit ($ per hectare).

Parameters:
  • P (float) – Volume harvested per hectare.

  • vr (float) – Mean piece size of harvested stems.

  • vp (float) – mean piece size of stand before harvesting.

  • formula – formula index (1 to 7).

Note

Assumes that variables (P, vr, vp) are random variates (returns expected value of function, using PaCAL packages to model random variates, assuming normal distribution for all three variables). Can use either PaCAL numerical integration (sssslow!), or custom numerical integration using Monte Carlo sampling (default).

common.timed(func)[source]

The function records the execution time of a function.

Parameters:

func (function) – The function to be timed.

common.warp_raster(src, dst_path, dst_crs={'init': 'EPSG:4326'})[source]

The function warpes a raster from its original CRS to a new CRS.

Parameters:
  • src (raserio.DatasetReader) – The source rasterio dataset to be warped.

  • dst_path (str) – The path to save the warped raster

  • dst_crs (dict) – The destination CRS in rasterio format (Default is init’:’EPSG:4326’)