edisgo.io.db

Functions

config_settings(path)

Return a nested dictionary containing the configuration settings.

credentials(path)

Return database connection parameters for the egon-data database.

ssh_tunnel(cred)

Initialize an SSH tunnel to a remote host according to the input arguments.

engine([path, ssh, token])

Engine for local or remote database.

session_scope_egon_data(engine)

Provide a transactional scope around a series of operations.

sql_grid_geom(edisgo_obj)

Returns the grid district geometry as a PostGIS geometry.

get_srid_of_db_table(session, geom_col)

Returns the SRID of a geometry column in a database table.

sql_within(geom_a, geom_b, srid)

Checks if geometry a is completely within geometry b.

sql_intersects(geom_col, geom_shape, srid)

Checks if a geometry column intersects a given geometry.

Module Contents

edisgo.io.db.config_settings(path)

Return a nested dictionary containing the configuration settings.

It’s a nested dictionary because the top level has command names as keys and dictionaries as values where the second level dictionary has command line switches applicable to the command as keys and the supplied values as values.

So you would obtain the --database-name configuration setting used by the current invocation of egon-data via

settings()["egon-data"]["--database-name"]
Parameters:

path (pathlib.Path or str) – Path to configuration YAML file of egon-data database.

Returns:

Nested dictionary containing the egon-data and optional ssh tunnel configuration settings.

Return type:

dict

edisgo.io.db.credentials(path)

Return database connection parameters for the egon-data database.

Parameters:

path (pathlib.Path or str) – Path to configuration YAML file of egon-data database.

Returns:

Complete DB connection information.

Return type:

dict

edisgo.io.db.ssh_tunnel(cred)

Initialize an SSH tunnel to a remote host according to the input arguments. See https://sshtunnel.readthedocs.io/en/latest/ for more information.

Parameters:

cred (dict) – Complete DB connection information.

Returns:

Name of local port.

Return type:

str

edisgo.io.db.engine(path=None, ssh=False, token=None)

Engine for local or remote database.

Parameters:
  • path (str or pathlib.Path, optional (default=None)) – Path to configuration YAML file of egon-data database.

  • ssh (bool (default=False)) – If False, connects to the remote Open Energy Platform database (using the token, see parameter token). If True, establishes an ssh tunnel to a local egon-data database using the connection information in the configuration YAML given through path.

  • token (str or pathlib.Path, optional (default=None)) – Token for database connection or path to text file containing token. If empty the default token file in the config folder OEP_TOKEN.txt will be used. If the default token file is not found, no token will be used and the connection will be established without token.

Returns:

Database engine

Return type:

sqlalchemy.Engine

edisgo.io.db.session_scope_egon_data(engine)

Provide a transactional scope around a series of operations.

edisgo.io.db.sql_grid_geom(edisgo_obj)

Returns the grid district geometry as a PostGIS geometry.

Parameters:

edisgo_obj (EDisGo) – EDisGo object whose grid district geometry is used.

Returns:

Grid district geometry built from its WKT and SRID, for use in spatial SQL queries.

Return type:

Geometry

edisgo.io.db.get_srid_of_db_table(session, geom_col)

Returns the SRID of a geometry column in a database table.

Parameters:
  • session (Session) – SQLAlchemy session used to query the database.

  • geom_col (InstrumentedAttribute) – Geometry column whose spatial reference identifier is determined.

Returns:

Spatial reference identifier (SRID) of the geometry column.

Return type:

int

edisgo.io.db.sql_within(geom_a, geom_b, srid)

Checks if geometry a is completely within geometry b.

Parameters:
  • geom_a (Geometry) – Geometry within geom_b.

  • geom_b (Geometry) – Geometry containing geom_a.

  • srid (int) – SRID geometries are transformed to in order to use the same SRID for both geometries.

edisgo.io.db.sql_intersects(geom_col, geom_shape, srid)

Checks if a geometry column intersects a given geometry.

Parameters:
  • geom_col (InstrumentedAttribute) – Geometry column to test for intersection.

  • geom_shape (Geometry) – Geometry to test the column against.

  • srid (int) – SRID both geometries are transformed to before the intersection test.