Skip to content

Commit 84bc485

Browse files
committed
fix: Use proper length for number of total cells in area encoder
1 parent 26d399d commit 84bc485

File tree

2 files changed

+8
-13
lines changed

2 files changed

+8
-13
lines changed

encode.js

+7-12
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module.exports = function (item, deps) {
2323
var labelLen = getLabelLen(item.tags)
2424
var buf = Buffer.alloc(9 + typeLen + idLen + labelLen)
2525
var offset = 0
26-
buf.writeUInt8(0x01, offset)
26+
buf.writeUInt8(0x01, offset)
2727
offset+=1
2828
varint.encode(type, buf, offset)
2929
offset+=varint.encode.bytes
@@ -47,16 +47,11 @@ module.exports = function (item, deps) {
4747
coords.push(deps[ref].lat)
4848
})
4949
var cells = earcut(coords)
50-
var coords = []
51-
item.refs.forEach(function (ref) {
52-
coords.push(deps[ref].lon)
53-
coords.push(deps[ref].lat)
54-
})
55-
var cells = earcut(coords)
56-
var cLen = varint.encodingLength(earcut.length/3)
50+
var cLen = varint.encodingLength(cells.length/3)
51+
var cLenData = cells.reduce((acc, cell) => acc + varint.encodingLength(cell), 0)
5752
var labelLen = getLabelLen(item.tags)
5853
var buf = Buffer.alloc(1 + typeLen + idLen + pCount + cLen + n*4*2
59-
+ (n-2)*3*2 + labelLen)
54+
+ cLenData + labelLen)
6055
var offset = 0
6156
buf.writeUInt8(0x03, 0)
6257
offset+=1
@@ -74,8 +69,8 @@ module.exports = function (item, deps) {
7469
})
7570
varint.encode(cells.length/3, buf, offset)
7671
offset+=varint.encode.bytes
77-
cells.forEach(function (item) {
78-
varint.encode(item, buf, offset)
72+
cells.forEach(function (cell) {
73+
varint.encode(cell, buf, offset)
7974
offset+=varint.encode.bytes
8075
})
8176
writeLabelData(item.tags, buf, offset)
@@ -117,7 +112,7 @@ function getLabelLen (tags) {
117112
if (!/^([^:]+_|)name($|:)/.test(key)) { return }
118113
var pre = key.replace(/^(|[^:]+_)name($|:)/,'')
119114
var dataLen = Buffer.byteLength(pre) + 1
120-
+ Buffer.byteLength(tags[key])
115+
+ Buffer.byteLength(tags[key])
121116
labelLen += varint.encodingLength(dataLen) + dataLen
122117
})
123118
return labelLen

example/encode.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var fs = require('fs')
22
var through = require('through2')
33
var parseOSM = require('osm-pbf-parser')
44
var georenderPack = require('../encode.js')
5-
5+
66
var osm = parseOSM()
77
var allItems = {}
88
var itemsRefsObject = {}

0 commit comments

Comments
 (0)