Administration

This module defines the functions used for administration tasks.

geoalchemy2.admin.select_dialect(dialect_name)[source]

Select the dialect from its name.

geoalchemy2.admin.setup_ddl_event_listeners()[source]

Setup the DDL event listeners to automatically process spatial columns.

Common objects

This module defines functions used by several dialects.

geoalchemy2.admin.dialects.common._get_dispatch_info(table, bind, check_col_management=None)[source]

Get info required for dispatch events.

geoalchemy2.admin.dialects.common._update_table_for_dispatch(table, regular_cols)[source]

Update the table before dispatch events.

geoalchemy2.admin.dialects.common.check_management(*args)[source]

Default function to check management (always True by default).

geoalchemy2.admin.dialects.common.setup_create_drop(table, bind, check_col_management=None)[source]

Prepare the table for before_create and before_drop events.

PostgreSQL-specific objects

This module defines specific functions for Postgresql dialect.

geoalchemy2.admin.dialects.postgresql.after_create(table, bind, **kw)[source]

Handle spatial indexes during the after_create event.

geoalchemy2.admin.dialects.postgresql.after_drop(table, bind, **kw)[source]

Handle spatial indexes during the after_drop event.

geoalchemy2.admin.dialects.postgresql.before_create(table, bind, **kw)[source]

Handle spatial indexes during the before_create event.

geoalchemy2.admin.dialects.postgresql.before_drop(table, bind, **kw)[source]

Handle spatial indexes during the before_drop event.

geoalchemy2.admin.dialects.postgresql.check_management(column)[source]

Check if the column should be managed.

geoalchemy2.admin.dialects.postgresql.create_spatial_index(bind, table, col)[source]

Create spatial index on the given column.

geoalchemy2.admin.dialects.postgresql.reflect_geometry_column(inspector, table, column_info)[source]

Reflect a column of type Geometry with Postgresql dialect.

MySQL-specific objects

This module defines specific functions for MySQL dialect.

geoalchemy2.admin.dialects.mysql.after_create(table, bind, **kw)[source]

Handle spatial indexes during the after_create event.

geoalchemy2.admin.dialects.mysql.before_create(table, bind, **kw)[source]

Handle spatial indexes during the before_create event.

geoalchemy2.admin.dialects.mysql.reflect_geometry_column(inspector, table, column_info)[source]

Reflect a column of type Geometry with Postgresql dialect.

geoalchemy2.admin.dialects.mysql.register_mysql_mapping(mapping)[source]

Register compilation mappings for the given functions.

Parameters:

mapping

Should have the following form:

{
    "function_name_1": "mysql_function_name_1",
    "function_name_2": "mysql_function_name_2",
    ...
}

SQLite-specific objects

This module defines specific functions for SQLite dialect.

geoalchemy2.admin.dialects.sqlite._setup_dummy_type(table, gis_cols)[source]

Setup dummy type for new Geometry columns so they can be updated later.

geoalchemy2.admin.dialects.sqlite.after_create(table, bind, **kw)[source]

Handle spatial indexes during the after_create event.

geoalchemy2.admin.dialects.sqlite.after_drop(table, bind, **kw)[source]

Handle spatial indexes during the after_drop event.

geoalchemy2.admin.dialects.sqlite.before_create(table, bind, **kw)[source]

Handle spatial indexes during the before_create event.

geoalchemy2.admin.dialects.sqlite.before_drop(table, bind, **kw)[source]

Handle spatial indexes during the before_drop event.

geoalchemy2.admin.dialects.sqlite.create_spatial_index(bind, table, col)[source]

Create spatial index on the given column.

geoalchemy2.admin.dialects.sqlite.disable_spatial_index(bind, table, col)[source]

Disable spatial indexes if present.

geoalchemy2.admin.dialects.sqlite.get_col_dim(col)[source]

Get dimension of the column type.

geoalchemy2.admin.dialects.sqlite.get_spatialite_version(bind)[source]

Get the version of the currently loaded Spatialite extension.

geoalchemy2.admin.dialects.sqlite.init_spatialite(dbapi_conn, *args, transaction: bool = False, init_mode: str | None = None, journal_mode: str | None = None)[source]

Initialize internal SpatiaLite tables.

Parameters:
  • dbapi_conn – The DBAPI connection.

  • init_mode

    Can be None to load all EPSG SRIDs, ‘WGS84’ to load only the ones related to WGS84 or ‘EMPTY’ to not load any EPSG SRID.

    Note

    It is possible to load other EPSG SRIDs afterwards using InsertEpsgSrid(srid).

  • transaction – If set to True the whole operation will be handled as a single Transaction (faster). The default value is False (slower, but safer).

  • journal_mode

    Change the journal mode to the given value. This can make the table creation much faster. The possible values are the following: [‘DELETE’, ‘TRUNCATE’, ‘PERSIST’, ‘MEMORY’, ‘WAL’, ‘OFF’]. See https://www.sqlite.org/pragma.html#pragma_journal_mode for more details.

    Warning

    Some values, like ‘MEMORY’ or ‘OFF’, can lead to corrupted databases if the process is interrupted during initialization.

    Note

    The original value is restored after the initialization.

Note

