1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
| import Feature from 'ol/Feature.js'; import Map from 'ol/Map.js'; import View from 'ol/View.js'; import { DragAndDrop, defaults as defaultInteractions, } from 'ol/interaction.js'; import {GPX, GeoJSON, IGC, KML, MVT, TopoJSON} from 'ol/format.js'; import {OSM, Vector as VectorSource} from 'ol/source.js'; import {Tile as TileLayer, Vector as VectorLayer} from 'ol/layer.js'; import {createXYZ} from 'ol/tilegrid.js';
const tileCoordZ = document.getElementById('tileCoordZ'); const tileCoordX = document.getElementById('tileCoordX'); const tileCoordY = document.getElementById('tileCoordY');
class customMVT extends MVT { constructor() { super({featureClass: Feature}); } readFeatures(source, options) { options.extent = createXYZ().getTileCoordExtent([ parseInt(tileCoordZ.value), parseInt(tileCoordX.value), parseInt(tileCoordY.value), ]); return super.readFeatures(source, options); } }
const dragAndDropInteraction = new DragAndDrop({ formatConstructors: [customMVT, GPX, GeoJSON, IGC, KML, TopoJSON], });
const 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) { const vectorSource = new VectorSource({ features: event.features, }); map.addLayer( new VectorLayer({ source: vectorSource, }) ); map.getView().fit(vectorSource.getExtent()); });
const displayFeatureInfo = function (pixel) { const features = []; map.forEachFeatureAtPixel(pixel, function (feature) { features.push(feature); }); if (features.length > 0) { const info = []; let i, ii; for (i = 0, ii = features.length; i < ii; ++i) { const description = features[i].get('name') || features[i].get('_name') || features[i].get('layer'); if (description) { info.push(description); } } document.getElementById('info').innerHTML = info.join(', ') || ' '; } else { document.getElementById('info').innerHTML = ' '; } };
map.on('pointermove', function (evt) { if (evt.dragging) { return; } const pixel = map.getEventPixel(evt.originalEvent); displayFeatureInfo(pixel); });
map.on('click', function (evt) { displayFeatureInfo(evt.pixel); });
const link = document.getElementById('download');
function download(fullpath, filename) { fetch(fullpath) .then(function (response) { return response.blob(); }) .then(function (blob) { link.href = URL.createObjectURL(blob); link.download = filename; link.click(); }); }
document.getElementById('download-mvt').addEventListener('click', function () { const fullpath = 'https://basemaps.arcgis.com/arcgis/rest/services/World_Basemap_v2/VectorTileServer/tile/' + tileCoordZ.value + '/' + tileCoordY.value + '/' + tileCoordX.value + '.pbf'; const filename = tileCoordZ.value + '-' + tileCoordX.value + '-' + tileCoordY.value + '.mvt'; download(fullpath, filename); });
|