Class: Worldize::Countries
- Inherits:
-
Object
- Object
- Worldize::Countries
- 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.('../../data/countries.geojson', __FILE__)
Instance Method Summary collapse
- #country_codes ⇒ Object
- #country_names ⇒ Object
-
#draw(countries_and_options = {}) ⇒ Object
NB: syntax draw(countries = {}, **options) causes segfault in Ruby 2.2.0.
- #draw_gradient(from_color, to_color, value_by_country, **options) ⇒ Object
- #draw_highlighted(*countries, **options) ⇒ Object
-
#initialize ⇒ Countries
constructor
A new instance of Countries.
- #inspect ⇒ Object
Methods included from WebMercator
Constructor Details
Instance Method Details
#country_codes ⇒ Object
35 36 37 |
# File 'lib/worldize.rb', line 35 def country_codes @countries.map{|c| c.properties.iso_a3} end |
#country_names ⇒ Object
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( = {}) = DEFAULT_OPTIONS.merge() width = .fetch(:width) img = Image.new(width, width){ self.background_color = .fetch(:ocean) } gc = Magick::Draw.new. stroke(.fetch(:border)).stroke_width(1). fill(.fetch(:land)) @countries.each do |country| bg = [country.properties.name] || [country.properties.iso_a3] || .fetch(:land) draw_country(gc, country, width, bg) gc.fill(.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, **) 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()) end |
#draw_highlighted(*countries, **options) ⇒ Object
73 74 75 76 77 |
# File 'lib/worldize.rb', line 73 def draw_highlighted(*countries, **) highlight_color = .fetch(:highlight, DEFAULT_OPTIONS[:highlight]) draw(countries.map{|c| [c, highlight_color]}.to_h.merge()) end |
#inspect ⇒ Object
93 94 95 |
# File 'lib/worldize.rb', line 93 def inspect "#<#{self.class}>" end |