-
Notifications
You must be signed in to change notification settings - Fork 0
/
tilebelt.js
282 lines (260 loc) · 6.11 KB
/
tilebelt.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
'use strict';
var d2r = Math.PI / 180,
r2d = 180 / Math.PI;
/**
* Get the bbox of a tile
*
* @name tileToBBOX
* @param {Array<number>} tile
* @returns {Array<number>} bbox
* @example
* var bbox = tileToBBOX([5, 10, 10])
* //=bbox
*/
export function tileToBBOX(tile) {
var e = tile2lon(tile[0] + 1, tile[2]);
var w = tile2lon(tile[0], tile[2]);
var s = tile2lat(tile[1] + 1, tile[2]);
var n = tile2lat(tile[1], tile[2]);
return [w, s, e, n];
}
/**
* Get a geojson representation of a tile
*
* @name tileToGeoJSON
* @param {Array<number>} tile
* @returns {Feature<Polygon>}
* @example
* var poly = tileToGeoJSON([5, 10, 10])
* //=poly
*/
function tileToGeoJSON(tile) {
var bbox = tileToBBOX(tile);
var poly = {
type: 'Polygon',
coordinates: [[
[bbox[0], bbox[3]],
[bbox[0], bbox[1]],
[bbox[2], bbox[1]],
[bbox[2], bbox[3]],
[bbox[0], bbox[3]]
]]
};
return poly;
}
function tile2lon(x, z) {
return x / Math.pow(2, z) * 360 - 180;
}
function tile2lat(y, z) {
var n = Math.PI - 2 * Math.PI * y / Math.pow(2, z);
return r2d * Math.atan(0.5 * (Math.exp(n) - Math.exp(-n)));
}
/**
* Get the tile for a point at a specified zoom level
*
* @name pointToTile
* @param {number} lon
* @param {number} lat
* @param {number} z
* @returns {Array<number>} tile
* @example
* var tile = pointToTile(1, 1, 20)
* //=tile
*/
function pointToTile(lon, lat, z) {
var tile = pointToTileFraction(lon, lat, z);
tile[0] = Math.floor(tile[0]);
tile[1] = Math.floor(tile[1]);
return tile;
}
/**
* Get the 4 tiles one zoom level higher
*
* @name getChildren
* @param {Array<number>} tile
* @returns {Array<Array<number>>} tiles
* @example
* var tiles = getChildren([5, 10, 10])
* //=tiles
*/
export function getChildren(tile) {
return [
[tile[0] * 2, tile[1] * 2, tile[2] + 1],
[tile[0] * 2 + 1, tile[1] * 2, tile[2 ] + 1],
[tile[0] * 2 + 1, tile[1] * 2 + 1, tile[2] + 1],
[tile[0] * 2, tile[1] * 2 + 1, tile[2] + 1]
];
}
/**
* Get the tile one zoom level lower
*
* @name getParent
* @param {Array<number>} tile
* @returns {Array<number>} tile
* @example
* var tile = getParent([5, 10, 10])
* //=tile
*/
function getParent(tile) {
return [tile[0] >> 1, tile[1] >> 1, tile[2] - 1];
}
function getSiblings(tile) {
return getChildren(getParent(tile));
}
/**
* Get the 3 sibling tiles for a tile
*
* @name getSiblings
* @param {Array<number>} tile
* @returns {Array<Array<number>>} tiles
* @example
* var tiles = getSiblings([5, 10, 10])
* //=tiles
*/
function hasSiblings(tile, tiles) {
var siblings = getSiblings(tile);
for (var i = 0; i < siblings.length; i++) {
if (!hasTile(tiles, siblings[i])) return false;
}
return true;
}
/**
* Check to see if an array of tiles contains a particular tile
*
* @name hasTile
* @param {Array<Array<number>>} tiles
* @param {Array<number>} tile
* @returns {boolean}
* @example
* var tiles = [
* [0, 0, 5],
* [0, 1, 5],
* [1, 1, 5],
* [1, 0, 5]
* ]
* hasTile(tiles, [0, 0, 5])
* //=boolean
*/
function hasTile(tiles, tile) {
for (var i = 0; i < tiles.length; i++) {
if (tilesEqual(tiles[i], tile)) return true;
}
return false;
}
/**
* Check to see if two tiles are the same
*
* @name tilesEqual
* @param {Array<number>} tile1
* @param {Array<number>} tile2
* @returns {boolean}
* @example
* tilesEqual([0, 1, 5], [0, 0, 5])
* //=boolean
*/
function tilesEqual(tile1, tile2) {
return (
tile1[0] === tile2[0] &&
tile1[1] === tile2[1] &&
tile1[2] === tile2[2]
);
}
/**
* Get the quadkey for a tile
*
* @name tileToQuadkey
* @param {Array<number>} tile
* @returns {string} quadkey
* @example
* var quadkey = tileToQuadkey([0, 1, 5])
* //=quadkey
*/
function tileToQuadkey(tile) {
var index = '';
for (var z = tile[2]; z > 0; z--) {
var b = 0;
var mask = 1 << (z - 1);
if ((tile[0] & mask) !== 0) b++;
if ((tile[1] & mask) !== 0) b += 2;
index += b.toString();
}
return index;
}
/**
* Get the tile for a quadkey
*
* @name quadkeyToTile
* @param {string} quadkey
* @returns {Array<number>} tile
* @example
* var tile = quadkeyToTile('00001033')
* //=tile
*/
function quadkeyToTile(quadkey) {
var x = 0;
var y = 0;
var z = quadkey.length;
for (var i = z; i > 0; i--) {
var mask = 1 << (i - 1);
var q = +quadkey[z - i];
if (q === 1) x |= mask;
if (q === 2) y |= mask;
if (q === 3) {
x |= mask;
y |= mask;
}
}
return [x, y, z];
}
/**
* Get the smallest tile to cover a bbox
*
* @name bboxToTile
* @param {Array<number>} bbox
* @returns {Array<number>} tile
* @example
* var tile = bboxToTile([ -178, 84, -177, 85 ])
* //=tile
*/
function bboxToTile(bboxCoords) {
var min = pointToTile(bboxCoords[0], bboxCoords[1], 32);
var max = pointToTile(bboxCoords[2], bboxCoords[3], 32);
var bbox = [min[0], min[1], max[0], max[1]];
var z = getBboxZoom(bbox);
if (z === 0) return [0, 0, 0];
var x = bbox[0] >>> (32 - z);
var y = bbox[1] >>> (32 - z);
return [x, y, z];
}
function getBboxZoom(bbox) {
var MAX_ZOOM = 28;
for (var z = 0; z < MAX_ZOOM; z++) {
var mask = 1 << (32 - (z + 1));
if (((bbox[0] & mask) !== (bbox[2] & mask)) ||
((bbox[1] & mask) !== (bbox[3] & mask))) {
return z;
}
}
return MAX_ZOOM;
}
/**
* Get the precise fractional tile location for a point at a zoom level
*
* @name pointToTileFraction
* @param {number} lon
* @param {number} lat
* @param {number} z
* @returns {Array<number>} tile fraction
* var tile = pointToTileFraction(30.5, 50.5, 15)
* //=tile
*/
function pointToTileFraction(lon, lat, z) {
var sin = Math.sin(lat * d2r),
z2 = Math.pow(2, z),
x = z2 * (lon / 360 + 0.5),
y = z2 * (0.5 - 0.25 * Math.log((1 + sin) / (1 - sin)) / Math.PI);
// Wrap Tile X
x = x % z2;
if (x < 0) x = x + z2;
return [x, y, z];
}