In the company I work for, we started the year full of ideas to improve the products we offer. Our mantra is to sell an insurance “like you”, but to do this we have to know better our users, understand their needs to be a little more predictive.

Today, to be perfectly honest, we are offering a one size fits all solution. It’s not what we want. If the idea is to change the common sense that insurance is something boring, that thing we buy and expect to never use, we have to turn it into something valuable that meets the needs of people on a daily basis and not only when something goes wrong. That’s where Geolocation comes in.

Geolocation is the detection of the physical location of people, which enables us to better understand what happens in the environments around their houses, neighborhood or even their city. If we understand the problems that people have to deal with we can offer more appropriate services that better meet their needs.

To build our micro service we chose the PostGis extension. This plugin adds to Postgres spatial data types and resources to help us make complex calculations, such as distance and inclusion of points in areas, for example.

To install it on Mac:

brew install postgis

If you suffer from version incompatibility issues, I suggest you follow the steps in this blog.

We’ve also included the Geocoder gem, which, combined with Rails Rgeo, gives us all the features we need.

The Coordinate System

Before we get our hands dirty, there are some concepts that we need to understand. What comes to mind when we think of Geometry? I bet it’s something like this:

Image for post
Image for post

Triangles, circles, trapezoids, sine, cosine, Pythagoras … It’s the same Geometry we learned in school.

Now, what comes to mind when we think of Geography?

Image for post
Image for post

Now we are talking about our planet, that rocky sphere with flat poles that floats in space around the sun.

When we work with coordinates we have to choose between the two existing coordinate systems: the geometric or the geographic system. The words are so similar that it often causes confusion, but the difference between them lies in the fundamentals of what we think of when we talk about each of them.

Let’s start with the Geographic system.

Image for post
Image for post

This is the system that encompasses the notions of latitude and longitude. Latitude is the angle between the equator and the surface of reference, which, in practice, means the horizontal lines we are seeing in the image above. The longitude is the angle between the Greenwich meridian and the surface of reference, which generates the vertical lines.

It is important to understand that the geographic coordinates are on a curved surface, it is impossible to accurately project them on a flat surface like the computer. Mapped objects at points of latitude and longitude are necessarily curved like the Earth’s surface and the distances measured between them must also respect this curvature.

Image for post
Image for post

To calculate the distance between two points inside the square on the map on the left is easy. But what about in the globe on the right? We are talking about the same area, but the geometric figure went from a square to a trapezoid and the calculations are not only more complex but also a lot more costly.

This is where the geometric system comes in. It works with projections, by flattening the Earth on a flat surface, which can distort the design of some areas. The advantage is that it allows us to project them on a map and thereby calculate distances, areas or any other measurements using the simple calculations of Geometry we have learned in school. It is much easier for the computer to do these calculations.

Have you ever stopped to think that the image of the countries we usually see in the map is distorted? The further away from the equator, the more “stretched” the image is. Look at the difference between the size we think Russia has compared to its actual size, when in the vicinity of the equator line:

Image for post
Image for post

The only problem with the projected system is if we have to deal with points on the edge of the map, a problem that does not exist in the geographic system because the real world does not have edges (thank you, Pythagoras).

Image for post
Image for post

Because the Earth was flattened, the line can not go around from behind the map, so the distance between the two points is much greater than it should be. This problem also happens if you cross the poles or even surround them. The same line as the image above is very different in the geographical system:

Image for post
Image for post

Which system should we use?

The geographic model is more accurate. If you need to perform calculations that cross the globe and deal with very distant points, it is with the geographic coordinates system that you will want to work.

However, most of the applications we know don’t need this. Projected coordinates usually give better results to the problems we are currently trying to solve.

We are limited to the storage and processing capacity of the databases. The geographic system is more realistic and better to represent the real world, but what we do most of the time has to do with the calculations, not with 3D projections. If we’re going to plot it somewhere, we will probably do it on Google Maps, which is flattened and perfect to deal with projections.

Performance is important. Calculations on curved surfaces are much more complex and take time. If it is not hard enough to make calculations in a sphere like the Earth, include in the mix the fact that the flattening of the poles is not linear.

To delve into the Geolocation world

For those who have just crossed the threshold of the world of Geolocation, I highly recommend taking a look at the work of Daniel Azuma, Software Engineer at Google.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store