Building Location Based Apps with Heroku PostGIS
Posted over 2 years ago by Craig Kerstiens
Smartphones have changed the world – everyone has a device in their pocket that’s constantly connected to the internet and knows where you are. Combined with the rise of digital mapping it has become commonplace to build applications that use GIS (Geographical Information Systems) to digitally represent our physical reality and our location in it. Storing and manipulating geospatial data has become an essential part of application development. If you are building a mobile app it’s becoming table stakes that you take advantage of location.
Today we’re releasing PostGIS 2.0 into public beta as an extension to Heroku Postgres. Now all Heroku Postgres customers will be able to store and manipulate geospatial data as part of their Postgres database. PostGIS 2.0 capabilities are now available in all production tier plans at no additional charge—allowing you to add powerful location functionality to your application.
PostGIS 2.0 will enable a new class of Heroku applications that leverage location data. Whether you are looking to compute walkability scores to nearby schools, target ads based on GPS locations, or search for apartments by specific neighborhoods PostGIS can help make you build richer functionality into your application more easily.
PostGIS now follows the standard extension format within Heroku Postgres. Installing PostGIS is as simple as
create extension postgis on any new Postgres 9.2 database crane and above. This means that you can continue starting small with your application, grow functionality, then enable PostGIS at any time to begin taking advantage of it.
You can get started with PostGIS 2.0 today by provisioning a database then enabling the extension; or read more about what PostGIS provides.
To provision your database:
heroku addons:add heroku-postgresql:crane
Once provisioned you’ll want to connect to it and enable the extension:
$ heroku pg:psql create extension postgis;
Your geospatial database is now enabled and ready to use.
PostGIS is an extension, adding support for geographic objects and working with them, within PostgreSQL. Similar to PostgreSQL itself, PostGIS is open source. Since Heroku Postgres runs unmodified from the main branch you always use standard technology. The technology is flexible and there is no technology lock-in – you can take data in and out at any time.
PostGIS has grown over several years with a large community behind it now supporting a variety of new operators, specialized types, and a long list of functions for interacting with spatial data.
While SQL and specifically PostgreSQL can perform basic algebra this method quickly hits limitations when it comes to more complex location searching. Understandably there’s value in providing your users with richer functionality such as searching by neighborhood, by radius of proximity, or by routes versus just direct distance. At PyCon 2013 Julia Grace talked about how some developers use various math tricks to compute distances or you can take an easier approach by using PostGIS.
By using PostGIS whether natively or through Rails with ActiveRecord, Django with GeoDjango, Hibernate you can add a variety of rich functionality around location and geographic data very quickly, including:
- High performance point-in-polygon testing, using ST_Intersect.
- Native support for distance and area calculations on the spheroid, using the geography column type.
- Index-accelerated nearest-neighbor (KNN) searching.
- Support for server-side map rendering from a number of open source mapping engines – http://geoserver.org, http://mapserver.org, and http://mapbox.com
Heroku Postgres is increasingly enabling rich use cases – adding services from key/value datatype in hstore, querying across postgres databases with dblink, and now adding rich geospatial functionality. Adding PostGIS within your Postgres database reduces the number of services you need to add to your stack, reducing complexity and allowing you to build in location-based functionality into apps faster.
Get started integrating location into your apps today by provisioning your Heroku Postgres database and exploring the functionality of PostGIS 2.0.