forked from mapbox/geobuf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.js
174 lines (141 loc) · 4.9 KB
/
decode.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
'use strict';
module.exports = decode;
var keys, values, lengths, dim, e;
var geometryTypes = [
'Point', 'MultiPoint', 'LineString', 'MultiLineString',
'Polygon', 'MultiPolygon', 'GeometryCollection'];
function decode(pbf) {
dim = 2;
e = Math.pow(10, 6);
lengths = null;
keys = [];
values = [];
var 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 = Math.pow(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';
var 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) {
var end = pbf.readVarint() + pbf.pos,
value = null;
while (pbf.pos < end) {
var val = pbf.readVarint(),
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;
}
function readProps(pbf, props) {
var end = pbf.readVarint() + pbf.pos;
while (pbf.pos < end) props[keys[pbf.readVarint()]] = values[pbf.readVarint()];
values = [];
return props;
}
function readPoint(pbf) {
var end = pbf.readVarint() + pbf.pos,
coords = [];
while (pbf.pos < end) coords.push(pbf.readSVarint() / e);
return coords;
}
function readLinePart(pbf, end, len, closed) {
var i = 0,
coords = [],
p, d;
var prevP = [];
for (d = 0; d < dim; d++) prevP[d] = 0;
while (len ? i < len : pbf.pos < end) {
p = [];
for (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) {
var end = pbf.readVarint() + pbf.pos;
if (!lengths) return [readLinePart(pbf, end, null, closed)];
var coords = [];
for (var i = 0; i < lengths.length; i++) coords.push(readLinePart(pbf, end, lengths[i], closed));
lengths = null;
return coords;
}
function readMultiPolygon(pbf) {
var end = pbf.readVarint() + pbf.pos;
if (!lengths) return [[readLinePart(pbf, end, null, true)]];
var coords = [];
var j = 1;
for (var i = 0; i < lengths[0]; i++) {
var rings = [];
for (var 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;
}