# Administration

This module defines the functions used for administration tasks.

geoalchemy2.admin.select_dialect(dialect_name)

  Select the dialect from its name.

geoalchemy2.admin.setup_ddl_event_listeners()

  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)

  Get info required for dispatch events.

geoalchemy2.admin.dialects.common._update_table_for_dispatch(table, regular_cols)

  Update the table before dispatch events.

geoalchemy2.admin.dialects.common.check_management(*args)

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

geoalchemy2.admin.dialects.common.compile_bin_literal(wkb_clause)

  Compile a binary literal for WKBElement.

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

  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)

  Handle spatial indexes during the after_create event.

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

  Handle spatial indexes during the after_drop event.

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

  Handle spatial indexes during the before_create event.

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

  Handle spatial indexes during the before_drop event.

geoalchemy2.admin.dialects.postgresql.check_management(column)

  Check if the column should be managed.

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

  Create spatial index on the given column.

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

  Reflect a column of type Geometry with Postgresql dialect.

## MySQL/MariadDB-specific objects

This module defines specific functions for MySQL dialect.

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

  Handle spatial indexes during the after_create event.

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

  Handle spatial indexes during the before_create event.

geoalchemy2.admin.dialects.mysql.before_cursor_execute(conn, cursor, statement, parameters, context, executemany, convert=True)

  Event handler to cast the parameters properly.

  Parameters:

    convert (bool) – Trigger the conversion.

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

  Reflect a column of type Geometry with Postgresql dialect.

geoalchemy2.admin.dialects.mysql.register_mysql_mapping(mapping)

  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",
            ...
        }

This module defines specific functions for MariaDB dialect.

geoalchemy2.admin.dialects.mariadb.before_cursor_execute(conn, cursor, statement, parameters, context, executemany, convert=True)

  Event handler to cast the parameters properly.

  Parameters:

    convert (bool) – Trigger the conversion.

geoalchemy2.admin.dialects.mariadb.register_mariadb_mapping(mapping)

  Register compilation mappings for the given functions.

  Parameters:

    mapping –

    Should have the following form:

        {
            "function_name_1": "mariadb_function_name_1",
            "function_name_2": "mariadb_function_name_2",
            ...
        }

## SQLite-specific objects

This module defines specific functions for SQLite dialect.

geoalchemy2.admin.dialects.sqlite._setup_dummy_type(table, gis_cols)

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

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

  Handle spatial indexes during the after_create event.

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

  Handle spatial indexes during the after_drop event.

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

  Handle spatial indexes during the before_create event.

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

  Handle spatial indexes during the before_drop event.

geoalchemy2.admin.dialects.sqlite.connect(dbapi_conn, *args, **kwargs)

  Even handler to load spatial extension when a new connection is created.

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

  Create spatial index on the given column.

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

  Disable spatial indexes if present.

geoalchemy2.admin.dialects.sqlite.get_col_dim(col)

  Get dimension of the column type.

geoalchemy2.admin.dialects.sqlite.get_spatialite_version(bind)

  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)

  Initialize internal SpatiaLite tables.

  Parameters:

    - dbapi_conn – The DBAPI connection.

    - *args – Additional arguments (unused).

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

    - 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).

    - 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(dbapi_conn, *args, **kwargs)

  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)

  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.

    - *args – Additional arguments (unused).

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

  Reflect a column of type Geometry with SQLite dialect.

geoalchemy2.admin.dialects.sqlite.register_sqlite_mapping(mapping)

  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: bool = False, json_serializer: Callable[[...], Any] | None = None, json_deserializer: Callable[[...], Any] | None = None, _json_serializer: Callable[[...], Any] | None = None, _json_deserializer: Callable[[...], Any] | None = None, **kwargs: Any)

  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)

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

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

  Handle spatial indexes during the after_create event.

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

  Handle spatial indexes during the after_drop event.

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

  Handle spatial indexes during the before_create event.

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

  Handle spatial indexes during the before_drop event.

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

  Create spatial index on the given column.

geoalchemy2.admin.dialects.geopackage.create_spatial_ref_sys_view(bind)

  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)

  Disable spatial indexes if present.

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

  Initialize GeoPackage tables.

  Parameters:

    - dbapi_conn – The DBAPI connection.

    - *args – Additional arguments (unused).

  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)

  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.

    - *args – Additional arguments passed to the underlying loader.

geoalchemy2.admin.dialects.geopackage.load_spatialite_gpkg(dbapi_conn, *args, **kwargs)

  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)

  Reflect a column of type Geometry with GeoPackage dialect.

geoalchemy2.admin.dialects.geopackage.register_gpkg_mapping(mapping)

  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",
            ...
        }