From 6e9cf0fd9dad6b0571fc064d70e694f55d9bdc13 Mon Sep 17 00:00:00 2001 From: Christian Mayer Date: Wed, 21 Mar 2018 11:07:01 +0100 Subject: [PATCH 1/2] Add vector tile selection example --- examples/vector-tile-selection.html | 19 +++++++ examples/vector-tile-selection.js | 83 +++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 examples/vector-tile-selection.html create mode 100644 examples/vector-tile-selection.js diff --git a/examples/vector-tile-selection.html b/examples/vector-tile-selection.html new file mode 100644 index 0000000000..b856bb6543 --- /dev/null +++ b/examples/vector-tile-selection.html @@ -0,0 +1,19 @@ +--- +layout: example.html +title: Vector Tile Selection +shortdesc: Select features from vector tiles. +docs: > +

+ Click a rendered vector-tile feature to highlight it on the map. Click on an empty spot (ocean) to reset the selection. + By changing the action type to "Multi Select" you can select multiple features at a time. +

+tags: "vector tiles, selection" +--- +
+
+ + +
diff --git a/examples/vector-tile-selection.js b/examples/vector-tile-selection.js new file mode 100644 index 0000000000..be099c5000 --- /dev/null +++ b/examples/vector-tile-selection.js @@ -0,0 +1,83 @@ +import Map from '../src/ol/Map.js'; +import View from '../src/ol/View.js'; +import MVT from '../src/ol/format/MVT.js'; +import VectorTileLayer from '../src/ol/layer/VectorTile.js'; +import VectorTileSource from '../src/ol/source/VectorTile.js'; +import Style from '../src/ol/style/Style.js'; +import Fill from '../src/ol/style/Fill.js'; +import Stroke from '../src/ol/style/Stroke.js'; + +// lookup for selection objects +let selection = {}; +// feature property to act as identifier +const idProp = 'iso_a3'; + +const vtLayer = new VectorTileLayer({ + declutter: true, + source: new VectorTileSource({ + format: new MVT(), + url: 'https://ahocevar.com/geoserver/gwc/service/tms/1.0.0/' + + 'ne:ne_10m_admin_0_countries@EPSG%3A900913@pbf/{z}/{x}/{-y}.pbf' + }), + style: function(feature) { + // normal style + let style = new Style({ + stroke: new Stroke({ + color: 'gray', + width: 1 + }), + fill: new Fill({ + color: 'rgba(20,20,20,0.9)' + }) + }); + if (selection[feature.get(idProp)]) { + // selection style + style = new Style({ + stroke: new Stroke({ + color: 'rgba(200,20,20,0.8)', + width: 2 + }), + fill: new Fill({ + color: 'rgba(200,20,20,0.2)' + }) + }); + } + return style; + } +}); + +const map = new Map({ + layers: [ + vtLayer + ], + target: 'map', + view: new View({ + center: [0, 0], + zoom: 2 + }) +}); + +const selectElement = document.getElementById('type'); + +map.on('click', updateSelection); + +function updateSelection(event) { + const features = map.getFeaturesAtPixel(event.pixel); + if (!features) { + selection = {}; + // force redraw of layer style + vtLayer.setStyle(vtLayer.getStyleFunction()); + return; + } + const feature = features[0]; + const fid = feature.get(idProp); + + if (selectElement.value === 'singleselect') { + selection = {}; + } + // add selected feature to lookup + selection[fid] = feature; + + // force redraw of layer style + vtLayer.setStyle(vtLayer.getStyleFunction()); +} From 467cf3ce5b1673ad657605536620e899bbe75a2c Mon Sep 17 00:00:00 2001 From: Andreas Hocevar Date: Wed, 21 Mar 2018 12:01:42 +0100 Subject: [PATCH 2/2] Improve readability and create only one style --- examples/vector-tile-selection.js | 33 +++++++++---------------------- 1 file changed, 9 insertions(+), 24 deletions(-) diff --git a/examples/vector-tile-selection.js b/examples/vector-tile-selection.js index be099c5000..2f2fa41a44 100644 --- a/examples/vector-tile-selection.js +++ b/examples/vector-tile-selection.js @@ -20,29 +20,16 @@ const vtLayer = new VectorTileLayer({ 'ne:ne_10m_admin_0_countries@EPSG%3A900913@pbf/{z}/{x}/{-y}.pbf' }), style: function(feature) { - // normal style - let style = new Style({ + const selected = !!selection[feature.get(idProp)]; + return new Style({ stroke: new Stroke({ - color: 'gray', - width: 1 + color: selected ? 'rgba(200,20,20,0.8)' : 'gray', + width: selected ? 2 : 1 }), fill: new Fill({ - color: 'rgba(20,20,20,0.9)' + color: selected ? 'rgba(200,20,20,0.2)' : 'rgba(20,20,20,0.9)' }) }); - if (selection[feature.get(idProp)]) { - // selection style - style = new Style({ - stroke: new Stroke({ - color: 'rgba(200,20,20,0.8)', - width: 2 - }), - fill: new Fill({ - color: 'rgba(200,20,20,0.2)' - }) - }); - } - return style; } }); @@ -59,14 +46,12 @@ const map = new Map({ const selectElement = document.getElementById('type'); -map.on('click', updateSelection); - -function updateSelection(event) { +map.on('click', function(event) { const features = map.getFeaturesAtPixel(event.pixel); if (!features) { selection = {}; // force redraw of layer style - vtLayer.setStyle(vtLayer.getStyleFunction()); + vtLayer.setStyle(vtLayer.getStyle()); return; } const feature = features[0]; @@ -79,5 +64,5 @@ function updateSelection(event) { selection[fid] = feature; // force redraw of layer style - vtLayer.setStyle(vtLayer.getStyleFunction()); -} + vtLayer.setStyle(vtLayer.getStyle()); +});