Class: CloudMade::TilesService
Overview
Class that is responsible for tile services of Cloudmade
Instance Attribute Summary collapse
-
#default_style_id ⇒ Object
Returns the value of attribute default_style_id.
-
#default_tile_size ⇒ Object
Returns the value of attribute default_tile_size.
Attributes inherited from Service
Instance Method Summary collapse
- #degrees(radians) ⇒ Object
-
#get_tile(lat, lon, zoom, style_id = nil, tile_size = nil) ⇒ Object
Get tile with given latitude, longitude and zoom.
-
#get_xy_tile(xtile, ytile, zoom, style_id = nil, tile_size = nil) ⇒ Object
Get tile with given x, y numbers and zoom Returns Raw PNG data which could be saved to file.
-
#initialize(client, subdomain, options = {}) ⇒ TilesService
constructor
A new instance of TilesService.
-
#latlon2tilenums(lat, lon, zoom) ⇒ Object
Convert latitude, longitude pair to tile coordinates.
-
#radians(degrees) ⇒ Object
:nodoc:.
-
#tilenums2latlon(xtile, ytile, zoom) ⇒ Object
Convert tile coordinates pair to latitude, longitude.
- #xtile(lon, zoom) ⇒ Object
- #ytile(lat, zoom) ⇒ Object
Methods inherited from Service
to_url_params, #url, #url_template
Constructor Details
#initialize(client, subdomain, options = {}) ⇒ TilesService
Returns a new instance of TilesService.
26 27 28 29 30 |
# File 'lib/tiles.rb', line 26 def initialize(client, subdomain, = {}) super(client, subdomain) @default_tile_size = (.has_key? 'tile_size') ? ['tile_size'] : 256 @default_style_id = (.has_key? 'style_id') ? ['style_id'] : 1 end |
Instance Attribute Details
#default_style_id ⇒ Object
Returns the value of attribute default_style_id.
24 25 26 |
# File 'lib/tiles.rb', line 24 def default_style_id @default_style_id end |
#default_tile_size ⇒ Object
Returns the value of attribute default_tile_size.
23 24 25 |
# File 'lib/tiles.rb', line 23 def default_tile_size @default_tile_size end |
Instance Method Details
#degrees(radians) ⇒ Object
61 62 63 |
# File 'lib/tiles.rb', line 61 def degrees(radians) radians * 180 / Math::PI end |
#get_tile(lat, lon, zoom, style_id = nil, tile_size = nil) ⇒ Object
Get tile with given latitude, longitude and zoom. Returns Raw PNG data which could be saved to file
-
latLatitude of requested tile -
lonLongitude of requested tile -
zoomZoom level, on which tile is being requested -
style_idCloudMade’s style id, if not given, default style is used (usually 1) -
tile_sizesize of tile, if not given the default 256 is used
86 87 88 |
# File 'lib/tiles.rb', line 86 def get_tile(lat, lon, zoom, style_id = nil, tile_size = nil) get_xy_tile(xtile(lon, zoom), ytile(lat, zoom), zoom, style_id, tile_size) end |
#get_xy_tile(xtile, ytile, zoom, style_id = nil, tile_size = nil) ⇒ Object
Get tile with given x, y numbers and zoom Returns Raw PNG data which could be saved to file
-
xtile -
ytile -
zoomZoom level, on which tile is being requested -
style_idCloudMade’s style id, if not given, default style is used (usually 1) -
tile_sizesize of tile, if not given the default 256 is used
98 99 100 101 102 |
# File 'lib/tiles.rb', line 98 def get_xy_tile(xtile, ytile, zoom, style_id = nil, tile_size = nil) style_id = self.default_style_id if style_id == nil tile_size = self.default_tile_size if tile_size == nil connect "/#{style_id}/#{tile_size}/#{zoom}/#{xtile}/#{ytile}.png" end |
#latlon2tilenums(lat, lon, zoom) ⇒ Object
Convert latitude, longitude pair to tile coordinates. Returns tile coordinates as a CloudMade::Point object
-
latLatitude -
lonLongitude -
zoomZoom level
36 37 38 39 40 41 42 43 |
# File 'lib/tiles.rb', line 36 def latlon2tilenums(lat, lon, zoom) factor = 2**(zoom - 1) lat = radians(lat) lon = radians(lon) xtile = 1 + lon / Math::PI ytile = 1 - Math.log(Math.tan(lat) + (1 / Math.cos(lat))) / Math::PI return Point.new((xtile * factor).to_i, (ytile * factor).to_i) end |
#radians(degrees) ⇒ Object
:nodoc:
57 58 59 |
# File 'lib/tiles.rb', line 57 def radians(degrees) Math::PI * degrees / 180 end |
#tilenums2latlon(xtile, ytile, zoom) ⇒ Object
Convert tile coordinates pair to latitude, longitude. Returns latitude, longitude as a CloudMade::Point object
-
xtileX coordinate of the tile -
ytileY coordinate of the tile -
zoomZoom level
49 50 51 52 53 54 |
# File 'lib/tiles.rb', line 49 def tilenums2latlon(xtile, ytile, zoom) factor = 2.0 ** zoom lon = (xtile * 360 / factor) - 180.0 lat = Math.atan(Math.sinh(Math::PI * (1 - 2 * ytile / factor))) return Point.new(degrees(lat), lon) end |
#xtile(lon, zoom) ⇒ Object
65 66 67 68 69 |
# File 'lib/tiles.rb', line 65 def xtile(lon, zoom) factor = 2**(zoom - 1) xtile = 1 + lon / 180.0 return (xtile * factor).to_i end |
#ytile(lat, zoom) ⇒ Object
71 72 73 74 75 76 |
# File 'lib/tiles.rb', line 71 def ytile(lat, zoom) factor = 2**(zoom - 1) lat = radians(lat) ytile = 1 - Math.log(Math.tan(lat) + (1 / Math.cos(lat))) / Math::PI return (ytile * factor).to_i end |