-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproj.js
154 lines (140 loc) · 3.38 KB
/
proj.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
import gm from 'global-mercator'
const lnglat2google = gm.lngLatToGoogle
const lnglat2googlefrac = gm.pointToTileFraction
const google2bbox = gm.googleToBBox
const google2quadkey = gm.googleToQuadkey
const quadkey2google = gm.quadkeyToGoogle
const quadkey2quadint = quadkey => {
let res = '0b'
for (let i = 0; i < 32; i++) {
if (i >= quadkey.length) {
res += '00'
continue
}
const quat = quadkey.charAt(i)
if (quat == '0') res += '00'
else if (quat == '1') res += '01'
else if (quat == '2') res += '10'
else if (quat == '3') res += '11'
}
return BigInt(res)
}
const quadint2quadkey = quadint => {
const s = quadint.toString(2).padStart(64, '0')
let res = ''
for (let i = 0; i < 32; i++) {
const bin = s.slice(i * 2, i * 2 + 2)
if (bin == '00') res += '0'
else if (bin == '01') res += '1'
else if (bin == '10') res += '2'
else if (bin == '11') res += '3'
}
return res
}
const zoom2quadint = zoom => {
let res = '0b'
for (let i = 0; i < 32; i++) {
if (i > zoom - 1) {
res += '11'
continue
}
res += '00'
}
return BigInt(res)
}
const zoom2quadint_inv = zoom => {
let res = '0b'
for (let i = 0; i < 32; i++) {
if (i > zoom - 1) {
res += '00'
continue
}
res += '11'
}
return BigInt(res)
}
const lnglat2quadint = lnglat => {
const tile = gm.lngLatToGoogle(lnglat, 32)
const quadkey = gm.googleToQuadkey(tile)
const quadint = quadkey2quadint(quadkey)
return quadint
}
const google2quadint = tile => {
const quadkey = gm.googleToQuadkey(tile)
const quadint = quadkey2quadint(quadkey)
return quadint
}
const google2quadintrange = tile => {
const quadint_low = google2quadint(tile)
const quadint_zoom = zoom2quadint(tile[2])
const quadint_high = quadint_low + quadint_zoom
return [quadint_low, quadint_high]
}
const quadint2googlefrac = (quadint, z) => {
const s = quadint.toString(2).padStart(64, '0')
let x = 0
let y = 0
for (let i = z + 1; i > 0; i--) {
const mask = 1 << (i - 1)
const ii = (z - i) * 2
const bin = s.slice(ii, ii + 2)
if (bin == '00') continue
else if (bin == '01') x |= mask
else if (bin == '10') y |= mask
else if (bin == '11') {
x |= mask
y |= mask
}
}
let frac = 1
for (let i = z; i < 32; i++) {
frac /= 2
const ii = i * 2
const bin = s.slice(ii, ii + 2)
if (bin == '00') continue
else if (bin == '01') x += frac
else if (bin == '10') y += frac
else if (bin == '11') {
x += frac
y += frac
}
}
return [x, y, z]
}
const googlefrac2vecxy = (xyz, origin_xyz, extent) =>
xyz.slice(0, 2).map((v, i) => Math.trunc((v - origin_xyz[i]) * extent))
const vecxy2obj = xy => ({ x: xy[0], y: xy[1] })
const generatevectortile = layers => ({
layers: Object.fromEntries(
layers.map(layer => [layer.name, {
...layer,
version: 2,
length: layer.features.length,
feature: i => {
const feature = layer.features[i]
return {
...feature,
loadGeometry: () => feature.geometry
}
}
}])
)
})
export {
lnglat2google,
lnglat2googlefrac,
google2bbox,
google2quadkey,
quadkey2google,
quadkey2quadint,
quadint2quadkey,
lnglat2quadint,
google2quadint,
zoom2quadint,
zoom2quadint_inv,
google2quadintrange,
quadint2googlefrac,
googlefrac2vecxy,
vecxy2obj,
generatevectortile
}