Edit

Custom Drag-and-Drop (MVT preview)

drag-and-drop3 mvt1


Tile coordinate    z:  x:  y:   

 

Example of using the drag-and-drop interaction with a custom format to preview MVT tiles.

Example of using the drag-and-drop interaction with a custom format to preview MVT tiles. In addition to the formats used in the Drag-and-Drop example individual MVT tiles can be previewed. There is no projection transform support, so this will only work with data in EPSG:4326 and EPSG:3857.

main.js
import 'ol/ol.css';
import Feature from 'ol/Feature';
import Map from 'ol/Map';
import View from 'ol/View';
import {
  DragAndDrop,
  defaults as defaultInteractions,
} from 'ol/interaction';
import {GPX, GeoJSON, IGC, KML, MVT, TopoJSON} from 'ol/format';
import {OSM, Vector as VectorSource} from 'ol/source';
import {Tile as TileLayer, Vector as VectorLayer} from 'ol/layer';
import {createXYZ} from 'ol/tilegrid';

// Define a custom MVT format as ol/format/MVT requires an extent

var tileCoordZ = document.getElementById('tileCoordZ');
var tileCoordX = document.getElementById('tileCoordX');
var tileCoordY = document.getElementById('tileCoordY');

var customMVT = /*@__PURE__*/(function (MVT) {
  function customMVT() {
    MVT.call(this, {featureClass: Feature});
  }

  if ( MVT ) customMVT.__proto__ = MVT;
  customMVT.prototype = Object.create( MVT && MVT.prototype );
  customMVT.prototype.constructor = customMVT;
  customMVT.prototype.readFeatures = function readFeatures (source, options) {
    options.extent = createXYZ().getTileCoordExtent([
      parseInt(tileCoordZ.value),
      parseInt(tileCoordX.value),
      parseInt(tileCoordY.value) ]);
    return MVT.prototype.readFeatures.call(this, source, options);
  };

  return customMVT;
}(MVT));

// Set up map with Drag and Drop interaction

var dragAndDropInteraction = new DragAndDrop({
  formatConstructors: [customMVT, GPX, GeoJSON, IGC, KML, TopoJSON],
});

var map = new Map({
  interactions: defaultInteractions().extend([dragAndDropInteraction]),
  layers: [
    new TileLayer({
      source: new OSM(),
    }) ],
  target: 'map',
  view: new View({
    center: [0, 0],
    zoom: 2,
  }),
});

dragAndDropInteraction.on('addfeatures', function (event) {
  var vectorSource = new VectorSource({
    features: event.features,
  });
  map.addLayer(
    new VectorLayer({
      source: vectorSource,
    })
  );
  map.getView().fit(vectorSource.getExtent());
});

var displayFeatureInfo = function (pixel) {
  var features = [];
  map.forEachFeatureAtPixel(pixel, function (feature) {
    features.push(feature);
  });
  if (features.length > 0) {
    var info = [];
    var i, ii;
    for (i = 0, ii = features.length; i < ii; ++i) {
      var description =
        features[i].get('name') ||
        features[i].get('_name') ||
        features[i].get('layer');
      if (description) {
        info.push(description);
      }
    }
    document.getElementById('info').innerHTML = info.join(', ') || '&nbsp';
  } else {
    document.getElementById('info').innerHTML = '&nbsp;';
  }
};

map.on('pointermove', function (evt) {
  if (evt.dragging) {
    return;
  }
  var pixel = map.getEventPixel(evt.originalEvent);
  displayFeatureInfo(pixel);
});

map.on('click', function (evt) {
  displayFeatureInfo(evt.pixel);
});

// Sample data download

var link = document.getElementById('download');

function download(fullpath, filename) {
  fetch(fullpath)
    .then(function (response) {
      return response.blob();
    })
    .then(function (blob) {
      if (navigator.msSaveBlob) {
        // link download attribuute does not work on MS browsers
        navigator.msSaveBlob(blob, filename);
      } else {
        link.href = URL.createObjectURL(blob);
        link.download = filename;
        link.click();
      }
    });
}

document.getElementById('download-mvt').addEventListener('click', function () {
  var fullpath =
    'https://basemaps.arcgis.com/v1/arcgis/rest/services/World_Basemap/VectorTileServer/tile/' +
    tileCoordZ.value +
    '/' +
    tileCoordY.value +
    '/' +
    tileCoordX.value +
    '.pbf';
  var filename =
    tileCoordZ.value + '-' + tileCoordX.value + '-' + tileCoordY.value + '.mvt';
  download(fullpath, filename);
});
index.html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Custom Drag-and-Drop (MVT preview)</title>
    <!-- Pointer events polyfill for old browsers, see https://caniuse.com/#feat=pointer -->
    <script src="https://unpkg.com/elm-pep"></script>
    <style>
      .map {
        width: 100%;
        height:400px;
      }
      .tileCoord input {
        width: 60px;
      }
    </style>
  </head>
  <body>
    <div id="map" class="map"></div>
    <br />
    <div class="tileCoord">
      <a id="download" download></a>
      <span>Tile coordinate&nbsp;&nbsp;</span>
      <span>&nbsp;z: <input type="number" id="tileCoordZ" value="6" /></span>
      <span>&nbsp;x: <input type="number" id="tileCoordX" value="30" /></span>
      <span>&nbsp;y: <input type="number" id="tileCoordY" value="20" /></span>
      <span>&nbsp;&nbsp;</span>
      <button id="download-mvt">Download sample</button>
    </div>
    <br />
    <div id="info">&nbsp;</div>
    <script src="main.js"></script>
  </body>
</html>
package.json
{
  "name": "drag-and-drop-custom-mvt",
  "dependencies": {
    "ol": "6.5.0"
  },
  "devDependencies": {
    "parcel": "^2.0.0-beta.1"
  },
  "scripts": {
    "start": "parcel index.html",
    "build": "parcel build --public-url . index.html"
  }
}