natcap.invest.recreation package

Submodules

natcap.invest.recreation.buffered_numpy_disk_map module

Buffered file manager module.

class natcap.invest.recreation.buffered_numpy_disk_map.BufferedNumpyDiskMap(manager_filename, max_bytes_to_buffer)

Bases: object

Persistent object to append and read numpy arrays to unique keys.

This object is abstractly a key/value pair map where the operations are to append, read, and delete numpy arrays associated with those keys. The object attempts to keep data in RAM as much as possible and saves data to files on disk to manage memory and persist between instantiations.

append(array_id, array_data)

Append data to the file.

Parameters:
  • array_id (int) – unique key to identify the array node
  • array_data (numpy.ndarray) – data to append to node.
Returns:

None

delete(array_id)

Delete node array_id from disk and cache.

flush()

Method to flush data in memory to disk.

read(array_id)

Read the entirety of the file.

Internally this might mean that part of the file is read from disk and the end from the buffer or any combination of those.

Parameters:array_id (string) – unique node id to read
Returns:contents of node as a numpy.ndarray.

natcap.invest.recreation.out_of_core_quadtree module

A hierarchical spatial index for fast culling of points in 2D space.

class natcap.invest.recreation.out_of_core_quadtree.OutOfCoreQuadTree

Bases: object

An out of core quad tree spatial indexing structure.

add_points()

Add a list of points to the current node.

This function will split the current node if the added points exceed the maximum number of points allowed per node and is already not at the maximum level.

Parameters:
  • point_list (numpy.ndarray) – a numpy array of (data, x_coord, y_coord) tuples
  • left_bound (int) – left index inclusive of points to consider under point_list
  • right_bound (int) – right index non-inclusive of points to consider under point_list
Returns:

None

build_node_shapes()

Add features to an ogr.Layer to visualize quadtree segmentation.

Parameters:ogr_polygon_layer (ogr.layer) – an ogr polygon layer with fields ‘n_points’ (int) and ‘bb_box’ (string) defined.
Returns:None
flush()

Flush any cached data to disk.

get_intersecting_points_in_bounding_box()

Get list of data that is contained by bounding_box.

This function takes in a bounding box and returns a list of (data, lat, lng) tuples that are contained in the leaf nodes that intersect that bounding box.

Parameters:bounding_box (list) – of the form [xmin, ymin, xmax, ymax]
Returns:numpy.ndarray array of (data, x_coord, lng) of nodes that intersect the bounding box.
get_intersecting_points_in_polygon()

Return the points contained in shapely_prepared_polygon.

This function is a high performance test routine to return the points contained in the shapely_prepared_polygon that are stored in self‘s representation of a quadtree.

Parameters:shapely_polygon (ogr.DataSource) – a polygon datasource to bound against
Returns:deque of (data, x_coord, y_coord) of nodes that are contained in shapely_prepared_polygon.
n_nodes()

Return the number of nodes in the quadtree

n_points()

Return the number of nodes in the quadtree

next_available_blob_id = 0

natcap.invest.recreation.recmodel_client module

InVEST Recreation Client.

natcap.invest.recreation.recmodel_client.delay_op(last_time, time_delay, func)

Execute func if last_time + time_delay >= current time.

Parameters:
  • last_time (float) – last time in seconds that func was triggered
  • time_delay (float) – time to wait in seconds since last_time before triggering func
  • func (function) – parameterless function to invoke if current_time >= last_time + time_delay
Returns:

If func was triggered, return the time which it was triggered in seconds, otherwise return last_time.

natcap.invest.recreation.recmodel_client.execute(args)

Recreation.

Execute recreation client model on remote server.

