src.digitaltwin.tables

This script contains SQLAlchemy models for various database tables and utility functions for database operations.

Attributes

Base

Classes

GeospatialLayers

Class representing the 'geospatial_layers' table.

UserLogInfo

Class representing the 'user_log_information' table.

RiverNetworkExclusions

Class representing the 'rec_network_exclusions' table.

RiverNetwork

Class representing the 'rec_network' table.

BGFloodModelOutput

Class representing the 'bg_flood_model_output' table.

BuildingFloodStatus

Class representing the 'building_flood_status' table.

Functions

create_table(→ None)

Create a table in the database if it doesn't already exist, using the provided engine.

check_table_exists(→ bool)

Check if a table exists in the database.

execute_query(→ None)

Execute the given query on the provided engine using a session.

Module Contents

src.digitaltwin.tables.Base
class src.digitaltwin.tables.GeospatialLayers

Bases: Base

Class representing the ‘geospatial_layers’ table.

__tablename__

Name of the database table.

Type:

str

unique_id

Unique identifier for each geospatial layer entry (primary key).

Type:

int

data_provider

Name of the data provider.

Type:

str

layer_id

Identifier for the layer.

Type:

int

table_name

Name of the table containing the data.

Type:

str

unique_column_name

Name of the unique column in the table.

Type:

Optional[str]

coverage_area

Coverage area of the geospatial data, e.g. ‘New Zealand’.

Type:

Optional[str]

url

URL pointing to the geospatial layer.

Type:

str

__tablename__ = 'geospatial_layers'
unique_id
data_provider
layer_id
table_name
unique_column_name
coverage_area
url
__table_args__
class src.digitaltwin.tables.UserLogInfo

Bases: Base

Class representing the ‘user_log_information’ table.

__tablename__

Name of the database table.

Type:

str

unique_id

Unique identifier for each log entry (primary key).

Type:

int

source_table_list

A list of tables (geospatial layers) associated with the log entry.

Type:

Dict[str]

created_at

Timestamp indicating when the log entry was created.

Type:

datetime

geometry

Geometric representation of the catchment area coverage.

Type:

Polygon

__tablename__ = 'user_log_information'
unique_id
source_table_list
created_at
geometry
class src.digitaltwin.tables.RiverNetworkExclusions

Bases: Base

Class representing the ‘rec_network_exclusions’ table.

__tablename__

Name of the database table.

Type:

str

rec_network_id

An identifier for the river network associated with each new run.

Type:

int

objectid

An identifier for the REC river object matching from the ‘rec_data’ table.

Type:

int

exclusion_cause

Cause of exclusion, i.e., the reason why the REC river geometry was excluded.

Type:

str

geometry

Geometric representation of the excluded REC river features.

Type:

LineString

__tablename__ = 'rec_network_exclusions'
rec_network_id
objectid
exclusion_cause
geometry
__table_args__
class src.digitaltwin.tables.RiverNetwork

Bases: Base

Class representing the ‘rec_network’ table.

__tablename__

Name of the database table.

Type:

str

rec_network_id

An identifier for the river network associated with each new run (primary key).

Type:

int

network_path

Path to the REC river network file.

Type:

str

network_data_path

Path to the REC river network data file for the AOI.

Type:

str

created_at

Timestamp indicating when the output was created.

Type:

datetime

geometry

Geometric representation of the catchment area coverage.

Type:

Polygon

__tablename__ = 'rec_network'
rec_network_id
network_path
network_data_path
created_at
geometry
class src.digitaltwin.tables.BGFloodModelOutput

Bases: Base

Class representing the ‘bg_flood_model_output’ table.

__tablename__

Name of the database table.

Type:

str

unique_id

Unique identifier for each entry (primary key).

Type:

int

file_name

Name of the flood model output file.

Type:

str

file_path

Path to the flood model output file.

Type:

str

created_at

Timestamp indicating when the output was created.

Type:

datetime

geometry

Geometric representation of the catchment area coverage.

Type:

Polygon

__tablename__ = 'bg_flood_model_output'
unique_id
file_name
file_path
created_at
geometry
class src.digitaltwin.tables.BuildingFloodStatus

Bases: Base

Class representing the ‘building_flood_status’ table. Represents if a building is flooded for a given flood model output

__tablename__

Name of the database table.

Type:

str

unique_id

Unique identifier for each entry (primary key).

Type:

int

building_outline_id

Foreign key building outline id matching from nz_building_outlines table

Type:

int

is_flooded

If the building is flooded or not

Type:

bool

flood_model_id

Foreign key mathing the unique_id from bg_flood_model_output table

Type:

int.

__tablename__ = 'building_flood_status'
unique_id
building_outline_id
is_flooded
flood_model_id
src.digitaltwin.tables.create_table(engine: sqlalchemy.engine.Engine, table: Base) None

Create a table in the database if it doesn’t already exist, using the provided engine.

Parameters:
  • engine (Engine) – The engine used to connect to the database.

  • table (Base) – Class representing the table to create.

Returns:

This function does not return any value.

Return type:

None

src.digitaltwin.tables.check_table_exists(engine: sqlalchemy.engine.Engine, table_name: str, schema: str = 'public') bool

Check if a table exists in the database.

Parameters:
  • engine (Engine) – The engine used to connect to the database.

  • table_name (str) – The name of the table to check for existence.

  • schema (str = "public") – The name of the schema where the table resides. Defaults to “public”.

Returns:

True if the table exists, False otherwise.

Return type:

bool

src.digitaltwin.tables.execute_query(engine: sqlalchemy.engine.Engine, query) None

Execute the given query on the provided engine using a session.

Parameters:
  • engine (Engine) – The engine used to connect to the database.

  • query – The query to be executed.

Returns:

This function does not return any value.

Return type:

None

Raises:

Exception – If an error occurs during the execution of the query.