Class: Mormon::Tile::Data

Inherits:
Object
  • Object
show all
Defined in:
lib/mormon/tile_data.rb

Constant Summary collapse

TILE_DIR =
'cache/%d/%d/%d'
TILE_URL =
'http://dev.openstreetmap.org/~ojw/api/?/map/%d/%d/%d'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Data

Returns a new instance of Data.



12
13
14
# File 'lib/mormon/tile_data.rb', line 12

def initialize(options = {})
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/mormon/tile_data.rb', line 10

def options
  @options
end

Class Method Details

.download_levelObject



16
17
18
19
# File 'lib/mormon/tile_data.rb', line 16

def self.download_level
  # All primary downloads are done at a particular zoom level
  15
end

Instance Method Details

#get_osm(z, x, y) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/mormon/tile_data.rb', line 21

def get_osm(z, x, y)
  # Download OSM data for the region covering a slippy-map tile
  if x < 0 or y < 0 or z < 0 or z > 25
    puts "Disallowed %d,%d at %d" % [x, y, z]
    return
  end
  
  directory = TILE_DIR % [z,x,y]
  FileUtils.mkdir_p(directory) unless Dir.exists?(directory)
  
  if z == Data.download_level
    url = TILE_URL % [z,x,y]
    filename = '%s/data.osm' % directory

    # puts "URL: %s" % url
    # puts "filename: %s" % filename

    # download the data
    if options[:reset_cache] || !File.exists?(filename)
      begin
        open(url) do |content|
          File.new(filename) { |f| f.write content }
        end
        filename
      rescue OpenURI::HTTPError
        "Tile not found in #{url}"
      end
    end
    
  elsif z > Data.download_level
    # use larger tile
    while z > Data.download_level
      z = z - 1
      x = (x / 2).to_i
      y = (y / 2).to_i
    end
    get_osm z, x, y
  end

end