forked from datalyze-solutions/globalmaptiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
globalmaptiles.js
157 lines (141 loc) · 4.83 KB
/
globalmaptiles.js
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const pi_div_360 = Math.PI / 360.0;
const pi_div_180 = Math.PI / 180.0;
const pi_div_2 = Math.PI / 2.0;
const pi_4 = Math.PI * 4;
const pi_2 = Math.PI * 2;
const pi = Math.PI;
const _180_div_pi = 180 / Math.PI;
class GlobalMercator {
constructor() {
this.tileSize = 256;
this.initialResolution = pi_2 * 6378137 / this.tileSize;
this.originShift = pi_2 * 6378137 / 2.0;
}
LatLonToMeters(lat, lon) {
// Converts given lat/lon in WGS84 Datum to XY in Spherical Mercator EPSG:900913
let mx = lon * this.originShift / 180.0;
let my = Math.log(Math.tan((90 + lat) * pi_div_360)) / pi_div_180;
my = my * this.originShift / 180.0;
return { mx: mx, my: my };
}
MetersToLatLon(mx, my) {
// Converts XY point from Spherical Mercator EPSG:900913 to lat/lon in WGS84 Datum
let lon = mx / this.originShift * 180.0;
let lat = my / this.originShift * 180.0;
lat =
_180_div_pi *
(2 * Math.atan(Math.exp(lat * pi_div_180)) - pi_div_2);
return { lat: lat, lon: lon };
}
MetersToPixels(mx, my, zoom) {
// Converts EPSG:900913 to pyramid pixel coordinates in given zoom level
var res = this.Resolution(zoom);
var px = (mx + this.originShift) / res;
var py = (my + this.originShift) / res;
return { px: px, py: py };
}
Resolution(zoom) {
// Resolution (meters/pixel) for given zoom level (measured at Equator)
return this.initialResolution / Math.pow(2, zoom);
}
TileBounds(tx, ty, zoom) {
// Returns bounds of the given tile in EPSG:900913 coordinates
let minx, miny, maxx, maxy;
minx = this.PixelsToMeters(
tx * this.tileSize,
ty * this.tileSize,
zoom
)["mx"];
miny = this.PixelsToMeters(
tx * this.tileSize,
ty * this.tileSize,
zoom
)["my"];
maxx = this.PixelsToMeters(
(tx + 1) * this.tileSize,
(ty + 1) * this.tileSize,
zoom
)["mx"];
maxy = this.PixelsToMeters(
(tx + 1) * this.tileSize,
(ty + 1) * this.tileSize,
zoom
)["my"];
return { minx: minx, miny: miny, maxx: maxx, maxy: maxy };
}
PixelsToMeters(px, py, zoom) {
// Converts pixel coordinates in given zoom level of pyramid to EPSG:900913
var res, mx, my;
res = this.Resolution(zoom);
mx = px * res - this.originShift;
my = py * res - this.originShift;
return { mx: mx, my: my };
}
PixelsToTile(px, py) {
// Returns a tile covering region in given pixel coordinates
var tx, ty;
tx = Math.round(Math.ceil(px / this.tileSize) - 1);
ty = Math.round(Math.ceil(py / this.tileSize) - 1);
return { tx: tx, ty: ty };
}
PixelsToRaster(px, py, zoom) {
// Move the origin of pixel coordinates to top-left corner
var mapSize;
mapSize = this.tileSize << zoom;
return { x: px, y: mapSize - py };
}
LatLonToTile(lat, lon, zoom) {
var meters = this.LatLonToMeters(lat, lon);
var pixels = this.MetersToPixels(meters.mx, meters.my, zoom);
return this.PixelsToTile(pixels.px, pixels.py);
}
MetersToTile(mx, my, zoom) {
var pixels = this.MetersToPixels(mx, my, zoom);
return this.PixelsToTile(pixels.px, pixels.py);
}
GoogleTile(tx, ty, zoom) {
// Converts TMS tile coordinates to Google Tile coordinates
// coordinate origin is moved from bottom-left to top-left corner of the extent
return { tx: tx, ty: Math.pow(2, zoom) - 1 - ty };
}
QuadKey(tx, ty, zoom) {
// Converts TMS tile coordinates to Microsoft QuadTree
let quadKey = "";
ty = 2 ** zoom - 1 - ty;
for (let i = zoom; i > 0; i--) {
let digit = 0;
let mask = 1 << (i - 1);
if ((tx & mask) != 0) {
digit += 1;
}
if ((ty & mask) != 0) {
digit += 2;
}
quadKey += digit.toString();
}
return quadKey;
}
QuadKeyToTile(quadKey) {
// Transform quadkey to tile coordinates
let tx = 0;
let ty = 0;
let zoom = quadKey.length;
for (let i = 0; i < zoom; i++) {
let bit = zoom - i;
let mask = 1 << (bit - 1);
if (quadKey[zoom - bit] === "1") {
tx |= mask;
}
if (quadKey[zoom - bit] == "2") {
ty |= mask;
}
if (quadKey[zoom - bit] == "3") {
tx |= mask;
ty |= mask;
}
}
ty = 2 ** zoom - 1 - ty;
return { tx: tx, ty: ty, zoom: zoom };
}
}
module.exports = GlobalMercator;