Module: MapKit

Defined in:
lib/mapkit.rb

Overview

Module to create tile for the google maps tile overlay

Defined Under Namespace

Classes: BoundingBox, Point

Constant Summary collapse

RADIANT =

consant for radiants

Math::PI / 180.0
TILE_SIZE =

the size of tiles in google maps

256
EARTH_RADIUS =

the constant earth radius in meters

6_378_137
MIN_LATITUDE =

the min latitude based on the mercator projection

-85.05112877
MAX_LATITUDE =

the max latitude based on the mercator projection

85.05112877
MIN_LONGITUDE =

the min longitude based on the mercator projection

-180
MAX_LONGITUDE =

the max longitude based on the mercator projection

180
RESOLUTION =

the resolution in meters per pixel

2 * Math::PI * EARTH_RADIUS / TILE_SIZE
VERSION =

version of MapKit

"0.0.2"

Class Method Summary collapse

Class Method Details

.bounding_box(tile_x, tile_y, zoom) ⇒ Object

return bounding box for passed tile coordinates tiles



107
108
109
110
# File 'lib/mapkit.rb', line 107

def self.bounding_box(tile_x, tile_y, zoom)
  top, left, bottom, right = tile_bounds(tile_x, tile_y, zoom)
  BoundingBox.new(top, left, bottom, right, zoom)
end

.clip(val, min, max) ⇒ Object

returns the passed value in case it is in the passed range or the bounding min or max value



161
162
163
# File 'lib/mapkit.rb', line 161

def self.clip(val, min, max)
  (val < min) ? min : (val > max) ? max : val
end

.latlng2pixel(lat, lng, zoom) ⇒ Object

returns pixel coordinates [x, y] based on the passed lat/lng WGS-84 coordinates using the specified zoom level



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/mapkit.rb', line 133

def self.latlng2pixel(lat, lng, zoom)
  lat = clip(lat.to_f, MIN_LATITUDE, MAX_LATITUDE)
  lng = clip(lng.to_f, MIN_LONGITUDE, MAX_LONGITUDE)
  
  x = (lng + 180.0) / 360.0
  sin_lat = Math.sin(lat * RADIANT)
  y = 0.5 - Math.log((1.0 + sin_lat) / (1.0 - sin_lat)) / (4.0 * Math::PI)
  sx, sy = map_size(zoom)
  
  pixel_x = clip(x * sx + 0.5, 0.0, sx - 1.0)
  pixel_y = clip(y * sy + 0.5, 0.0, sy - 1.0)
  [pixel_x.to_i, pixel_y.to_i]
end

.map_size(zoom) ⇒ Object

returns the size [x, y] of the map using the passed zoom level



176
177
178
# File 'lib/mapkit.rb', line 176

def self.map_size(zoom)
  [TILE_SIZE << zoom, TILE_SIZE << zoom]
end

.pixel2latlng(pixel_x, pixel_y, zoom) ⇒ Object

returns lat/lng WGS-84 coordinates [lat, lng] basedon the passed pixel coordinates using the specified zoom level



149
150
151
152
153
154
155
156
157
# File 'lib/mapkit.rb', line 149

def self.pixel2latlng(pixel_x, pixel_y, zoom)
  sx, sy = map_size(zoom)
  x = clip(pixel_x.to_f, 0.0, sx - 1.0) / sx - 0.5
  y = 0.5 - clip(pixel_y.to_f, 0.0, sy - 1.0) / sy
  
  lat = 90.0 - 360.0 * Math.atan(Math.exp(-y * 2.0 * Math::PI)) / Math::PI
  lng = 360.0 * x
  [lat, lng]
end

.pixel2tile(pixel_x, pixel_y) ⇒ Object

returns coordinates of tiles using passed pixel coordinates



166
167
168
# File 'lib/mapkit.rb', line 166

def self.pixel2tile(pixel_x, pixel_y)
  [pixel_x / TILE_SIZE, pixel_y / TILE_SIZE]
end

.resolution(zoom) ⇒ Object

returns resolution in meters per pixel for passed zoom level



181
182
183
# File 'lib/mapkit.rb', line 181

def self.resolution(zoom)
  RESOLUTION / (2 ** zoom)
end

.shift_latlng(lat, lng, shift_x, shift_y, zoom) ⇒ Object

returns [lat, lng] shifted using the passed pixels and zoom



125
126
127
128
129
# File 'lib/mapkit.rb', line 125

def self.shift_latlng(lat, lng, shift_x, shift_y, zoom)
  pixel_x, pixel_y = latlng2pixel(lat.to_f, lng.to_f, zoom)
  pixel_x, pixel_y = pixel_x + shift_x, pixel_y + shift_y
  pixel2latlng(pixel_x, pixel_y, zoom)
end

.tile2pixel(tile_x, tile_y) ⇒ Object

returns coordinates of pixels using passed tile coordinates



171
172
173
# File 'lib/mapkit.rb', line 171

def self.tile2pixel(tile_x, tile_y)
  [tile_x * TILE_SIZE, tile_y * TILE_SIZE]
end

.tile_bounds(tile_x, tile_y, zoom) ⇒ Object

returns bounds [top, left, bottom, right] of the given tile in WGS-94 coordinates



114
115
116
117
118
119
120
121
122
# File 'lib/mapkit.rb', line 114

def self.tile_bounds(tile_x, tile_y, zoom)
  pixel_x, pixel_y = tile2pixel(tile_x, tile_y)
  top, left = pixel2latlng(pixel_x, pixel_y, zoom)
  
  pixel_x, pixel_y = tile2pixel(tile_x + 1, tile_y + 1)
  bottom, right = pixel2latlng(pixel_x, pixel_y, zoom)
  
  [top, left, bottom, right]
end