Class: Worldize::Countries

Inherits:
Object
  • Object
show all
Includes:
Magick, WebMercator
Defined in:
lib/worldize.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  width: 1024,
  ocean: 'white',
  land: '#E0E6FF',
  border: '#0000FF',
  highlight: '#5D7BFB'
}
DATA_PATH =
File.expand_path('../../data/countries.geojson', __FILE__)

Instance Method Summary collapse

Methods included from WebMercator

#lat2y, #lng2x

Constructor Details

#initializeCountries

Returns a new instance of Countries.



26
27
28
29
30
31
32
33
# File 'lib/worldize.rb', line 26

def initialize
  @countries = File.read(DATA_PATH).
    derp{|json| JSON.parse(json)}.
    derp{|hash| Hashie::Mash.new(hash)}.
    features.map{|country|
      parse_country(country)
    }
end

Instance Method Details

#country_codesObject



35
36
37
# File 'lib/worldize.rb', line 35

def country_codes
  @countries.map{|c| c.properties.iso_a3}
end

#country_namesObject



39
40
41
# File 'lib/worldize.rb', line 39

def country_names
  @countries.map{|c| c.properties.name}
end

#draw(countries_and_options = {}) ⇒ Object

NB: syntax draw(countries = {}, **options) causes segfault in Ruby 2.2.0



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/worldize.rb', line 44

def draw(countries_and_options = {})
  options = DEFAULT_OPTIONS.merge(countries_and_options)
  width = options.fetch(:width)
  
  img = Image.new(width, width){
    self.background_color = options.fetch(:ocean)
  }

  gc = Magick::Draw.new.
    stroke(options.fetch(:border)).stroke_width(1).
    fill(options.fetch(:land))

  @countries.each do |country|
    bg = countries_and_options[country.properties.name] ||
          countries_and_options[country.properties.iso_a3] ||
          options.fetch(:land)
    draw_country(gc, country, width, bg)
    gc.fill(options.fetch(:land))
  end

  gc.draw(img)

  # really meaningful lat: -63..83, everything else is, in fact, poles
  ymin = lat2y(84, width)
  ymax = lat2y(-63, width)

  img.crop(0, ymin, width, ymax-ymin)
end

#draw_gradient(from_color, to_color, value_by_country, **options) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/worldize.rb', line 79

def draw_gradient(from_color, to_color, value_by_country, **options)
  min = value_by_country.values.min
  max = value_by_country.values.max
  from = ::Color::RGB.from_html(from_color)
  to   = ::Color::RGB.from_html(to_color)

  values = value_by_country.
    map{|country, value| [country, value.rescale(min..max, 0..100)]}.
    map{|country, value| [country, from.mix_with(to, 100 - value).html]}.
    to_h

  draw(values.merge(options))
end

#draw_highlighted(*countries, **options) ⇒ Object



73
74
75
76
77
# File 'lib/worldize.rb', line 73

def draw_highlighted(*countries, **options)
  highlight_color = options.fetch(:highlight, DEFAULT_OPTIONS[:highlight])
    
  draw(countries.map{|c| [c, highlight_color]}.to_h.merge(options))
end

#inspectObject



93
94
95
# File 'lib/worldize.rb', line 93

def inspect
  "#<#{self.class}>"
end