Geospatial Data with Rails in GIS and Mapping Apps

Lots of mobile and web applications include different geographical data – we can see maps almost everywhere. It’s very handy to use them in variant social area of everyday life – from check-in services like Foursquare to driving trackers like Carpooling or MitfahrZentrale. Giants like Google, Microsoft, Yahoo, and many others, provide useful APIs for simple operations, like drawing maps and objects on them or performing the most common calculations. As a popular framework, Ruby on Rails often is used for GIS and mapping app development. With an example from real life I’d like to show how the adding some features to your Rails app can change things and prevent troubles.

Facing an EvilVolcano: a Technicians Mistake

When it comes to developing a service that provides something extra the fun starts, for example, when searching for the shortest path between two geographical objects. Daniel Azuma in his series of blog posts entitled GeoRails describes a situation when a lack of knowledge could have resulted in tragedy.

Daniel Azuma offers us to consider a hypothetical situation. Imagine that one day a plane took off from San Francisco, California on a flight to Athens, Greece. At the same time one EvilVolcano (Whose Name Cannot Be Spoken) in Iceland was erupting. The pilot’s task was to plan the flight direction and fuel amount to fly by the volcano at a safe distance. So the captain sent a Twitter message to air traffic control to ensure flight safety.

Meanwhile, the air traffic technician had recently developed a brand new flight planning web application based on Ruby on Rails. Having received the message he decided to try the service out. He looked up the geographical coordinates of both start and end points, plotted a straight line between them using Google Maps through the 38-degree latitude line (pic. 1). Then he measured the distance, sent the data to the crew and wished the pilot a pleasant journey. But a few hours later flight command center received a SOS message from the plane that flew right into the erupting EvilVolcano (pic. 2).

A quick-witted reader will have already realized the difference between the hypothetical and the actual flight path was in different projections. Our planet has a shape close to that of a sphere, and it couldn’t be represented in a 2D mode without any distortions. In general, a straight line on a map is not in fact straight. In this case the real flight path placed the plane right above EvilVolcano, and the misunderstanding between the captain and the technician was based on different map projections.

Each projection has its own pros and cons, depending on the area under consideration and the developer’s goals. And the worst thing –to do is to work in a latitude-longitude coordinate’s model, while calculating distances and paths.

Easy to Create with RGeo

As a Ruby on Rails developer, I’m glad that Ruby developers have created special tools for working with geospatial data, including plugins for the most common SQL and NoSQL databases, which make my work as an apps builder easier. One of the largest and most functional libraries is PostGIS, a package for PostgreSQL. Daniel Azuma implemented RGeo gem and database connectors for ActiveRecord to make development using Ruby on Rails framework and geospatial data simpler and faster. This way I got the choice between hundreds of projections. At the same time I don’t need to memorize all the necessary formulas in order to switch from a geographical data representation to geometrical one.

It’s a really nice tool to work with. I used these gems and services while developing “Podvezu” (“I’ll drive you”), a driving tracker web-application, and the software decreased development time dramatically. I couldn’t even imagine it would be so fast and easy to work with geospatial data.