Edit

Trajectories Heatmap

heatmap2 geojson10 vector82 trajectory1 webgl18

Demonstrates the use of a heatmap layer with linear geometries.

This example shows linear geometries rendered using a ol/layer/Heatmap layer.
Data is AIS maritime traffic around Gothenburg transformed into GeoJSON, taken from this example.

main.js
import Map from 'ol/Map.js';
import View from 'ol/View.js';
import GeoJSON from 'ol/format/GeoJSON.js';
import HeatmapLayer from 'ol/layer/Heatmap.js';
import TileLayer from 'ol/layer/Tile.js';
import {fromLonLat} from 'ol/proj.js';
import StadiaMaps from 'ol/source/StadiaMaps.js';
import VectorSource from 'ol/source/Vector.js';

const blur = document.getElementById('blur');
const radius = document.getElementById('radius');

const heatmap = new HeatmapLayer({
  source: new VectorSource({
    url: 'data/geojson/ship-trajectories.json',
    format: new GeoJSON(),
    attributions: 'Danish Maritime Authority',
  }),
  blur: ['/', ['var', 'blur'], 2],
  radius: ['/', ['var', 'radius'], 2],
  variables: {
    blur: parseInt(blur.value, 10),
    radius: parseInt(radius.value, 10),
    shipType: 'All',
  },
  filter: [
    'any',
    ['==', ['var', 'shipType'], 'All'],
    ['==', ['var', 'shipType'], ['get', 'ShipType']],
  ],
  weight: () => 0.1,
});

const raster = new TileLayer({
  source: new StadiaMaps({
    layer: 'alidade_smooth_dark',
  }),
});

const map = new Map({
  layers: [raster, heatmap /*vector*/],
  target: 'map',
  view: new View({
    center: fromLonLat([11.86, 57.67]),
    zoom: 12,
  }),
});

blur.addEventListener('input', function () {
  heatmap.updateStyleVariables({blur: parseInt(blur.value, 10)});
});

radius.addEventListener('input', function () {
  heatmap.updateStyleVariables({radius: parseInt(radius.value, 10)});
});

const shipTypeSelect = document.getElementById('shiptype-filter');
shipTypeSelect.addEventListener('input', function () {
  heatmap.updateStyleVariables({shipType: shipTypeSelect.value});
  map.render();
});
index.html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Trajectories Heatmap</title>
    <link rel="stylesheet" href="node_modules/ol/ol.css">
    <style>
      .map {
        width: 100%;
        height: 400px;
      }
    </style>
  </head>
  <body>
    <div id="map" class="map"></div>
    <form>
      <label for="radius">radius size</label>
      <input id="radius" type="range" min="1" max="50" step="1" value="5"/>
      <label for="blur">blur size</label>
      <input id="blur" type="range" min="1" max="50" step="1" value="15"/>
      <label for="shiptype-filter">Filter by type of ship:</label>
      <select id="shiptype-filter">
          <option>All</option>
          <option>Passenger</option>
          <option>HSC</option>
          <option>Tanker</option>
          <option>Cargo</option>
          <option>Sailing</option>
          <option>Other</option>
          <option>Tug</option>
          <option>SAR</option>
          <option>Pleasure</option>
          <option>Dredging</option>
          <option>Law enforcement</option>
          <option>Pilot</option>
          <option>Fishing</option>
          <option>Diving</option>
          <option>Spare 2</option>
          <option>Undefined</option>
      </select>
    </form>

    <script type="module" src="main.js"></script>
  </body>
</html>
package.json
{
  "name": "heatmap-trajectories",
  "dependencies": {
    "ol": "10.5.0"
  },
  "devDependencies": {
    "vite": "^3.2.3"
  },
  "scripts": {
    "start": "vite",
    "build": "vite build"
  }
}