Types

This module defines the geoalchemy2.types.Geometry, geoalchemy2.types.Geography, and geoalchemy2.types.Raster classes, that are used when defining geometry, geography and raster columns/properties in models.

Reference

class geoalchemy2.types.CompositeType[source]

Bases: sqlalchemy.sql.type_api.UserDefinedType

A wrapper for geoalchemy2.elements.CompositeElement, that can be used as the return type in PostgreSQL functions that return composite values.

This is used as the base class of geoalchemy2.types.GeometryDump.

class comparator_factory(expr)[source]

Bases: sqlalchemy.sql.type_api.Comparator

typemap = {}

Dictionary used for defining the content types and their corresponding keys. Set in subclasses.

class geoalchemy2.types.Geography(geometry_type='GEOMETRY', srid=-1, dimension=2, spatial_index=True, management=False, use_typmod=None)[source]

Bases: geoalchemy2.types._GISType

The Geography type.

Creating a geography column is done like this:

Column(Geography(geometry_type='POINT', srid=4326))

See geoalchemy2.types._GISType for the list of arguments that can be passed to the constructor.

as_binary = 'ST_AsBinary'

The “as binary” function to use. Used by the parent class’ column_expression method.

from_text = 'ST_GeogFromText'

The FromText geography constructor. Used by the parent class’ bind_expression method.

name = 'geography'

Type name used for defining geography columns in CREATE TABLE.

class geoalchemy2.types.Geometry(geometry_type='GEOMETRY', srid=-1, dimension=2, spatial_index=True, management=False, use_typmod=None)[source]

Bases: geoalchemy2.types._GISType

The Geometry type.

Creating a geometry column is done like this:

Column(Geometry(geometry_type='POINT', srid=4326))

See geoalchemy2.types._GISType for the list of arguments that can be passed to the constructor.

as_binary = 'ST_AsEWKB'

The “as binary” function to use. Used by the parent class’ column_expression method.

from_text = 'ST_GeomFromEWKT'

The “from text” geometry constructor. Used by the parent class’ bind_expression method.

name = 'geometry'

Type name used for defining geometry columns in CREATE TABLE.

class geoalchemy2.types.GeometryDump[source]

Bases: geoalchemy2.types.CompositeType

The return type for functions like ST_Dump, consisting of a path and a geom field. You should normally never use this class directly.

typemap = {'geom': <class 'geoalchemy2.types.Geometry'>, 'path': ARRAY(Integer())}

Dictionary defining the contents of a geometry_dump.

class geoalchemy2.types.Raster(spatial_index=True)[source]

Bases: sqlalchemy.sql.type_api.UserDefinedType

The Raster column type.

Creating a raster column is done like this:

Column(Raster)

This class defines the result_processor method, so that raster values received from the database are converted to geoalchemy2.elements.RasterElement objects.

Constructor arguments:

spatial_index

Indicate if a spatial index should be created. Default is True.
comparator_factory

alias of geoalchemy2.comparator.BaseComparator

result_processor(dialect, coltype)[source]

Return a conversion function for processing result row values.

Returns a callable which will receive a result row column value as the sole positional argument and will return a value to return to the user.

If processing is not necessary, the method should return None.

Parameters:
  • dialect – Dialect instance in use.
  • coltype – DBAPI coltype argument received in cursor.description.
class geoalchemy2.types._GISType(geometry_type='GEOMETRY', srid=-1, dimension=2, spatial_index=True, management=False, use_typmod=None)[source]

Bases: sqlalchemy.sql.type_api.UserDefinedType

The base class for geoalchemy2.types.Geometry and geoalchemy2.types.Geography.

This class defines bind_expression and column_expression methods that wrap column expressions in ST_GeomFromEWKT, ST_GeogFromText, or ST_AsEWKB calls.

This class also defines result_processor and bind_processor methods. The function returned by result_processor converts WKB values received from the database to geoalchemy2.elements.WKBElement objects. The function returned by bind_processor converts geoalchemy2.elements.WKTElement objects to EWKT strings.

Constructor arguments:

geometry_type

The geometry type.

Possible values are:

  • "GEOMETRY",
  • "POINT",
  • "LINESTRING",
  • "POLYGON",
  • "MULTIPOINT",
  • "MULTILINESTRING",
  • "MULTIPOLYGON",
  • "GEOMETRYCOLLECTION"
  • "CURVE",
  • None.

The latter is actually not supported with geoalchemy2.types.Geography.

When set to None then no “geometry type” constraints will be attached to the geometry type declaration. Using None here is not compatible with setting management to True.

Default is "GEOMETRY".

srid

The SRID for this column. E.g. 4326. Default is -1.

dimension

The dimension of the geometry. Default is 2.

spatial_index

Indicate if a spatial index should be created. Default is True.

management

Indicate if the AddGeometryColumn and DropGeometryColumn managements functions should be called when adding and dropping the geometry column. Should be set to True for PostGIS 1.x. Default is False. Note that this option has no effect for geoalchemy2.types.Geography.

use_typmod

By default PostgreSQL type modifiers are used to create the geometry column. To use check constraints instead set use_typmod to False. By default this option is not included in the call to AddGeometryColumn. Note that this option is only taken into account if management is set to True and is only available for PostGIS 2.x.
as_binary = None

The name of the “as binary” function for this type. Set in subclasses.

bind_expression(bindvalue)[source]

“Given a bind value (i.e. a BindParameter instance), return a SQL expression in its place.

This is typically a SQL function that wraps the existing bound parameter within the statement. It is used for special data types that require literals being wrapped in some special database function in order to coerce an application-level value into a database-specific format. It is the SQL analogue of the TypeEngine.bind_processor() method.

The method is evaluated at statement compile time, as opposed to statement construction time.

Note that this method, when implemented, should always return the exact same structure, without any conditional logic, as it may be used in an executemany() call against an arbitrary number of bound parameter sets.

See also:

types_sql_value_processing

bind_processor(dialect)[source]

Return a conversion function for processing bind values.

Returns a callable which will receive a bind parameter value as the sole positional argument and will return a value to send to the DB-API.

If processing is not necessary, the method should return None.

Parameters:dialect – Dialect instance in use.
column_expression(col)[source]

Given a SELECT column expression, return a wrapping SQL expression.

This is typically a SQL function that wraps a column expression as rendered in the columns clause of a SELECT statement. It is used for special data types that require columns to be wrapped in some special database function in order to coerce the value before being sent back to the application. It is the SQL analogue of the TypeEngine.result_processor() method.

The method is evaluated at statement compile time, as opposed to statement construction time.

See also:

types_sql_value_processing

comparator_factory

alias of geoalchemy2.comparator.Comparator

from_text = None

The name of “from text” function for this type. Set in subclasses.

name = None

Name used for defining the main geo type (geometry or geography) in CREATE TABLE statements. Set in subclasses.

result_processor(dialect, coltype)[source]

Return a conversion function for processing result row values.

Returns a callable which will receive a result row column value as the sole positional argument and will return a value to return to the user.

If processing is not necessary, the method should return None.

Parameters:
  • dialect – Dialect instance in use.
  • coltype – DBAPI coltype argument received in cursor.description.