-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathdecode.js
More file actions
173 lines (141 loc) · 4.99 KB
/
Copy pathdecode.js
File metadata and controls
173 lines (141 loc) · 4.99 KB
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
let keys, values, lengths, dim, e;
const geometryTypes = [
'Point', 'MultiPoint', 'LineString', 'MultiLineString',
'Polygon', 'MultiPolygon', 'GeometryCollection'];
export function decode(pbf) {
dim = 2;
e = 10 ** 6;
lengths = null;
keys = [];
values = [];
const obj = pbf.readFields(readDataField, {});
keys = null;
return obj;
}
function readDataField(tag, obj, pbf) {
if (tag === 1) keys.push(pbf.readString());
else if (tag === 2) dim = pbf.readVarint();
else if (tag === 3) e = 10 ** pbf.readVarint();
else if (tag === 4) readFeatureCollection(pbf, obj);
else if (tag === 5) readFeature(pbf, obj);
else if (tag === 6) readGeometry(pbf, obj);
}
function readFeatureCollection(pbf, obj) {
obj.type = 'FeatureCollection';
obj.features = [];
return pbf.readMessage(readFeatureCollectionField, obj);
}
function readFeature(pbf, feature) {
feature.type = 'Feature';
const f = pbf.readMessage(readFeatureField, feature);
if (!('geometry' in f)) f.geometry = null;
return f;
}
function readGeometry(pbf, geom) {
geom.type = 'Point';
return pbf.readMessage(readGeometryField, geom);
}
function readFeatureCollectionField(tag, obj, pbf) {
if (tag === 1) obj.features.push(readFeature(pbf, {}));
else if (tag === 13) values.push(readValue(pbf));
else if (tag === 15) readProps(pbf, obj);
}
function readFeatureField(tag, feature, pbf) {
if (tag === 1) feature.geometry = readGeometry(pbf, {});
else if (tag === 11) feature.id = pbf.readString();
else if (tag === 12) feature.id = pbf.readSVarint();
else if (tag === 13) values.push(readValue(pbf));
else if (tag === 14) feature.properties = readProps(pbf, {});
else if (tag === 15) readProps(pbf, feature);
}
function readGeometryField(tag, geom, pbf) {
if (tag === 1) geom.type = geometryTypes[pbf.readVarint()];
else if (tag === 2) lengths = pbf.readPackedVarint();
else if (tag === 3) readCoords(geom, pbf, geom.type);
else if (tag === 4) {
geom.geometries = geom.geometries || [];
geom.geometries.push(readGeometry(pbf, {}));
} else if (tag === 13) values.push(readValue(pbf));
else if (tag === 15) readProps(pbf, geom);
}
function readCoords(geom, pbf, type) {
if (type === 'Point') geom.coordinates = readPoint(pbf);
else if (type === 'MultiPoint') geom.coordinates = readLine(pbf, true);
else if (type === 'LineString') geom.coordinates = readLine(pbf);
else if (type === 'MultiLineString') geom.coordinates = readMultiLine(pbf);
else if (type === 'Polygon') geom.coordinates = readMultiLine(pbf, true);
else if (type === 'MultiPolygon') geom.coordinates = readMultiPolygon(pbf);
}
function readValue(pbf) {
const end = pbf.readVarint() + pbf.pos;
let value = null;
while (pbf.pos < end) {
const val = pbf.readVarint();
const tag = val >> 3;
if (tag === 1) value = pbf.readString();
else if (tag === 2) value = pbf.readDouble();
else if (tag === 3) value = pbf.readVarint();
else if (tag === 4) value = -pbf.readVarint();
else if (tag === 5) value = pbf.readBoolean();
else if (tag === 6) value = JSON.parse(pbf.readString());
}
return value;
}
const UNSAFE_KEYS = new Set(['__proto__', 'constructor', 'prototype']);
function readProps(pbf, props) {
const end = pbf.readVarint() + pbf.pos;
while (pbf.pos < end) {
const key = keys[pbf.readVarint()];
const val = values[pbf.readVarint()];
if (!UNSAFE_KEYS.has(key)) props[key] = val;
}
values = [];
return props;
}
function readPoint(pbf) {
const end = pbf.readVarint() + pbf.pos;
const coords = [];
while (pbf.pos < end) coords.push(pbf.readSVarint() / e);
return coords;
}
function readLinePart(pbf, end, len, closed) {
let i = 0;
const coords = [];
const prevP = new Array(dim).fill(0);
while (len ? i < len : pbf.pos < end) {
const p = [];
for (let d = 0; d < dim; d++) {
prevP[d] += pbf.readSVarint();
p[d] = prevP[d] / e;
}
coords.push(p);
i++;
}
if (closed) coords.push(coords[0]);
return coords;
}
function readLine(pbf) {
return readLinePart(pbf, pbf.readVarint() + pbf.pos);
}
function readMultiLine(pbf, closed) {
const end = pbf.readVarint() + pbf.pos;
if (!lengths) return [readLinePart(pbf, end, null, closed)];
const coords = [];
for (const len of lengths) coords.push(readLinePart(pbf, end, len, closed));
lengths = null;
return coords;
}
function readMultiPolygon(pbf) {
const end = pbf.readVarint() + pbf.pos;
if (!lengths) return [[readLinePart(pbf, end, null, true)]];
const coords = [];
let j = 1;
for (let i = 0; i < lengths[0]; i++) {
const rings = [];
for (let k = 0; k < lengths[j]; k++) rings.push(readLinePart(pbf, end, lengths[j + 1 + k], true));
j += lengths[j] + 1;
coords.push(rings);
}
lengths = null;
return coords;
}