Class: Tairu::Store::MBTiles

Inherits:
Object
  • Object
show all
Defined in:
lib/tairu/store/mbtiles.rb

Instance Method Summary collapse

Constructor Details

#initialize(layer) ⇒ MBTiles

Returns a new instance of MBTiles.



7
8
9
10
11
# File 'lib/tairu/store/mbtiles.rb', line 7

def initialize(layer)
  tileset = File.join(File.expand_path(Tairu.layers[layer]['location']), Tairu.layers[layer]['tileset'])
  conn = defined?(JRUBY_VERSION) ? "jdbc:sqlite:#{tileset}" : "sqlite://#{tileset}"
  @db = Sequel.connect(conn, max_connections: 1)
end

Instance Method Details

#flip_y(zoom, y) ⇒ Object



13
14
15
# File 'lib/tairu/store/mbtiles.rb', line 13

def flip_y(zoom, y)
  (2 ** zoom - 1) - y
end

#get(coord, format = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/tairu/store/mbtiles.rb', line 17

def get(coord, format=nil)
  formats = {
    'png' => 'image/png',
    'jpg' => 'image/jpg'
  }

  format = @db[:metadata].select(:value).where(name: 'format').first
  mime_type = format.nil? ? formats['png'] : formats[format[:value]]

  tile = @db[:tiles].where(zoom_level: coord.zoom, tile_column: coord.column, tile_row: flip_y(coord.zoom, coord.row))

  tile_data = tile.first.nil? ? nil : Tairu::Tile.new(tile.first[:tile_data], mime_type)
  tile_data
end

#get_grid(coord) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/tairu/store/mbtiles.rb', line 32

def get_grid(coord)
  grid = @db[:grids].where(zoom_level: coord.zoom, tile_column: coord.column, tile_row: flip_y(coord.zoom, coord.row))

  utf_grid = JSON.parse(inflate(grid.first[:grid]))
  utf_grid[:data] = {}

  grid_data = @db[:grid_data].where(zoom_level: coord.zoom, tile_column: coord.column, tile_row: tile_row)

  grid_data.each do |gd|
    utf_grid[:data][gd[:key_name]] = JSON.parse(gd[:key_json])
  end

  utf_grid
end

#get_legendObject



47
48
49
# File 'lib/tairu/store/mbtiles.rb', line 47

def get_legend
  @db[:metadata].where(name: 'legend').first
end

#inflate(str) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/tairu/store/mbtiles.rb', line 62

def inflate(str)
  zstream = Zlib::Inflate.new
  buf = zstream.inflate(str)
  zstream.finish
  zstream.close
  buf
end

#infoObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/tairu/store/mbtiles.rb', line 51

def info
  info = {}

  %w{name type version description format bounds attribution minzoom maxzoom template legend}.each do |key|
    value = @db[:metadata].where(name: key).first
    info[key] = value[:value] unless value.nil?
  end

  info
end