Class: Tairu::Store::GeoJSON

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

Instance Method Summary collapse

Constructor Details

#initialize(layer) ⇒ GeoJSON

Returns a new instance of GeoJSON.



4
5
6
7
# File 'lib/tairu/store/geojson.rb', line 4

def initialize(layer)
  @tileset = File.join(File.expand_path(Tairu.layers[layer]['location']), Tairu.layers[layer]['tileset'])
  @flip = Tairu.layers[layer]['flip_y_coordinate']
end

Instance Method Details

#flip_y(z, y) ⇒ Object



9
10
11
# File 'lib/tairu/store/geojson.rb', line 9

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

#get(coord) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/tairu/store/geojson.rb', line 13

def get(coord)
  y = @flip ? flip_y(coord.zoom, coord.row) : coord.row

  path = File.join(@tileset, coord.zoom, coord.column, "#{y}.json")

  return nil unless File.exists?(path)

  data = File.open(path, 'r') do |f|
    begin
      f.flock(File::LOCK_SH)
      f.read
    ensure
      f.flock(File::LOCK_UN)
    end
  end

  mime_type = 'application/json'
  tile = data.nil? ? nil : Tairu::Tile.new(data, mime_type)
  tile
end