forked from migurski/Squares
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Geo.ts
106 lines (87 loc) · 2.36 KB
/
Geo.ts
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import Core = module('Core');
export interface Projection
{
project(loc:Location):Core.Point;
inverse(point:Core.Point):Location;
locationCoordinate: {
(loc:Location):Core.Coordinate;
(loc:Location, zoom:number):Core.Coordinate;
};
coordinateLocation(coord:Core.Coordinate):Location;
}
/**
* Location class stores latitude and longitude in degrees.
*/
export class Location
{
public lat:number;
public lon:number;
constructor(lat:number, lon:number)
{
this.lat = lat;
this.lon = lon;
}
public toString():string
{
return "(" + this.lat.toFixed(6) + ", "
+ this.lon.toFixed(6) + ")";
}
}
var π = Math.PI;
/**
* Spherical mercator implementation for tiles.
*
* See also http://mathworld.wolfram.com/MercatorProjection.html
*/
export class Mercator implements Projection
{
constructor()
{
}
/**
* Raw projection method returns points in radians.
*/
public project(loc:Location):Core.Point
{
var λ = π * loc.lon / 180,
φ = π * loc.lat / 180;
var x = λ,
y = Math.log(Math.tan(π/4 + φ/2));
return new Core.Point(x, y);
}
/**
* Raw inverse projection method takes points in radians.
*/
public inverse(point:Core.Point):Location
{
var λ = point.x,
φ = 2 * Math.atan(Math.exp(point.y)) - π/2;
var lat = 180 * φ / π,
lon = 180 * λ / π;
return new Location(lat, lon);
}
/**
* Return a tile coordinate for a location.
*
* See also http://wiki.openstreetmap.org/wiki/QuadTiles
*/
public locationCoordinate(loc:Location, zoom:number=0):Core.Coordinate
{
var p = this.project(loc),
col = (p.x + π) / (2 * π),
row = (π - p.y) / (2 * π);
return new Core.Coordinate(row, col, 0).zoomTo(zoom);
}
/**
* Return a location for a tile coordinate.
*
* See also http://wiki.openstreetmap.org/wiki/QuadTiles
*/
public coordinateLocation(coord:Core.Coordinate):Location
{
var coord = coord.zoomTo(0),
x = (coord.column * 2 * π) - π,
y = π - (coord.row * 2 * π);
return this.inverse(new Core.Point(x, y));
}
}