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

"1.0.0"

Class Method Summary collapse

Class Method Details

.bounding_box(tile_x, tile_y, zoom) ⇒ Object

return bounding box for passed tile coordinates tiles



116
117
118
119
# File 'lib/mapkit.rb', line 116

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



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

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



142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/mapkit.rb', line 142

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



185
186
187
# File 'lib/mapkit.rb', line 185

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



158
159
160
161
162
163
164
165
166
# File 'lib/mapkit.rb', line 158

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



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

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



190
191
192
# File 'lib/mapkit.rb', line 190

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



134
135
136
137
138
# File 'lib/mapkit.rb', line 134

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



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

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



123
124
125
126
127
128
129
130
131
# File 'lib/mapkit.rb', line 123

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