-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmercator.rb
25 lines (22 loc) · 845 Bytes
/
mercator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Mercator
TILE_SIZE = 256.0
def self.pixels(latitude, longitude)
siny = Math.sin(latitude * Math::PI / 180)
siny = [[siny, -0.99].max, 0.99].min
[TILE_SIZE * (0.5 + longitude / 360), TILE_SIZE * (0.5 - Math.log((1 + siny) / (1 - siny)) / (4 * Math::PI))]
end
def self.coordinates(pixels)
[Math.asin(Math.tanh(2 * Math::PI * (0.5 - pixels[1] / TILE_SIZE))) * 180 / Math::PI, 360 * (pixels[0] / TILE_SIZE - 0.5)]
end
def self.corners(center_lat, center_lon, zoom, width, height)
center = pixels(center_lat, center_lon)
ne = coordinates [center[0] + width / (2.0 * 2**zoom), center[1] - height / (2.0 * 2**zoom)]
sw = coordinates [center[0] - width / (2.0 * 2**zoom), center[1] + height / (2.0 * 2**zoom)]
{
'N': ne[0],
'E': ne[1],
'S': sw[0],
'W': sw[1],
}
end
end