Estimate a project
Estimate Project

Tips and tricks how to make your own gorgeous Ruby maps

by | March 17th, 2016

This time we would like to show an example of making world maps with countries painted according to some values. To make it possible we’ll use simple nevertheless very efficient gem Worldize.

^3033BFC10A70588051394E17CB6A296477C7D62E9EFCE9D55D^pimgpsh_fullsize_distr

When it comes to visualizing statistics on your website, heavy numbers won’t be your friend. People aren’t very likely to read an essay, or look at a table of statistics therefore you don’t want to drag your followers into analyzing bunch of data. However, there are always few ways to diversify: infographics, diagrams, charts etc.

But moving on from old school technics, we would like to introduce you our way to make your data appealing, while creating thematic map in Ruby.

In all statistical languages and packages there is an option to build a world map, where each country would be colored, depending on some quantity (e.g. life expectancy, panda’s population per capita, flat prices etc.). You could certainly make a map of basically anything you want.

Screen-Shot-2015-02-15-at-10.35.54-PM

Gini-coefficient

Such maps are also called choropleth maps.

Let’s ask what Google is saying in such case – and “voila!”:

Result:

result

Process:

  • find data in open access “country name – country polygon coordinates”;
  • learn drawing illustration with such polygons painted in different colours;
  • create simple API around all of it.

GeoData and maps

We used open data access from Natural Earth, and then found its comfortable conversion – in one amazing repository. Comfortable formatting – is GeoJSON, which, compared to other cartographic formats, is easily readable and usable without any additional tools and libraries.

But this is only half the work. Geo coordinates received from such datasets are still needed to be converted in beautiful polygons on surface. We need some projection. Again Google says that the most popular and recognized projection is Web Mercator, used for modern online maps. After some work with formulas from Wikipedia, we receive this Ruby-code:

Useful method rescale that converts number from one diapason to another one, in our library is defined, at that using feature from Ruby 2 – refine:

By the way, it is used not only here, but also for coloring gradient calculations (check below).

Colors, gradients and drawing

First and main choice of every rubist who wants to draw something – is library RMagick. This cover has not very good reputation, non-understandable API, questionable productivity, and, besides, is inclined to memory leakage … – but if you need to draw something quickly, you just do it:

Getting used to RMagick consents, to fulfill the task becomes not that difficult.

There is only the last algorithmic task: to calculate color for every country – gradient from first color to the last one. There is an incredible gem color, using which, the code for choosing the color for each country with its numerical value becomes super simple:

And here it is! Aside from simply making geographical and statistical data from your website easy to read, there are lots of ways to let your creativity flow and build various interactive maps. E.g. you could create city map of your retail coffee stands, country maps of dairy production households, maps of your servers loading in real time, well, actually everything you’re up to.

Keep Developing with Gera-IT!RB_4

Source: mkdev

Tags: , ,