Class: OSM::Exporter

Inherits:
Object
  • Object
show all
Defined in:
lib/osm/exporter.rb

Constant Summary collapse

TILE_SIZE =
256

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tile_urls, zoom) ⇒ Exporter

Returns a new instance of Exporter.



9
10
11
12
# File 'lib/osm/exporter.rb', line 9

def initialize(tile_urls, zoom)
  @tile_urls = Array(tile_urls)
  @zoom = zoom
end

Instance Attribute Details

#tile_urlsObject (readonly)

Returns the value of attribute tile_urls.



7
8
9
# File 'lib/osm/exporter.rb', line 7

def tile_urls
  @tile_urls
end

#zoomObject (readonly)

Returns the value of attribute zoom.



7
8
9
# File 'lib/osm/exporter.rb', line 7

def zoom
  @zoom
end

Instance Method Details

#blend(image1, image2) ⇒ Object



45
46
47
48
# File 'lib/osm/exporter.rb', line 45

def blend(image1, image2)
  png = image2.compose(image1)
  return png
end

#crop(png, crop_dimensions, target_size) ⇒ Object



64
65
66
# File 'lib/osm/exporter.rb', line 64

def crop(png, crop_dimensions, target_size)
  png.crop(crop_dimensions.x, crop_dimensions.y, target_size.x, target_size.y)
end

#download_tiles_as_blob(tiles) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/osm/exporter.rb', line 14

def download_tiles_as_blob(tiles)
  url = tile_urls.sample()
  hydra = Typhoeus::Hydra.new
  requests = tiles.map do |tile|
    tile_url = url.gsub('{x}', tile.x.to_s).gsub('{y}', tile.y.to_s).gsub('{z}', zoom.to_s)
    request = Typhoeus::Request.new(tile_url)
    hydra.queue(request)
    request
  end
  hydra.run

  requests.map do |request|
    request.response.body
  end
end

#draw_line(png, coordinates, rgb = { r: 255, g: 0, b: 0 }) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/osm/exporter.rb', line 50

def draw_line(png, coordinates, rgb = { r: 255, g: 0, b: 0 })
  str0 = coordinates.map { |c| "(#{c.x},#{c.y})" }.join(' ')
  str1 = coordinates.map { |c| "(#{c.x+1},#{c.y})" }.join(' ')
  str2 = coordinates.map { |c| "(#{c.x},#{c.y+1})" }.join(' ')
  str3 = coordinates.map { |c| "(#{c.x-1},#{c.y})" }.join(' ')
  str4 = coordinates.map { |c| "(#{c.x},#{c.y-1})" }.join(' ')
  s = Time.now.to_f
  [str0, str1, str2, str3, str4].each do |str|
    png = png.polygon(str, ChunkyPNG::Color.rgb(rgb[:r], rgb[:g], rgb[:b]))
  end
  puts Time.now.to_f - s
  png
end

#stitch(blobs, tile_dimensions) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/osm/exporter.rb', line 30

def stitch(blobs, tile_dimensions)
  width = tile_dimensions.x
  height = tile_dimensions.y
  png = ChunkyPNG::Image.new(width * TILE_SIZE, height * TILE_SIZE, ChunkyPNG::Color::WHITE)
  count = 0
  (0...height).each do |y|
    (0...width).each do |x|
      tile = ChunkyPNG::Image.from_blob(blobs[count])
      png.replace!(tile, x * TILE_SIZE, y * TILE_SIZE)
      count += 1
    end
  end
  png
end