Class: CloudMade::TilesService

Inherits:
Service
  • Object
show all
Defined in:
lib/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.



26
27
28
29
30
# File 'lib/tiles.rb', line 26

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.



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

def default_style_id
  @default_style_id
end

#default_tile_sizeObject

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

  • 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



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

  • 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



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

  • lat Latitude

  • lon Longitude

  • zoom Zoom 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

  • xtile X coordinate of the tile

  • ytile Y coordinate of the tile

  • zoom Zoom 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