-
Notifications
You must be signed in to change notification settings - Fork 38
/
index.js
179 lines (158 loc) · 4.61 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
var Pbf = require('pbf')
var GeoJSONWrapper = require('./lib/geojson_wrapper')
module.exports = fromVectorTileJs
module.exports.fromVectorTileJs = fromVectorTileJs
module.exports.fromGeojsonVt = fromGeojsonVt
module.exports.GeoJSONWrapper = GeoJSONWrapper
/**
* Serialize a vector-tile-js-created tile to pbf
*
* @param {Object} tile
* @return {Buffer} uncompressed, pbf-serialized tile data
*/
function fromVectorTileJs (tile) {
var out = new Pbf()
writeTile(tile, out)
return out.finish()
}
/**
* Serialized a geojson-vt-created tile to pbf.
*
* @param {Object} layers - An object mapping layer names to geojson-vt-created vector tile objects
* @param {Object} [options] - An object specifying the vector-tile specification version and extent that were used to create `layers`.
* @param {Number} [options.version=1] - Version of vector-tile spec used
* @param {Number} [options.extent=4096] - Extent of the vector tile
* @return {Buffer} uncompressed, pbf-serialized tile data
*/
function fromGeojsonVt (layers, options) {
options = options || {}
var l = {}
for (var k in layers) {
l[k] = new GeoJSONWrapper(layers[k].features, options)
l[k].name = k
l[k].version = options.version
l[k].extent = options.extent
}
return fromVectorTileJs({ layers: l })
}
function writeTile (tile, pbf) {
for (var key in tile.layers) {
pbf.writeMessage(3, writeLayer, tile.layers[key])
}
}
function writeLayer (layer, pbf) {
pbf.writeVarintField(15, layer.version || 1)
pbf.writeStringField(1, layer.name || '')
pbf.writeVarintField(5, layer.extent || 4096)
var i
var context = {
keys: [],
values: [],
keycache: {},
valuecache: {}
}
for (i = 0; i < layer.length; i++) {
context.feature = layer.feature(i)
pbf.writeMessage(2, writeFeature, context)
}
var keys = context.keys
for (i = 0; i < keys.length; i++) {
pbf.writeStringField(3, keys[i])
}
var values = context.values
for (i = 0; i < values.length; i++) {
pbf.writeMessage(4, writeValue, values[i])
}
}
function writeFeature (context, pbf) {
var feature = context.feature
if (feature.id !== undefined) {
pbf.writeVarintField(1, feature.id)
}
pbf.writeMessage(2, writeProperties, context)
pbf.writeVarintField(3, feature.type)
pbf.writeMessage(4, writeGeometry, feature)
}
function writeProperties (context, pbf) {
var feature = context.feature
var keys = context.keys
var values = context.values
var keycache = context.keycache
var valuecache = context.valuecache
for (var key in feature.properties) {
var value = feature.properties[key]
var keyIndex = keycache[key]
if (value === null) continue // don't encode null value properties
if (typeof keyIndex === 'undefined') {
keys.push(key)
keyIndex = keys.length - 1
keycache[key] = keyIndex
}
pbf.writeVarint(keyIndex)
var type = typeof value
if (type !== 'string' && type !== 'boolean' && type !== 'number') {
value = JSON.stringify(value)
}
var valueKey = type + ':' + value
var valueIndex = valuecache[valueKey]
if (typeof valueIndex === 'undefined') {
values.push(value)
valueIndex = values.length - 1
valuecache[valueKey] = valueIndex
}
pbf.writeVarint(valueIndex)
}
}
function command (cmd, length) {
return (length << 3) + (cmd & 0x7)
}
function zigzag (num) {
return (num << 1) ^ (num >> 31)
}
function writeGeometry (feature, pbf) {
var geometry = feature.loadGeometry()
var type = feature.type
var x = 0
var y = 0
var rings = geometry.length
for (var r = 0; r < rings; r++) {
var ring = geometry[r]
var count = 1
if (type === 1) {
count = ring.length
}
pbf.writeVarint(command(1, count)) // moveto
// do not write polygon closing path as lineto
var lineCount = type === 3 ? ring.length - 1 : ring.length
for (var i = 0; i < lineCount; i++) {
if (i === 1 && type !== 1) {
pbf.writeVarint(command(2, lineCount - 1)) // lineto
}
var dx = ring[i].x - x
var dy = ring[i].y - y
pbf.writeVarint(zigzag(dx))
pbf.writeVarint(zigzag(dy))
x += dx
y += dy
}
if (type === 3) {
pbf.writeVarint(command(7, 1)) // closepath
}
}
}
function writeValue (value, pbf) {
var type = typeof value
if (type === 'string') {
pbf.writeStringField(1, value)
} else if (type === 'boolean') {
pbf.writeBooleanField(7, value)
} else if (type === 'number') {
if (value % 1 !== 0) {
pbf.writeDoubleField(3, value)
} else if (value < 0) {
pbf.writeSVarintField(6, value)
} else {
pbf.writeVarintField(5, value)
}
}
}