
Author: Sergey Zinoviev
Many mobile and web applications include different geographic data, because we are confronted with maps almost everywhere. So, they are used in various social applications - from geosocial networks like Foursquare to services for searching for travel companions 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. A popular framework for the rapid development of Ruby on Rails is often used to develop geographic information systems (GIS) and web applications using map data. Using a simple example, I’ll 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 with an evil volcano
')
So, we are developing an application, and at some point it is necessary to add a search function for the shortest direct distance between two geographic points. It would seem nothing complicated? No matter how wrong! In his blog, in a series of posts under the
GeoRails tag,
Daniel Azuma gives an example when the inability to work with spatial data can lead to tragedy.
Daniel suggests we consider the following
hypothetical situation . Imagine a plane that needs to take a flight from San Francisco to Athens during the next eruption of the TogoSamogogo Volkana, which is not possible 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 fill the plane with enough fuel. To calculate everything correctly, the aircraft commander makes a request to the air traffic control center.
A center employee recently developed the latest flight planning web application for Ruby on Rails. After receiving the request, he decides to test the application for the first time in action: he enters the coordinates of the starting and ending points of the journey and draws a straight line between them on Google Maps, right along the 38th parallel of northern latitude:

All is well, the volcano does not threaten the flight! Then the center employee measures the distance between 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 mission control center receives an SOS signal from this plane, which flew straight into the volcanic eruption zone:

As you probably already guessed, this was due to the fact that the distortions of the cartographic projection were not taken into account. The spherical shape of our planet can not be represented in any way in the plane without distortion. Therefore, it turned out that the actual shortest route in a straight line between San Francisco and Athens passes right above Iceland, where a volcano erupts. A straight line on a flat map route is actually not straight.
In general, each map projection has its own 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 create routes and not to count distances using the linear orthogonal coordinate system with latitudes and longitudes that we are used to from the school.
Simple solution: RGeo
As a Ruby on Rails developer, I am very pleased that all the business logic of recalculating coordinates, calculating distances and working with projections does not have to be implemented from scratch every time. We have tools for working with spatial data, including add-ons for the most common SQL and NoSQL databases - with their help, building cartographic 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 gems and adapters for various DBMS for ActiveRecord to make developing 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 a geographical representation of data to a geometric one.
This is a very good working tool. When I was developing a web-application for searching for fellow travelers “I brought up,” gems and services helped to significantly reduce development time. I could not even imagine how easy and fast it is to work with spatial data.