Skip to content

Commit

Permalink
mvt: Add result.byteLength (#862)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibgreen committed Jul 17, 2020
1 parent eb691ed commit 073c877
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
15 changes: 11 additions & 4 deletions modules/mvt/src/lib/parse-mvt.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import {transformCoordinates, transformToLocalCoordinates} from './transform-to-
* @param {arrayBuffer} _ A MVT arrayBuffer
* @return {?Object} A GeoJSON geometry object
*/
export default function parseMVT(input, options) {
export default function parseMVT(arrayBuffer, options) {
options = options || {};
options.mvt = options.mvt || {};

if (input.byteLength === 0) {
if (arrayBuffer.byteLength === 0) {
return [];
}

const tile = new VectorTile(new Protobuf(input));
const tile = new VectorTile(new Protobuf(arrayBuffer));
const loaderOptions = options.mvt;
const features = [];

Expand All @@ -41,7 +41,14 @@ export default function parseMVT(input, options) {
}
});

return options.mvt._format === 'binary' ? geojsonToBinary(features) : features;
if (options.mvt._format === 'binary') {
const data = geojsonToBinary(features);
// TODO decide where to store this
// @ts-ignore
data.byteLength = arrayBuffer.byteLength;
return data;
}
return features;
}

function getDecodedFeature(feature, options = {}) {
Expand Down
6 changes: 4 additions & 2 deletions modules/mvt/test/mvt-loader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,10 @@ test('Polygon MVT to local coordinates binary', async t => {
const response = await fetchFile(MVT_POLYGONS_DATA_URL);
const mvtArrayBuffer = await response.arrayBuffer();

const geometryJSON = await parse(mvtArrayBuffer, MVTLoader, {mvt: {_format: 'binary'}});
t.deepEqual(geometryJSON, geojsonToBinary(decodedPolygonsGeometry));
const geometryBinary = await parse(mvtArrayBuffer, MVTLoader, {mvt: {_format: 'binary'}});
t.ok(geometryBinary.byteLength > 0);
delete geometryBinary.byteLength;
t.deepEqual(geometryBinary, geojsonToBinary(decodedPolygonsGeometry));

t.end();
});

0 comments on commit 073c877

Please sign in to comment.