• Stars
    star
    258
  • Rank 158,189 (Top 4 %)
  • Language
    Ruby
  • Created almost 9 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple coloured countries drawing

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.

More Repositories

1

wikipedia_ql

Query language for efficient data extraction from Wikipedia
Python
357
star
2

time_math2

Small library for operations with time steps (like "next day", "floor to hour" and so on)
Ruby
278
star
3

spylls

Pure Python spell-checker, (almost) full port of Hunspell
Python
265
star
4

time_calc

Simple time arithmetics in a modern, readable, idiomatic, no-"magic" Ruby.
Ruby
213
star
5

hm

Idiomatic Ruby hash transformations
Ruby
129
star
6

geo_coord

Simple yet useful Geo Coordinates class for Ruby
Ruby
122
star
7

any_good

Is this gem any good?
Ruby
119
star
8

wheretz

Fast and precise time zone by geo coordinates lookup
Ruby
99
star
9

good-value-object

Ruby Value Object conventions
Ruby
93
star
10

magic_cloud

Simple pretty word cloud for Ruby
Ruby
85
star
11

the_schema_is

ActiveRecord schema annotations done right
Ruby
70
star
12

saharspec

RSpec sugar to DRY your specs
Ruby
67
star
13

yard-junk

Get rid of the junk in your YARD docs
Ruby
67
star
14

whatthegem

Ruby gem information, stats and usage for your terminal
Ruby
62
star
15

sho

Experimental post-framework view library
Ruby
45
star
16

xkcdize

XKCD-like picture distortion in Ruby and RMagick
Ruby
45
star
17

clio

Clio — better Friendfeed backup tool
JavaScript
39
star
18

ruby_as_apl

Conway's game of life in one statement of idiomatic Ruby... ported from APL
Ruby
35
star
19

delegates

delegate :methods, to: :target, extracted from ActiveSupport
Ruby
32
star
20

dokaz

Use your documentation as a specification: parse and evaluate ruby code from markdown
Ruby
32
star
21

rubyseeds

Ruby core extensions repository (not a gem!)
Ruby
28
star
22

lmsa

Let's Make Something Awesome! — project ideas repo for mentees
Ruby
26
star
23

drosterize

Self-replicating images with Ruby & RMagick
Ruby
26
star
24

linkhum

URL auto-linker with reasonable and humane behavior
Ruby
25
star
25

object_enumerate

Object#enumerate Ruby core proposal demo. Merged in Ruby 2.7 as Enumerator.produce
Ruby
19
star
26

procme

Fun with proc
Ruby
17
star
27

my-ruby-contributions

Moved to https://zverok.github.io/ruby.html
Ruby
17
star
28

fstrings

Python-alike fstrings (formatting strings) for Ruby
Ruby
14
star
29

grok-shan-shui

Grok {Shan, Shui}*: Advent of understanding the generative art
HTML
14
star
30

did_you

Ruby version-agnostic wrapper for did_you_mean gem
Ruby
10
star
31

idempotent_enumerable

IdempotentEnumerable is like Enumerable but preserves original collection class
Ruby
8
star
32

enumerator_generate

Enumerator#generate Ruby core proposal demo
Ruby
4
star
33

grokability

Grokability -- step after Readability
JavaScript
4
star
34

clio-web

Online version of FrF backup
Ruby
3
star
35

lastic

ElasticSearch DSL which erases all the complexity
Ruby
3
star
36

bloxl

Hi-level Excel-2007 reports DSL
Ruby
3
star
37

confucius

Simple framework-agnostic configuration for any Ruby app
Ruby
2
star
38

zverok.github.io

HTML
2
star
39

sequel_marginalia

Port of 37 signals marginalia for use with Sequel
Ruby
2
star
40

culturecodes

parsers for http://friendfeed.com/culturecodes
Ruby
2
star
41

uberdictionary

HTML/JS client to several En-Ru dictionaries
JavaScript
2
star
42

pattern-matching-prototype

Showcase of possible Ruby core language pattern matching
Ruby
2
star
43

cobb

Cobb is Yet Another Web Scraper, named after Firefly's Jayne Cobb
Ruby
1
star
44

matchish

An exercise for pattern matching in Ruby
Ruby
1
star