From 0cfdfdd2409b58739ed8af507dd636c5bf78c917 Mon Sep 17 00:00:00 2001 From: vershwal Date: Thu, 31 May 2018 18:14:23 +0530 Subject: [PATCH] Cleaning code: a separate function vtToGeoJson, support .mvt and .pbf --- data/core.yaml | 2 +- dist/locales/en.json | 2 +- modules/svg/mvt.js | 50 +++++++++++++++++++++++--------------------- 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/data/core.yaml b/data/core.yaml index d401c3c45..2665b0cdb 100644 --- a/data/core.yaml +++ b/data/core.yaml @@ -595,7 +595,7 @@ en: report: Report a privacy concern with this image mvt: local_layer: "Add a MVT" - drag_drop: "Drag and drop a .mvt or .geojson file on the page, or click the button to the right to browse" + drag_drop: "Drag and drop a .mvt, .pbf or .geojson file on the page, or click the button to the right to browse" zoom: "Zoom to layer" browse: "Browse for a file" mapillary_images: diff --git a/dist/locales/en.json b/dist/locales/en.json index c1e5a8432..9cac81463 100644 --- a/dist/locales/en.json +++ b/dist/locales/en.json @@ -722,7 +722,7 @@ }, "mvt": { "local_layer": "Add a MVT", - "drag_drop": "Drag and drop a .mvt or .geojson file on the page, or click the button to the right to browse", + "drag_drop": "Drag and drop a .mvt, .pbf or .geojson file on the page, or click the button to the right to browse", "zoom": "Zoom to layer", "browse": "Browse for a file" }, diff --git a/modules/svg/mvt.js b/modules/svg/mvt.js index 666e160da..ee037e0dc 100644 --- a/modules/svg/mvt.js +++ b/modules/svg/mvt.js @@ -128,8 +128,25 @@ export function svgMvt(projection, context, dispatch) { } - function toDom(x) { - return (new DOMParser()).parseFromString(x, 'text/xml'); + function vtToGeoJson(x) { + var tile = new vt.VectorTile(new Protobuf(x)); + var layers = Object.keys(tile.layers); + if (!Array.isArray(layers)) + layers = [layers] + + var collection = {type: 'FeatureCollection', features: []}; + + layers.forEach(function (layerID) { + var layer = tile.layers[layerID]; + if (layer) { + for (var i = 0; i < layer.length; i++) { + var feature = layer.feature(i).toGeoJSON(0, 1, 2); + if (layers.length > 1) feature.properties.vt_layer = layerID; + collection.features.push(feature); + } + } + }); + return collection; } @@ -148,30 +165,15 @@ export function svgMvt(projection, context, dispatch) { function parseSaveAndZoom(extension, data) { - var tile = new vt.VectorTile(new Protobuf(data)); - var layers = Object.keys(tile.layers); - if (!Array.isArray(layers)) - layers = [layers] - - var collection = {type: 'FeatureCollection', features: []}; - - layers.forEach(function (layerID) { - var layer = tile.layers[layerID]; - if (layer) { - for (var i = 0; i < layer.length; i++) { - var feature = layer.feature(i).toGeoJSON(150, 194, 9); - if (layers.length > 1) feature.properties.vt_layer = layerID; - collection.features.push(feature); - } - } - }); - switch (extension) { - /*default: - drawMvt.geojson(tile.toGeoJSON(x,y,z)).fitZoom(); - break;*/ + default: + drawMvt.geojson(JSON.parse(data)).fitZoom(); + break; case '.pbf': - drawMvt.geojson(collection).fitZoom(); + drawMvt.geojson(vtToGeoJson(data)).fitZoom(); + break; + case '.mvt': + drawMvt.geojson(vtToGeoJson(data)).fitZoom(); break; case '.geojson': case '.json':