Method: ActiveRecord::ConnectionAdapters::PostgreSQLTableDefinition#spatial

Defined in:
lib/active_record/postgresql_extensions/geometry.rb

#spatial(column_name, opts = {}) ⇒ Object Also known as: geometry

This is a special spatial type for the PostGIS extension's data types. It is used in a table definition to define a spatial column.

Depending on the version of PostGIS being used, we'll try to create geometry columns in a post-2.0-ish, typmod-based way or a pre-2.0-ish AddGeometryColumn-based way. We can also add CHECK constraints and create a GiST index on the column all in one go.

In versions of PostGIS prior to 2.0, geometry columns are created using the AddGeometryColumn and will created with CHECK constraints where appropriate and entries to the geometry_columns will be updated accordingly.

In versions of PostGIS after 2.0, geometry columns are creating using typmod specifiers. CHECK constraints can still be created, but their creation must be forced using the :force_constraints option.

The geometry and geography methods are shortcuts to calling the spatial method with the :spatial_column_type option set accordingly.

Options

  • :spatial_column_type - the column type. This value can be one of :geometry or :geography. This value doesn't refer to the spatial type used by the column, but rather by the actual column type itself.
  • :geometry_type - set the geometry type. The actual data type is either "geometry" or "geography"; this option refers to the spatial type being used, i.e. "POINT", "POLYGON", ""
  • :add_constraints - automatically creates the CHECK constraints used to enforce ndims, srid and geometry type. The default is true.
  • :force_constraints - forces the creation of CHECK constraints in versions of PostGIS post-2.0.
  • :add_geometry_columns_entry - automatically adds an entry to the geometry_columns table. We will try to delete any existing match in geometry_columns before inserting. The default is true. This value is ignored in versions of PostGIS post-2.0.
  • :create_gist_index - automatically creates a GiST index for the new geometry column. This option accepts either a true/false expression or a String. If the value is a String, we'll use it as the index name. The default is true.
  • :ndims - the number of dimensions to allow in the geometry. This value is either 2 or 3 by default depending on the value of the :geometry_type option. If the :geometry_type ends in an "m" (for "measured geometries" the default is 3); for everything else, it is 2.
  • :srid - the SRID, a.k.a. the Spatial Reference Identifier. The default depends on the version of PostGIS being used and the spatial column type being used. Refer to the PostGIS docs for the specifics, but generally this means either a value of -1 for versions of PostGIS prior to 2.0 for geometry columns and a value of 0 for versions post-2.0 and for all geography columns.


313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/active_record/postgresql_extensions/geometry.rb', line 313

def spatial(column_name, opts = {})
  column = self[column_name] || PostgreSQLGeometryColumnDefinition.new(base, column_name, opts)

  unless @columns.include?(column)
    @columns << column
  end

  table_constraints.concat(column.table_constraints)
  post_processing.concat(column.geometry_columns_entry(table_name))
  post_processing.concat(column.geometry_column_index(table_name))

  self
end