ActiveRecord connection adapter for PostGIS, based on postgresql and rgeo
ActiveRecord connection adapter for PostGIS, based on postgresql and rgeo
The activerecord-postgis-adapter provides access to features of the PostGIS geospatial database from ActiveRecord. It extends the standard postgresql adapter to provide support for the spatial data types and features added by the PostGIS extension. It uses the RGeo library to represent spatial data in Ruby.
The adapter provides three basic capabilities:
First, it provides spatial migrations. It extends the ActiveRecord migration syntax to support creating spatially-typed columns and spatial indexes. You can control the various PostGIS-provided attributes such as SRID, dimension, and geographic vs geometric math.
Second, it recognizes spatial types and casts them properly to RGeo geometry objects. The adapter can configure these objects automatically based on the SRID and dimension in the database table, or you can tell it to convert the data to a different form. You can also set attribute data using WKT format.
Third, it lets you include simple spatial data in queries. WKT format data and RGeo objects can be embedded in where clauses.
The adapter requires PostgreSQL 9.0+ and PostGIS 2.4+.
Here are common methods for installing PostGIS, but more detailed methods can be found on the installation guide.
brew install postgissudo apt-get install postgis postgresql-16-postgis-3PostGIS is likely available as an optional package via your Postgresql installer. If not, refer to the installation guide.
| Version | ActiveRecord | Ruby | PostGIS | Note |
|---|---|---|---|---|
| 11.1.x | 8.1 | 3.2.0+ | 2.0+ | |
| 11.0.x | 8.0 | 3.2.0+ | 2.0+ | |
| 10.x | 7.2 | 3.1.0+ | 2.0+ | |
| 9.x | 7.1 | 3.0.0+ | 2.0+ | |
| 8.x | 7.0 | 2.7.0+ | 2.0+ | |
| 7.x | 6.1 | 2.5.0+, JRuby | 2.0+ | |
| 6.x | 6.0 | 2.5.0+, JRuby | 2.0+ | |
| 5.x | 5.1, 5.2 | 2.2.2+, JRuby | 2.0+ | |
| 4.x | 5.0 | 2.2.2+, JRuby | 2.0+ | |
| 3.x | 4.2 | 1.9.3+, JRuby | 2.0+ | |
| 2.x | 4.0.0 - 4.1.x | 1.9.3+, JRuby | 2.0+ | Read 2.x README |
| 0.6.x | 3.x | 1.8.7+, JRuby, Rubinius | 1.5+ | Read 0.6.x / 2.x README |
| PostGIS 1 Notes |
The PostgisDatabaseTasks module has been removed which means that the rake tasks to install postgis are no longer available. If using a Rails app, please see Upgrading an Existing Database
When upgrading from version 6.x to a newer major version, you may need to modify your SpatialFactoryStore configuration. Please see this section of the README in rgeo-activerecord for more details (https://github.com/rgeo/rgeo-activerecord#spatial-factories-for-columns).
You must modify your config/database.yml file to use the postgis
adapter. At minimum, you will need to change the adapter field from
postgresql to postgis. Recommended configuration:
development:
username: your_username
adapter: postgis
host: localhost
schema_search_path: publicIf you have installed your PostGIS extension in a schema other than public, which
is the default, add that schema to your schema_search_path:
development:
schema_search_path: public, postgisHere are some other options that are supported:
development:
adapter: postgis
encoding: unicode
postgis_extension: postgis # default is postgis
postgis_schema: public # default is public
schema_search_path: public,postgis
pool: 5
database: my_app_development # your database name
username: my_app_user # the username your app will use to connect
password: my_app_password # the user's password
su_username: my_global_user # a superuser for the database
su_password: my_global_pasword # the superuser's passwordrgeo dependencyThis adapter uses the rgeo gem, which has additional dependencies.
Please see the README documentation for rgeo for more information: https://github.com/rgeo/rgeo
If you have not created your rails app yet start there.
rails new my_app --database=postgresqlAdd the gem to your Gemfile.
gem 'activerecord-postgis-adapter'And tell ActiveRecord to use the adapter by setting the adapter field in config/database.yml
default: &default
adapter: postgisCreate the database if you haven't already.
rake db:createCreate a migration to add the PostGIS extension to your database.
rails generate migration AddPostgisExtensionToDatabaseThe migration should look something like this:
class AddPostgisExtensionToDatabase factory.point(-122, 47)).firstYou can also use WKT:
record3 = MySpatialTable.where(:lonlat => 'POINT(-122 47)').firstNote that these queries use representational equality, meaning they return records where the lonlat value matches the given value exactly. A 0.00001 degree difference would not match, nor would a different representation of the same geometry (like a multipoint with a single element). Equality queries aren't generally all that useful in real world applications. Typically, if you want to perform a spatial query, you'll look for, say, all the points within a given area. For those queries, you'll need to use the standard spatial SQL functions provided by PostGIS.
To perform more advanced spatial queries, you can use the extended Arel interface included in the activerecord-postgis-adapter. The functions accept WKT strings or RGeo features.
point = RGeo::Geos.factory(srid: 0).point(1,1)
buildings = Building.arel_table
containing_buiildings = Building.where(buildings[:geom].st_contains(point))See rgeo-activerecord for more information about advanced spatial queries.
If a spatial column is joined with another model, srid and geographic will not be automatically inferred and they will default to 0 and false, by default. In order to properly infer these options after a join, an attribute must be created on the target table.
class SpatialModel RGeo::Geographic::SphericalPointImpl
p foo.geo_point.srid
# => 4326A spatial database is one that includes a set of data types, functions, tables, and other objects related to geospatial data. When these objects are present in your database, you can use them to store and query spatial objects such as points, lines, and polygons.
PostGIS is an extension for PostgreSQL that provides definitions for the objects you need to add to a database to enable geospatial capabilities.
When you create your Rails database as described above in the section on installation and configuration, activerecord-postgis-adapter automatically invokes PostGIS to add the appropriate definitions to your database. You can determine whether your database includes the correct definitions by attempting to invoke the POSTGIS_VERSION function:
SELECT POSTGIS_VERSION(); # succeeds if PostGIS objects are present.Standard spatial databases also include a table called spatial_ref_sys. This
table includes a set of "spatial reference systems", or coordinate systems---
for example, WGS84 latitude and longitude, or Mercator Projection. Spatial
databases also usually include a table called geometry_columns, which
includes information on each database column that includes geometric data. In
recent versions of PostGIS, geometry_columns is actually not a table but a
view into the system catalogs.
RubyDoc Documentation is available at https://rubydoc.info/gems/activerecord-postgis-adapter
Contributions are welcome. See CONTRIBUTING.md for instructions.
Report issues at https://github.com/rgeo/activerecord-postgis-adapter/issues
Support is also available on the rgeo-users google group at https://groups.google.com/group/rgeo-users
Daniel Azuma authored the PostGIS Adapter and its supporting libraries (including RGeo).
Tee Parham is a former maintainer.
Keith Doggett is a current maintainer.
Ulysse Buonomo is a current maintainer.
Development is supported by:
This adapter implementation owes some debt to the spatial_adapter plugin (https://github.com/fragility/spatial_adapter). Although we made some different design decisions for this adapter, studying the spatial_adapter source gave us a head start on the implementation.
Copyright Daniel Azuma, Tee Parham
https://github.com/rgeo/activerecord-postgis-adapter/blob/master/LICENSE.txt
No open issues yet, or sync has not completed.