Defeat the evil volcano with Rails and RGeo


    Author: Sergey Zinoviev

    Many mobile and web applications include different geographical data, because we encounter maps almost everywhere. So, they are used in various social applications - from geosocial networks like Foursquare to travel search services like BlaBlaCar. Google, Microsoft, Yahoo and many other IT giants provide their API for simple operations: displaying maps, markers on these maps and performing the most popular calculations. The popular Ruby on Rails rapid development framework is often used to develop geographic information systems (GIS) and web applications using map data. With a simple example, I will demonstrate what problems can be expected when using spatial data and how to avoid them in a Ruby on Rails application.

    How not to meet an evil volcano



    So, we are developing an application, and at some point it is necessary to add the function of finding the shortest direct distance between two geographical points. It would seem nothing complicated? No matter how! In his blog, in a series of entries under the GeoRails tag , Daniel Azuma gives an example where the inability to work with spatial data can lead to tragedy.

    Daniel suggests us consider the following hypothetical situation. Imagine a plane that needs to make a flight from San Francisco to Athens during the next eruption of Togo most Volcano, which is impossible to pronounce in Iceland. The task of the pilot is to plan the route so as to fly at a safe distance from the volcano. He also needs to know the length of the route in order to refuel the aircraft with enough fuel. In order to correctly calculate everything, the aircraft commander makes a request to the air traffic control center.

    A center employee recently developed the latest web-based flight planning application on Ruby on Rails. Upon receiving the request, he decides to test the application for the first time in action: he enters the coordinates of the start and end points of the journey and draws a direct line between them on Google Maps, right along the 38th parallel of north latitude:



    All is well, the volcano is not in danger of flying! Then the center employee measures the distance between the two points (so that the crew can calculate the right amount of fuel), sends all the necessary data to the crew and wishes the pilot a good flight. A few hours later, the flight control center receives an SOS signal from this aircraft, which flew straight into the volcanic eruption zone:



    As you might have guessed, this was due to the fact that the distortion of the map projection was not taken into account. The spherical shape of our planet cannot be represented in the plane without distortion. Therefore, it turned out that the actual shortest route in a straight line between San Francisco and Athens runs directly over Iceland, where a volcano erupts. A direct route on a flat map is actually not a direct route.

    In general, each cartographic projection has its advantages and disadvantages; the choice of the desired projection depends on the territory captured by the map, and on the goals of the developer. The main thing is not to lay routes and not to calculate distances using the linear orthogonal coordinate system with latitudes and longitudes that is familiar to us from school.

    Simple solution: RGeo



    As a Ruby on Rails developer, I’m very glad that you don’t have to implement all the business logic of coordinate recalculation, distance calculations and projection work from scratch every time. We have tools for working with spatial data, including add-ons for the most common SQL and NoSQL databases - using them to build mapping applications is much easier.

    One of the largest and most functional libraries of this kind is PostGIS for PostgreSQL. Daniel Azuma also developed the RGeo gemand adapters for various DBMSs for ActiveRecord to make development on Ruby on Rails using spatial data easier and faster. Thus, as a developer, I have a choice between hundreds of projections. At the same time, I do not need to memorize all the necessary formulas in order to switch from the geographical representation of the data to the geometric.

    This is a very good work tool. When I was developing a web-application for searching for travel companions, “Gimme”, gems and services helped to significantly reduce development time. I could not imagine how easily and quickly you can work with spatial data.

    Also popular now: