Open Source Map Stack, Part 1: The DB
The Project
I'm currently working on a web mapping project for Turing. It is effectively a guidebook for backcountry skiing and ski mountaineering with user-generated content. It relies pretty extensively on mapping these routes, and that has been a good challenge. There are three main pieces of my free and open source map stack:
- Leaflet map
- Ruby on Rails framework
- PostGIS database
The crux of this project is wiring the stack together. I'm going to document some of that process here.
Why These Tools?
I like using open source tools and these are some of the best tools around, period. I have some limited experience working with them, and was interested in learning more.
PostGIS
PostGIS is a spatial library for the popular PostgreSQL database. It's a great choice in general, and an especially good choice for those of us already used to working with Postgres. PostGIS gives us two really nice features for this project: storing geometry and performing spatial operations and queries.
Storing Geometry
PostGIS makes it easy to store geometries in one column of your table. This is distinctly different than storing, say, coördinates in two distinct latitude and longitude or x and y columns. Storing coordinates in individual columns presents one problem in that you must keep track of the spatial reference system (a topic for another post), and a second in that you are limited to point data.
PostGIS overcomes both of these issues. You can set an SRID for your table and then not worry about it, and you can store points as well as lines or polygons (both of which are mde up of multiple points) without changing your geometry column.
Spatial Operations and Queries
PostGIS maintains the typical rails database queries and functionality, but adds a lot of basic spatial operations. These include things like distances between two or more entities as well as the creation of new spatial entities based on additive or subtractive spatial functions.
In Practice
There are a number of tools we can use to get PostGIS working in a Rails app. I have had some luck with RGeo, a geospatial ruby gem which I will talk about in more detail in a later post. Also maintained by the RGeo team is a gem called activerecord-postgis-adapter. This gem does exactly what it soudns like it does - lets you set up your Rails App database as PostGIS.
The instructions are pretty straightforward and include specific changes you'll need to make to config/database/yml
, and in the model.
From there, you can generate and run migrations as you usual, but with the additional column types
geometry
point
line_string
polygon
geometry_collection
multi_point
multi_line_string
multi_polygon
Wrap Up
This is a high-level overview of PostGIS and why you might want to use it in your stack. There are a few resorces listed below that go into more depth or feel free to drop me a line.
- ESRI Help is a great resource for concise descriptions of various spatial operations. You will need to have a good idea about what you are looking for, but it will probably be in there.
- RGeo activerecord-postgis-adapter You're going to want to use this gem if you want to use PostGIS with Rails.
- RGeo This gem provides the spatial functions to Rails.
- GeoRails This is a collection of blog posts from Daniel Azuma, the author of RGeo, and covers a lot of GIS basics along with the specifics of how to best utilize the RGeo suite.