Created
April 5, 2011 19:30
-
-
Save alexfish/904336 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'cloudmade' | |
include CloudMade | |
class MapService | |
def initialize(latmin,latmax,lonmin,lonmax,zoommin,zoommax,styleid) | |
@latmin = latmin | |
@latmax = latmax | |
@lonmin = lonmin | |
@lonmax = lonmax | |
@zoommin = zoommin | |
@zoommax = zoommax | |
@styleid = styleid | |
@apikey = 'YOUR KEY' | |
end | |
def selecttiles | |
cm = Client.from_parameters(@apikey) | |
tl = [] | |
for zoom in @zoommin..@zoommax | |
txmin = cm.tiles.xtile(@lonmin,zoom) | |
txmax = cm.tiles.xtile(@lonmax,zoom) | |
tymin = cm.tiles.ytile(@latmax,zoom) | |
tymax = cm.tiles.ytile(@latmin,zoom) | |
ntx = txmax - txmin + 1 | |
nty = tymax - tymin + 1 | |
tl[zoom] = [] | |
puts "Schedule #{ntx*nty} tiles for zoom level #{zoom} for downloan .." | |
for tx in txmin..txmax | |
for ty in tymin..tymax | |
tl[zoom].push([tx,ty,zoom]) | |
end | |
end | |
end | |
return tl | |
end | |
def downloadtiles(tiles) | |
for zoom in tiles | |
unless zoom == nil | |
puts "Downloadings #{zoom.count} tiles for zoom level #{zoom[0][2]}" | |
for tile in zoom | |
downloadtile(tile[0],tile[1],tile[2]) | |
end | |
end | |
end | |
end | |
def downloadtile(x,y,z) | |
cm = Client.from_parameters(@apikey) | |
png = cm.tiles.get_xy_tile(x,y,z,@styleid) | |
directory_z = "#{z}" | |
directory_x = "#{z}/#{x}" | |
if !FileTest::directory?(directory_z) | |
Dir::mkdir(directory_z) | |
end | |
if !FileTest::directory?(directory_x) | |
Dir::mkdir(directory_x) | |
end | |
file = File.new("#{z}/#{x}/#{y}.png", 'w') | |
file.write(png) | |
file.close | |
end | |
end | |
#example usage | |
m = MapService.new(51.4264,51.5886,-0.2695,0.0124,3,18,2666) | |
tiles = m.selecttiles | |
m.downloadtiles(tiles) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment