diff --git a/lib/vectortilefeature.js b/lib/vectortilefeature.js index b9880a1..1cb7cc6 100644 --- a/lib/vectortilefeature.js +++ b/lib/vectortilefeature.js @@ -38,6 +38,63 @@ function readTag(pbf, feature) { VectorTileFeature.types = ['Unknown', 'Point', 'LineString', 'Polygon']; +VectorTileFeature.prototype.getFlatGeometryAndHoles = function(indexX, indexY) { + var pbf = this._pbf; + pbf.pos = this._geometry; + + var tileX = this.extent * indexX, + tileY = this.extent * indexY; + + var end = pbf.readVarint() + pbf.pos, + cmd = 1, + length = 0, + x = 0, + y = 0, + ringFirstPosition = [], + positions = [], + holeIndices = []; + + while (pbf.pos < end) { + if (length <= 0) { + var cmdLen = pbf.readVarint(); + cmd = cmdLen & 0x7; + length = cmdLen >> 3; + } + + length--; + + if (cmd === 1 || cmd === 2) { + x += pbf.readSVarint(); + y += pbf.readSVarint(); + + if (cmd === 1) { // moveTo + // Starting or new hole + // if (line) lines.push(line); + // line = []; + ringFirstPosition = [x + tileX, y + tileY]; + } + + positions.push(x + tileX); + positions.push(y + tileY); + + } else if (cmd === 7) { + positions.push(ringFirstPosition[0]); // closePolygon + positions.push(ringFirstPosition[1]); // closePolygon + holeIndices.push(positions.length); + } else { + throw new Error('unknown command ' + cmd); + } + } + + holeIndices.pop(); + + // return lines; + return { + positions: positions, + holeIndices: holeIndices + }; +}; + VectorTileFeature.prototype.loadGeometry = function() { var pbf = this._pbf; pbf.pos = this._geometry; @@ -126,6 +183,21 @@ VectorTileFeature.prototype.bbox = function() { return [x1, y1, x2, y2]; }; +VectorTileFeature.prototype.toUnprojectedJSON = function(x, y) { + var type = VectorTileFeature.types[this.type]; + + var geometry = this.getFlatGeometryAndHoles(x, y); + + var geometryValue = { type: type }; + Object.assign(geometryValue, geometry); + + return { + type: "Feature", + geometry: geometryValue, + properties: this.properties + } +}; + VectorTileFeature.prototype.toGeoJSON = function(x, y, z) { var size = this.extent * Math.pow(2, z), x0 = this.extent * x,