When using this function as a listener it is not possible to pass the transaction, init_mode or journal_mode arguments directly. To do this you can either create another function that calls init_spatialite (or geoalchemy2.admin.dialects.sqlite.load_spatialite() if you also want to load the SpatiaLite drivers) with an hard-coded init_mode or just use a lambda:

>>> sqlalchemy.event.listen(
...     engine,
...     "connect",
...     lambda x, y: init_spatialite(
...         x,
...         y,
...         transaction=True,
...         init_mode="EMPTY",
...         journal_mode="OFF",
...     )
... )
geoalchemy2.admin.dialects.sqlite.load_spatialite(*args, **kwargs)[source]

Load SpatiaLite extension in SQLite DB and initialize internal tables.

See geoalchemy2.admin.dialects.sqlite.load_spatialite_driver() and geoalchemy2.admin.dialects.sqlite.init_spatialite() functions for details about arguments.

geoalchemy2.admin.dialects.sqlite.load_spatialite_driver(dbapi_conn, *args)[source]

Load SpatiaLite extension in SQLite connection.

Warning

The path to the SpatiaLite module should be set in the SPATIALITE_LIBRARY_PATH environment variable.

Parameters:

dbapi_conn – The DBAPI connection.

geoalchemy2.admin.dialects.sqlite.reflect_geometry_column(inspector, table, column_info)[source]

Reflect a column of type Geometry with SQLite dialect.

geoalchemy2.admin.dialects.sqlite.register_sqlite_mapping(mapping)[source]

Register compilation mappings for the given functions.

Parameters:

mapping

Should have the following form:

{
    "function_name_1": "sqlite_function_name_1",
    "function_name_2": "sqlite_function_name_2",
    ...
}

GeoPackage-specific objects

This module defines specific functions for GeoPackage dialect.

See GeoPackage specifications here: http://www.geopackage.org/spec/

class geoalchemy2.admin.dialects.geopackage.GeoPackageDialect(native_datetime=False, json_serializer=None, json_deserializer=None, _json_serializer=None, _json_deserializer=None, **kwargs)[source]

Bases: SQLiteDialect_pysqlite

Define a specific dialect for GeoPackage.

driver: str = 'gpkg'

identifying name for the dialect’s DBAPI

name: str = 'geopackage'

identifying name for the dialect from a DBAPI-neutral point of view (i.e. ‘sqlite’)

supports_statement_cache: bool = True

Enable caching for GeoPackage dialect.

geoalchemy2.admin.dialects.geopackage._setup_dummy_type(table, gis_cols)[source]

Setup dummy type for new Geometry columns so they can be updated later.

geoalchemy2.admin.dialects.geopackage.after_create(table, bind, **kw)[source]

Handle spatial indexes during the after_create event.

geoalchemy2.admin.dialects.geopackage.after_drop(table, bind, **kw)[source]

Handle spatial indexes during the after_drop event.

geoalchemy2.admin.dialects.geopackage.before_create(table, bind, **kw)[source]

Handle spatial indexes during the before_create event.

geoalchemy2.admin.dialects.geopackage.before_drop(table, bind, **kw)[source]

Handle spatial indexes during the before_drop event.

geoalchemy2.admin.dialects.geopackage.create_spatial_index(bind, table, col)[source]

Create spatial index on the given column.

geoalchemy2.admin.dialects.geopackage.create_spatial_ref_sys_view(bind)[source]

Create the spatial_ref_sys view from the gpkg_spatial_ref_sys table.

Note

This is usually only needed to use the ST_Transform function on GeoPackage data because this function, when used with SpatiaLite, requires the spatial_ref_sys table.

geoalchemy2.admin.dialects.geopackage.disable_spatial_index(bind, table, col)[source]

Disable spatial indexes if present.

geoalchemy2.admin.dialects.geopackage.init_geopackage(dbapi_conn, *args)[source]

Initialize GeoPackage tables.

Parameters:

dbapi_conn – The DBAPI connection.

Warning

No EPSG SRID is loaded in the gpkg_spatial_ref_sys table after initialization but it is possible to load other EPSG SRIDs afterwards using the gpkgInsertEpsgSRID(srid). Nevertheless, SRIDs of newly created tables are automatically added.

geoalchemy2.admin.dialects.geopackage.load_geopackage_driver(dbapi_conn, *args)[source]

Load SpatiaLite extension in GeoPackage connection and set VirtualGpkg and Amphibious modes.

Warning

The path to the SpatiaLite module should be set in the SPATIALITE_LIBRARY_PATH environment variable.

Parameters:

dbapi_conn – The DBAPI connection.

geoalchemy2.admin.dialects.geopackage.load_spatialite_gpkg(*args, **kwargs)[source]

Load SpatiaLite extension in GeoPackage and initialize internal tables.

See geoalchemy2.admin.dialects.geopackage.load_geopackage_driver() and geoalchemy2.admin.dialects.geopackage.init_geopackage() functions for details about arguments.

geoalchemy2.admin.dialects.geopackage.reflect_geometry_column(inspector, table, column_info)[source]

Reflect a column of type Geometry with GeoPackage dialect.

geoalchemy2.admin.dialects.geopackage.register_gpkg_mapping(mapping)[source]

Register compilation mappings for the given functions.

Parameters:

mapping

Should have the following form:

{
    "function_name_1": "gpkg_function_name_1",
    "function_name_2": "gpkg_function_name_2",
    ...
}