Cleaning code: a separate function vtToGeoJson, support .mvt and .pbf

This commit is contained in:
vershwal
2018-05-31 18:14:23 +05:30
parent 652d2ed65f
commit 0cfdfdd240
3 changed files with 28 additions and 26 deletions
+1 -1
View File
@@ -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:
+1 -1
View File
@@ -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"
},
+26 -24
View File
@@ -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':