natcap.invest.validation

Common validation utilities for InVEST models.

natcap.invest.validation.CHECK_ALL_KEYS = None

A flag to pass to the validation context manager indicating that all keys should be checked.

natcap.invest.validation.args_enabled(args, model_spec)

Get enabled/disabled status of arg fields given their values and spec.

Parameters:
  • args (dict) – Dict mapping arg keys to user-provided values

  • model_spec (dict) – MODEL_SPEC dictionary

Returns:

Dictionary mapping each arg key to a boolean value - True if the arg field should be enabled, False otherwise

natcap.invest.validation.check_spatial_overlap(spatial_filepaths_list, different_projections_ok=False)

Check that the given spatial files spatially overlap.

Parameters:
  • spatial_filepaths_list (list) – A list of files that can be opened with GDAL. Must be on the local filesystem.

  • different_projections_ok=False (bool) – Whether it’s OK for the input spatial files to have different projections. If True, all projections will be converted to WGS84 before overlap is checked.

Returns:

A string error message if an error is found. None otherwise.

natcap.invest.validation.get_invalid_keys(validation_warnings)

Get the invalid keys from a validation warnings list.

Parameters:

validation_warnings (list) – A list of two-tuples where the first item is an iterable of string args keys affected and the second item is a string error message.

Returns:

A set of the string args keys found across all of the first elements in the validation tuples.

natcap.invest.validation.get_sufficient_keys(args)

Determine which keys in args are sufficient.

A sufficient key is one that is:

  1. Present within args

  2. Does not have a value of '' or None.

Parameters:

args (dict) – An args dict of string keys to serializeable values.

Returns:

A set of keys from args that are sufficient.

natcap.invest.validation.invest_validator(validate_func)

Decorator to enforce characteristics of validation inputs and outputs.

Attributes of inputs and outputs that are enforced are:

  • args parameter to validate must be a dict

  • limit_to parameter to validate must be either None or a string (str or unicode) that exists in the args dict.

  • All keys in args must be strings

  • Decorated validate func must return a list of 2-tuples, where each 2-tuple conforms to these rules:

    • The first element of the 2-tuple is an iterable of strings. It is an error for the first element to be a string.

    • The second element of the 2-tuple is a string error message.

In addition, this validates the n_workers argument if it’s included.

Raises:

AssertionError when an invalid format is found.

Example:

from natcap.invest import validation
@validation.invest_validator
def validate(args, limit_to=None):
    # do your validation here
natcap.invest.validation.load_fields_from_vector(filepath, layer_id=0)

Load fieldnames from a given vector.

Parameters:
  • filepath (string) – The path to a GDAL-compatible vector on disk.

  • layer_id=0 (string or int) – The identifier for the layer to use.

Returns:

A list of string fieldnames within the target layer.

natcap.invest.validation.validate(args, model_spec)

Validate an args dict against a model spec.

Validates an arguments dictionary according to the rules laid out in spec.

Parameters:
  • args (dict) – The InVEST model args dict to validate.

  • model_spec (dict) – The InVEST model spec dict to validate against.

Returns:

A list of tuples where the first element of the tuple is an iterable of keys affected by the error in question and the second element of the tuple is the string message of the error. If no validation errors were found, an empty list is returned.