Class: CloudMade::TilesService

Inherits:
Service
  • Object
show all
Defined in:
lib/cloudmade/tiles.rb

Overview

Class that is responsible for tile services of Cloudmade

Instance Attribute Summary collapse

Attributes inherited from Service

#connection, #subdomain

Instance Method Summary collapse

Methods inherited from Service

to_url_params, #url, #url_template

Constructor Details

#initialize(client, subdomain, options = {}) ⇒ TilesService

Returns a new instance of TilesService.



24
25
26
27
28
# File 'lib/cloudmade/tiles.rb', line 24

def initialize(client, subdomain, options = {})
  super(client, subdomain)
  @default_tile_size = (options.has_key? 'tile_size') ? options['tile_size'] : 256
  @default_style_id = (options.has_key? 'style_id') ? options['style_id'] : 1
end

Instance Attribute Details

#default_style_idObject

Returns the value of attribute default_style_id.



22
23
24
# File 'lib/cloudmade/tiles.rb', line 22

def default_style_id
  @default_style_id
end

#default_tile_sizeObject

Returns the value of attribute default_tile_size.



21
22
23
# File 'lib/cloudmade/tiles.rb', line 21

def default_tile_size
  @default_tile_size
end

Instance Method Details

#degrees(radians) ⇒ Object



59
60
61
# File 'lib/cloudmade/tiles.rb', line 59

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

  • lat Latitude of requested tile

  • lon Longitude of requested tile

  • zoom Zoom level, on which tile is being requested

  • style_id CloudMade’s style id, if not given, default style is used (usually 1)

  • tile_size size of tile, if not given the default 256 is used



84
85
86
# File 'lib/cloudmade/tiles.rb', line 84

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

  • zoom Zoom level, on which tile is being requested

  • style_id CloudMade’s style id, if not given, default style is used (usually 1)

  • tile_size size of tile, if not given the default 256 is used



96
97
98
99
100
# File 'lib/cloudmade/tiles.rb', line 96

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

  • lat Latitude

  • lon Longitude

  • zoom Zoom level



34
35
36
37
38
39
40
41
# File 'lib/cloudmade/tiles.rb', line 34

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:



55
56
57
# File 'lib/cloudmade/tiles.rb', line 55

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

  • xtile X coordinate of the tile

  • ytile Y coordinate of the tile

  • zoom Zoom level



47
48
49
50
51
52
# File 'lib/cloudmade/tiles.rb', line 47

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



63
64
65
66
67
# File 'lib/cloudmade/tiles.rb', line 63

def xtile(lon, zoom)
  factor = 2**(zoom - 1)
  xtile = 1 + lon / 180.0
  return (xtile * factor).to_i
end

#ytile(lat, zoom) ⇒ Object



69
70
71
72
73
74
# File 'lib/cloudmade/tiles.rb', line 69

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