Class: MapPrint::Tile

Inherits:
Object
  • Object
show all
Defined in:
lib/map_print/tiles/tile.rb

Direct Known Subclasses

BingTile, OSMTile

Instance Method Summary collapse

Constructor Details

#initialize(x, y, z, base_url) ⇒ Tile

Returns a new instance of Tile.



6
7
8
9
10
11
# File 'lib/map_print/tiles/tile.rb', line 6

def initialize(x, y, z, base_url)
  @base_url = base_url
  @x = x
  @y = y
  @z = z
end

Instance Method Details

#cache_nameObject



41
42
43
# File 'lib/map_print/tiles/tile.rb', line 41

def cache_name
  raise 'SubClasses should overwrite this method'
end

#coordsObject



13
14
15
# File 'lib/map_print/tiles/tile.rb', line 13

def coords
  {x: @x, y: @y, z: @z}
end

#downloadObject



17
18
19
20
21
22
# File 'lib/map_print/tiles/tile.rb', line 17

def download
  unless File.exists?(file_path)
    content = open(tile_url).read
    write_file(content)
  end
end

#file_pathObject



33
34
35
# File 'lib/map_print/tiles/tile.rb', line 33

def file_path
  File.join(folder_name, "#{@y}.png")
end

#provider_nameObject



37
38
39
# File 'lib/map_print/tiles/tile.rb', line 37

def provider_name
  raise 'SubClasses should overwrite this method'
end

#tile_number_to_lat_lngObject



24
25
26
27
28
29
30
31
# File 'lib/map_print/tiles/tile.rb', line 24

def tile_number_to_lat_lng
  n = 2.0 ** @z
  lon_deg = @x / n * 360.0 - 180.0
  lat_rad = Math::atan(Math::sinh(Math::PI * (1 - 2 * @y / n)))
  lat_deg = 180.0 * (lat_rad / Math::PI)

  { lat: lat_deg, lng: lon_deg }
end

#tile_urlObject



45
46
47
# File 'lib/map_print/tiles/tile.rb', line 45

def tile_url
  raise 'SubClasses should overwrite this method'
end