natcap.invest.reports.vector_utils

natcap.invest.reports.vector_utils.create_aggregate_map(geodataframe, xy_ratio, attribute, colorscheme, title, divergent=False)

Generate Altair choropleth map JSON of a vector attribute.

Parameters:
  • geodataframe (geodataframe) – The source data for the plots, as a geodataframe.

  • xy_ratio (float) – The aspect ratio of the bounding box (width/height).

  • attribute (str) – The name of the vector attribute to plot.

  • colorscheme (str) – The Altair color scheme to use.

  • title (str) – The title for the plot.

  • divergent (bool) – Whether the data are divergent. If divergent, domainMid is set to 0.

Returns:

Altair chart JSON, to be passed to the report template.

natcap.invest.reports.vector_utils.generate_results_table_from_vector(filepath: str, target_cols: list[int | str] = [], cols_to_sum: list[str] = []) str | tuple[str, str | None]

Generate HTML table—and, optionally, HTML table of totals—from a vector.

Totals are calculated for every column named in cols_to_sum, if the main table contains more than one row. If cols_to_sum is empty (the default), no totals table is generated. If cols_to_sum is _not_ empty, but the main table contains only one row, the totals table is None.

Parameters:
  • filepath (str) – path to vector file (e.g., Shapefile or GeoPackage).

  • target_cols (list[int | str], optional) – indices and/or names of columns to include in table. If not specified, all source columns are included.

  • cols_to_sum (list[str], optional) – names of columns to include in totals table. Defaults to an empty list.

Returns:

HTML table containing all data from the vector’s

attribute table.

html_table_totals (str | None, optional): HTML table containing totals,

if cols_to_sum is not empty and the main table contains more than one row. If the main table contains only one row, this output is None. If cols_to_sum is empty, this output is omitted.

Return type:

html_table_main (str)

natcap.invest.reports.vector_utils.get_geojson_bbox(geodataframe)

Get the bounding box of a GeoDataFrame as a GeoJSON feature.

Also calculate its aspect ratio. These are useful for cropping other layers in altair plots.

Parameters:

geodataframe (geopandas.GeoDataFrame)

Returns:

A 2-tuple containing:
  • extent_feature (dict): A GeoJSON feature representing the bounding box of the input GeoDataFrame.

  • xy_ratio (float): The aspect ratio of the bounding box (width/height).

Return type:

tuple

natcap.invest.reports.vector_utils.get_vector_attr_table_caption(vector_spec: VectorOutput, fields_to_include: list[str] = []) list[str]

Generate caption for a vector attribute table.

Useful when column names are abbreviated or otherwise unclear.

Parameters:
  • vector_spec (natcap.invest.spec.VectorOutput) – the specification of the vector output.

  • fields_to_include (list[str], optional) – names of fields to include in the caption. If no list is provided, all fields defined in the spec are included in the caption.

Returns:

a list of formatted strings suitable for passing to the natcap.invest.reports caption macro with definition_list=True.

Return type:

caption (list[str])