-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
262 lines (220 loc) · 7.19 KB
/
index.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
'use strict';
var Pbf = require('pbf');
var VT = require('./vector-tile.js');
exports.read = readTile;
exports.write = writeTile;
exports.getLayerValues = getLayerValues;
exports.updateLayerProperties = updateLayerProperties;
exports.mergeLayer = mergeLayer;
exports.filterLayerByKeys = filterLayerByKeys;
exports.selectLayerKeys = selectLayerKeys;
function readTile(buf) {
return VT.readTile(new Pbf(buf));
}
function writeTile(tile) {
var pbf = new Pbf();
VT.writeTile(tile, pbf);
return pbf.finish();
}
function getLayerValues(layer, key) {
var keyIndex = layer.keys.indexOf(key);
var values = [];
for (var i = 0; i < layer.features.length; i++) {
var tags = layer.features[i].tags;
for (var j = 0; j < tags.length; j += 2) {
if (tags[j] === keyIndex) {
var value = layer.values[tags[j + 1]];
if (value !== undefined) values.push(value);
else throw new Error(key + ' not found');
break;
}
}
}
return values;
}
function filterLayerByKeys(layer, requiredKeys) {
var keys = layer.keys;
var requiredLookup = {};
var requiredCount = requiredKeys.length;
for (var i = 0; i < requiredCount; i++) {
requiredLookup[keys.indexOf(requiredKeys[i])] = true;
}
layer.features = layer.features.filter(function (feature) {
return hasAllKeys(feature.tags, requiredLookup, requiredCount);
});
return layer;
}
function selectLayerKeys(layer, keysToKeep) {
var keys = layer.keys;
var values = layer.values;
var keepLookup = {};
layer.keyLookup = {};
layer.valLookup = {};
layer.keys = [];
layer.values = [];
for (var i = 0; i < keysToKeep.length; i++) {
keepLookup[keys.indexOf(keysToKeep[i])] = true;
}
for (i = 0; i < layer.features.length; i++) {
var feature = layer.features[i];
var tags = feature.tags;
feature.tags = [];
for (var j = 0; j < tags.length; j += 2) {
if (keepLookup[tags[j]]) {
addKey(keys[tags[j]], layer.keyLookup, layer.keys, feature.tags);
addValue(values[tags[j + 1]], layer.valLookup, layer.values, feature.tags);
}
}
}
return layer;
}
function updateLayerProperties(layer, newProps) {
if (!newProps || (newProps.length !== layer.features.length)) {
throw new Error('The length of newProps array does not match the number of features');
}
if (!layer.keyLookup || !layer.valLookup) buildKeyValueLookups(layer);
for (var i = 0; i < layer.features.length; i++) {
var feature = layer.features[i];
for (var id in newProps[i]) {
addKey(id, layer.keyLookup, layer.keys, feature.tags);
addValue(newProps[i][id], layer.valLookup, layer.values, feature.tags);
}
}
return layer;
}
function mergeLayer(layer) {
layer.features.sort(compareTags);
var features = layer.features;
layer.features = [];
var lastFeature;
for (var i = 0; i < features.length; i++) {
if (!lastFeature || compareTags(features[i], lastFeature) !== 0) {
layer.features.push(features[i]);
lastFeature = features[i];
} else {
var geom = features[i].geometry;
for (var j = 0; j < geom.length; j++) {
lastFeature.geometry.push(geom[j]);
}
}
}
for (i = 0; i < layer.features.length; i++) {
var feature = layer.features[i];
if (feature.type === 2) { // lines
feature.geometry.sort(compareLines);
feature.geometry = mergeLines(feature.geometry);
}
}
return layer;
}
function mergeLines(geom) {
var starts = {};
var ends = {};
var newGeom = [];
for (var i = 0; i < geom.length; i++) {
var ring = geom[i];
var len = ring.length;
var startKey = zOrder(ring[0], ring[1]);
var endKey = zOrder(ring[len - 2], ring[len - 1]);
var endMatch = ends[startKey];
var startMatch = starts[endKey];
var matched = false;
var prev, next, j;
if (endMatch) { // found line that ends where current start
prev = zOrder(endMatch[endMatch.length - 4], endMatch[endMatch.length - 3]);
next = zOrder(ring[2], ring[3]);
if (prev !== next) {
for (j = 2; j < len; j++) ends[startKey].push(ring[j]);
ends[endKey] = ends[startKey];
delete ends[startKey];
matched = true;
}
}
if (startMatch) { // found line that starts where current ends
next = zOrder(startMatch[2], startMatch[3]);
prev = zOrder(ring[len - 4], ring[len - 3]);
if (next !== prev) {
for (j = len - 3; j >= 0; j--) starts[endKey].unshift(ring[j]);
starts[startKey] = starts[endKey];
delete starts[endKey];
matched = true;
}
}
if (!matched) {
starts[startKey] = ring;
ends[endKey] = ring;
newGeom.push(ring);
}
}
return newGeom;
}
function addKey(key, keyLookup, keys, tags) {
var keyTag = keyLookup[key];
if (keyTag === undefined) {
keyTag = keys.length;
keyLookup[key] = keys.length;
keys.push(key);
}
tags.push(keyTag);
}
function getValueKey(val) {
var valType = typeof val;
return valType !== 'number' ? valType + ':' + val : val;
}
function addValue(val, valLookup, values, tags) {
var valKey = getValueKey(val);
var valTag = valLookup[valKey];
if (valTag === undefined) {
valTag = values.length;
valLookup[valKey] = values.length;
values.push(val);
}
tags.push(valTag);
}
function compareTags(a, b) {
if (a.type !== b.type) return a.type - b.type;
var tags1 = a.tags;
var tags2 = b.tags;
if (!tags1 && tags2) return -1;
if (tags1 && !tags2) return 1;
if (!tags1 && !tags2) return 0;
if (tags1.length < tags2.length) return -1;
if (tags1.length > tags2.length) return 1;
for (var i = 0; i < tags1.length; i++) {
var d = tags1[i] - tags2[i];
if (d !== 0) return d;
}
return 0;
}
function compareLines(a, b) {
return zOrder(a[0], a[1]) - zOrder(b[0], b[1]);
}
function zOrder(x, y) {
x = (x | (x << 8)) & 0x00FF00FF;
x = (x | (x << 4)) & 0x0F0F0F0F;
x = (x | (x << 2)) & 0x33333333;
x = (x | (x << 1)) & 0x55555555;
y = (y | (y << 8)) & 0x00FF00FF;
y = (y | (y << 4)) & 0x0F0F0F0F;
y = (y | (y << 2)) & 0x33333333;
y = (y | (y << 1)) & 0x55555555;
return x | (y << 1);
}
function hasAllKeys(tags, requiredLookup, requiredCount) {
var found = 0;
for (var i = 0; i < tags.length; i += 2) {
if (requiredLookup[tags[i]]) found++;
}
return found === requiredCount;
}
function buildKeyValueLookups(layer) {
layer.keyLookup = {};
layer.valLookup = {};
for (var k = 0; k < layer.keys.length; k++) {
layer.keyLookup[layer.keys[k]] = k;
}
for (var v = 0; v < layer.values.length; v++) {
var val = layer.values[v];
layer.valLookup[getValueKey(val)] = v;
}
}