Skip to content

zverok/worldize

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Worldize

Worldize is a very simple and naive gem to make world map, with countries painted according to some values (see also choropleth map).

Demonstration

Just monochrome countries

Code:

worldize = Worldize::Countries.new
img = worldize.draw # Magick::Image of RMagick
img.write('blank.png')

Picture: Monochrome countries

You can set some options (see Usage for details):

worldize = Worldize::Countries.new
img = worldize.draw(ocean: '#3A3C3C', land: 'black', border: 'yellow')
img.write('night.png')

Styles

Some countries highlighted

Code:

Worldize::Countries.new.
  draw_highlighted('Ukraine', 'Argentina', 'Portugal', 'India', 'Iceland').
  write('highlighted.png')

Picture: Highlighted countries

Countries painted in custom colors

Code:

Worldize::Countries.new.
  draw(
    'Ukraine' => '#FCF83D',
    'Argentina' => '#FE7ECD',
    'Portugal' => '#FD1F30',
    'India' => '#108400',
    'Iceland' => 'white'
  ).
  write('colors.png')

Picture: Countries in different colors

Countries painted in gradient according to value

worldize = Worldize::Countries.new

# create hash like {country name => value}
values = {
  'Argentina' => 100,
  'Bolivia' => 50,
  'Chile' => 180
  #...
}

worldize.
  draw_gradient(
    '#D4F6C8', # gradient from this color
    '#247209', # ...to that color
    values     # ...according to value
    ).
  write('gradient.png')

Picture: Countries gradient

NB: on this picture, countries associated with values according to their position in sorted countries list, just like this:

values = worldize.country_names.sort.each_with_index.to_a.to_h
# => {"Afghanistan"=>0, "Albania"=>1, "Algeria"=>2, "Angola"=>3, "Antarctica"=>4, "Argentina"=>5, "Armenia"=>6, ...

Installation

It's gem, named worldize. Do your usual [sudo] gem install worldize or adding gem 'worldize' to Gemfile routine.

Usage

From code

Create an object: w = Worldize::Countries.new.

Generic draw

Synopsys: #draw('Country1' => 'color1', 'Country2' => 'color2', ... , option: value)

Country can be either full name or ISO 3-letter code. For list of known names/codes there are service functions #country_names and #country_codes.

color is any color value RMagick can understand (for ex., hex-codes or common color names).

Options awailable:

  • width of resulting image (default 1024 pixels, height will be calculated automatically);
  • land—default color of land (for countries not specified in list);
  • ocean—color of ocean;
  • border—color of country borders.

Both countries and options can be omitted completely (resulting in all countries being drawn in default color).

Select several countries with one color

Synopsys: #draw_selected('Country1', 'Country2', ... , option: value).

Options are the same as for #draw plus :selected background color (reasonable default exists, so can be omitted).

Paint countries proportionally to some measurement

Synopsys: #draw_gradient('from_color', 'to_color', 'Country1' => value1, 'Country2' => value2, ... option: value)

Values should be numeric and colors will be scaled to gradient between from_color and to_color.

From command line

Use worldize --help for details.

Highlight countries:

worldize -o highlighted.png \
  --highlight-countries Ukraine,Argentina,Portugal,India,Iceland 

Colors for countries:

worldize -o color.png \
  --paint-countries "Ukraine:#FCF83D,Argentina:#FE7ECD,Portugal:#FD1F30,India:#108400,Iceland:white" 

or from CSV file

worldize -o color.png \
  --paint-countries country_colors.csv --csv-columns 0,1 

means firs and second columns contain country name and color. Or from CSV with headers:

worldize -o color.png \
  --paint-countries country_colors.csv
  --csv-headers --csv-columns Country,Color 

Color-coded statisitcs

worldize  -o gradient.png \
  --from-color '#D4F6C8' --to-color '#247209' \
  --grad-countries "Argentina:100,Bolivia:50,Chile:180"

or from CSV file, like above:

worldize  -o gradient.png \
  --from-color '#D4F6C8' --to-color '#247209' \
  --grad-countries country_stats.csv --csv-headers --columns "Country,Population 2015"

How this was done

  • Country borders are taken from geojson (sourced from Natural Earth by OpenData license);
  • Web Mercator map projection calculated according to formulae;
  • Result is cropped to exclude polar areas (which has nothing interesting in any case!);
  • RMagick for drawing, awesome color gem for gradients calculation.

TODO

(or not TODO, depends on whether somebody needs this)

  • Options to draw legend and other text labels;
  • Use of some open-licensed tiles/picture of the world as background image.

Authors

Victor Shepelev

License

MIT.