Parameters:
  • args['workspace_dir'] (string) – path to workspace directory
  • args['aoi_path'] (string) – path to AOI vector
  • args['hostname'] (string) – FQDN to recreation server
  • args['port'] (string or int) – port on hostname for recreation server
  • args['start_year'] (string) – start year in form YYYY. This year is the inclusive lower bound to consider points in the PUD and regression.
  • args['end_year'] (string) – end year in form YYYY. This year is the inclusive upper bound to consider points in the PUD and regression.
  • args['grid_aoi'] (boolean) – if true the polygon vector in args[‘aoi_path’] should be gridded into a new vector and the recreation model should be executed on that
  • args['grid_type'] (string) – optional, but must exist if args[‘grid_aoi’] is True. Is one of ‘hexagon’ or ‘square’ and indicates the style of gridding.
  • args['cell_size'] (string/float) – optional, but must exist if args[‘grid_aoi’] is True. Indicates the cell size of square pixels and the width of the horizontal axis for the hexagonal cells.
  • args['compute_regression'] (boolean) – if True, then process the predictor table and scenario table (if present).
  • args['predictor_table_path'] (string) –

    required if args[‘compute_regression’] is True. Path to a table that describes the regression predictors, their IDs and types. Must contain the fields ‘id’, ‘path’, and ‘type’ where:

    • ‘id’: is a <=10 character length ID that is used to uniquely describe the predictor. It will be added to the output result shapefile attribute table which is an ESRI Shapefile, thus limited to 10 characters.
    • ‘path’: an absolute or relative (to this table) path to the predictor dataset, either a vector or raster type.
    • ‘type’: one of the following,
      • ‘raster_mean’: mean of values in the raster under the response polygon
      • ‘raster_sum’: sum of values in the raster under the response polygon
      • ‘point_count’: count of the points contained in the response polygon
      • ‘point_nearest_distance’: distance to the nearest point from the response polygon
      • ‘line_intersect_length’: length of lines that intersect with the response polygon in projected units of AOI
      • ‘polygon_area’: area of the polygon contained within response polygon in projected units of AOI
  • args['scenario_predictor_table_path'] (string) – (optional) if present runs the scenario mode of the recreation model with the datasets described in the table on this path. Field headers are identical to args[‘predictor_table_path’] and ids in the table are required to be identical to the predictor list.
  • args['results_suffix'] (string) – optional, if exists is appended to any output file paths.
Returns:

None

natcap.invest.recreation.recmodel_server module

InVEST Recreation Server.

class natcap.invest.recreation.recmodel_server.RecModel(*args, **kwargs)

Bases: object

Class that manages RPCs for calculating photo user days.

calc_photo_user_days_in_aoi(*args, **kwargs)

General purpose try/except wrapper.

fetch_workspace_aoi(*args, **kwargs)

General purpose try/except wrapper.

get_valid_year_range()

Return the min and max year queriable.

Returns:(min_year, max_year)
get_version()

Return the rec model server version.

This string can be used to uniquely identify the PUD database and algorithm for publication in terms of reproducibility.

natcap.invest.recreation.recmodel_server.build_quadtree_shape(quad_tree_shapefile_path, quadtree, spatial_reference)

Generate a vector of the quadtree geometry.

Parameters:
  • quad_tree_shapefile_path (string) – path to save the vector
  • quadtree (out_of_core_quadtree.OutOfCoreQuadTree) – quadtree data structure
  • spatial_reference (osr.SpatialReference) – spatial reference for the output vector
Returns:

None

natcap.invest.recreation.recmodel_server.construct_userday_quadtree(initial_bounding_box, raw_photo_csv_table, cache_dir, max_points_per_node)

Construct a spatial quadtree for fast querying of userday points.

Parameters:
  • initial_bounding_box (list of int) –
  • () (raw_photo_csv_table) –
  • cache_dir (string) – path to a directory that can be used to cache the quadtree files on disk
  • max_points_per_node (int) – maximum number of points to allow per node of the quadree. A larger amount will cause the quadtree to subdivide.
Returns:

None

natcap.invest.recreation.recmodel_server.execute(args)

Launch recreation server and parse/generate quadtree if necessary.

A call to this function registers a Pyro RPC RecModel entry point given the configuration input parameters described below.

There are many methods to launch a server, including at a Linux command line as shown:

nohup python -u -c “import natcap.invest.recreation.recmodel_server;
args={‘hostname’:’$LOCALIP’,
‘port’:$REC_SERVER_PORT, ‘raw_csv_point_data_path’: $POINT_DATA_PATH, ‘max_year’: $MAX_YEAR, ‘min_year’: $MIN_YEAR, ‘cache_workspace’: $CACHE_WORKSPACE_PATH’};

natcap.invest.recreation.recmodel_server.execute(args)”

Parameters:
  • args['raw_csv_point_data_path'] (string) – path to a csv file of the format
  • args['hostname'] (string) – hostname to host Pyro server.
  • args['port'] (int/or string representation of int) – port number to host Pyro entry point.
  • args['max_year'] (int) – maximum year allowed to be queries by user
  • args['min_year'] (int) – minimum valid year allowed to be queried by user
Returns:

Never returns

natcap.invest.recreation.recmodel_workspace_fetcher module

InVEST recreation workspace fetcher.

natcap.invest.recreation.recmodel_workspace_fetcher.execute(args)

Fetch workspace from remote server.

After the call a .zip file exists at args[‘workspace_dir’] named args[‘workspace_id’] + ‘.zip’ and contains the zipped workspace of that model run.

Parameters:
  • args['workspace_dir'] (string) – path to workspace directory
  • args['hostname'] (string) – FQDN to recreation server
  • args['port'] (string or int) – port on hostname for recreation server
  • args['workspace_id'] (string) – workspace identifier
Returns:

None

Module contents