-
Notifications
You must be signed in to change notification settings - Fork 0
/
quadkey.js
46 lines (41 loc) · 1.44 KB
/
quadkey.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
"use strict";
var Region2 = require("./region2");
var Point2 = require("./point2");
var dx = {0: 0, 1: 1, 2: 0, 3: 1};
var dy = {0: 0, 1: 0, 2: 1, 3: 1};
var quadrants = "0123";
// computes a quadkey, as used to name tiles, based on
// the position, including the scale, of the tile.
exports.encode = encode;
function encode(region, worldSize) {
var size, path = "";
// assert(worldSize.x === worldSize.y, "quadkeys only apply to square regions");
// assert(region.size.x === region.size.y, "quadkeys only apply to square regions");
var position = region.position;
for (var size = region.size.x; size < worldSize.x; size *= 2) {
var y = position.y & size && 1;
var x = position.x & size && 1;
path = quadrants.charAt(y << 1 | x) + path;
}
return path;
}
exports.decode = decode;
function decode(path, worldSize) {
var region = new Region2(new Point2(), new Point2());
decodeInto(path, region, worldSize);
return region;
}
exports.decodeInto = decodeInto;
function decodeInto(path, region, worldSize) {
region.position.x = 0;
region.position.y = 0;
region.size.x = worldSize.x;
region.size.y = worldSize.y;
for (var index = 0; index < path.length; index++) {
var quadrant = path.charAt(index);
region.size.x /= 2;
region.size.y /= 2;
region.position.x += dx[quadrant] * region.size.x;
region.position.y += dy[quadrant] * region.size.y;
}
}