src.flood_model.serve_model

Takes generated models and adds them to GeoServer so they can be retrieved by API calls by the frontend or other clients

Attributes

log

_xml_header

Functions

convert_nc_to_gtiff(→ pathlib.Path)

Creates a GeoTiff file from a netCDF model output. The Tiff represents the max flood height in the model output.

upload_gtiff_to_store(→ None)

Uploads a GeoTiff file to a new GeoServer store, to enable serving.

create_layer_from_store(→ None)

Creates a GeoServer Layer from a GeoServer store, making it ready to serve.

get_geoserver_url(→ str)

Retrieves full GeoServer URL from environment variables.

add_gtiff_to_geoserver(→ None)

Uploads a GeoTiff file to GeoServer, ready for serving to clients.

create_workspace_if_not_exists(→ None)

Creates a geoserver workspace if it does not currently exist.

create_datastore_layer(→ None)

create_building_layers(→ None)

Creates dynamic geoserver layers "nz_building_outlines" and "building_flood_status" for the given workspace.

create_db_store_if_not_exists(→ None)

Creates PostGIS database store in a geoserver workspace for a given database.

create_building_database_views_if_not_exists(→ None)

Creates a geoserver workspace and building layers using database views if they do not currently exist.

style_exists(→ bool)

Checks if a geoserver style definition already exists for a given style_name.

create_viridis_style_if_not_exists(→ None)

Creates a geoserver style for rasters using the viridis colour scale

add_model_output_to_geoserver(→ None)

Adds the model output max depths to GeoServer, ready for serving.

Module Contents

src.flood_model.serve_model.log
src.flood_model.serve_model._xml_header
src.flood_model.serve_model.convert_nc_to_gtiff(nc_file_path: pathlib.Path) pathlib.Path

Creates a GeoTiff file from a netCDF model output. The Tiff represents the max flood height in the model output.

Parameters:

nc_file_path (pathlib.Path) – The file path to the netCDF file.

Returns:

The filepath of the new GeoTiff file.

Return type:

pathlib.Path

src.flood_model.serve_model.upload_gtiff_to_store(geoserver_url: str, gtiff_filepath: pathlib.Path, store_name: str, workspace_name: str) None

Uploads a GeoTiff file to a new GeoServer store, to enable serving.

Parameters:
  • geoserver_url (str) – The URL to the geoserver instance.

  • gtiff_filepath (pathlib.Path) – The filepath to the GeoTiff file to be served.

  • store_name (str) – The name of the new Geoserver store to be created.

  • workspace_name (str) – The name of the existing GeoServer workspace that the store is to be added to.

Returns:

This function does not return anything

Return type:

None

src.flood_model.serve_model.create_layer_from_store(geoserver_url: str, layer_name: str, native_crs: str, workspace_name: str) None

Creates a GeoServer Layer from a GeoServer store, making it ready to serve.

Parameters:
  • geoserver_url (str) – The URL to the geoserver instance.

  • layer_name (str) – Defines the name of the layer in GeoServer.

  • native_crs (str) – The WKT form of the CRS of the data being shown in the layer.

  • workspace_name (str) – The name of the existing GeoServer workspace that the store is to be added to.

Returns:

This function does not return anything

Return type:

None

src.flood_model.serve_model.get_geoserver_url() str

Retrieves full GeoServer URL from environment variables.

Returns:

The full GeoServer URL

Return type:

str

src.flood_model.serve_model.add_gtiff_to_geoserver(gtiff_filepath: pathlib.Path, workspace_name: str, model_id: int) None

Uploads a GeoTiff file to GeoServer, ready for serving to clients.

Parameters:
  • gtiff_filepath (pathlib.Path) – The filepath to the GeoTiff file to be served.

  • workspace_name (str) – The name of the existing GeoServer workspace that the store is to be added to.

  • model_id (int) – The id of the model being added, to facilitate layer naming.

Returns:

This function does not return anything

Return type:

None

src.flood_model.serve_model.create_workspace_if_not_exists(workspace_name: str) None

Creates a geoserver workspace if it does not currently exist.

Parameters:

workspace_name (str) – The name of the workspace to create if it does not exists.

Returns:

This function does not return anything.

Return type:

None

src.flood_model.serve_model.create_datastore_layer(workspace_name, data_store_name: str, layer_name, metadata_elem: str = '') None
src.flood_model.serve_model.create_building_layers(workspace_name: str, data_store_name: str) None

Creates dynamic geoserver layers “nz_building_outlines” and “building_flood_status” for the given workspace. If they already exist then does nothing. “building_flood_status” required viewparam=scenario:{model_id} to dynamically fetch correct flood statuses.

Parameters:
  • workspace_name (str) – The name of the workspace to create views for

  • data_store_name (str) – The name of the datastore that the building layer is being created from

Returns:

This function does not return anything

Return type:

None

src.flood_model.serve_model.create_db_store_if_not_exists(db_name: str, workspace_name: str, new_data_store_name: str) None

Creates PostGIS database store in a geoserver workspace for a given database. If it already exists, does not do anything.

Parameters:
  • db_name (str) – The name of the connected database, to connect datastore to

  • workspace_name (str) – The name of the workspace to create views for

  • new_data_store_name (str) – The name of the new datastore to create

Returns:

This function does not return anything

Return type:

None

src.flood_model.serve_model.create_building_database_views_if_not_exists() None

Creates a geoserver workspace and building layers using database views if they do not currently exist. These only need to be created once per database.

Returns:

This function does not return anything.

Return type:

None

src.flood_model.serve_model.style_exists(style_name: str) bool

Checks if a geoserver style definition already exists for a given style_name. The style definition may be empty.

Parameters:

style_name (str) – The name of the style to check for

Returns:

True if the style exists, although it may be empty. False if it does not exist.

Return type:

bool

src.flood_model.serve_model.create_viridis_style_if_not_exists() None

Creates a geoserver style for rasters using the viridis colour scale

Returns:

This function does not return anything

Return type:

None

src.flood_model.serve_model.add_model_output_to_geoserver(model_output_path: pathlib.Path, model_id: int) None

Adds the model output max depths to GeoServer, ready for serving. The GeoServer layer name will be f”Output_{model_id}” and the workspace name will be “{db_name}-dt-model-outputs”

Parameters:
  • model_output_path (pathlib.Path) – The file path to the model output to serve.

  • model_id (int) – The database id of the model output.

Returns:

This function does not return anything

Return type:

None