deck.gl HexagonLayer Example

Demo

Source

<html>
  <head>
    <title>deck.gl HexagonLayer Example</title>

    <script src="https://unpkg.com/deck.gl@^9.0.0/dist.min.js"></script>
    <script src="https://d3js.org/d3.v5.min.js"></script>
    <script src='https://unpkg.com/maplibre-gl@3.6.0/dist/maplibre-gl.js'></script>

    <link href='https://unpkg.com/maplibre-gl@3.6.0/dist/maplibre-gl.css' rel='stylesheet' />
    <style type="text/css">
      body {
        font-family: Helvetica, Arial, sans-serif;
        width: 100vw;
        height: 100vh;
        margin: 0;
      }

      #control-panel {
        position: absolute;
        top: 0;
        left: 0;
        margin: 12px;
        padding: 20px;
        font-size: 12px;
        line-height: 1.5;
        z-index: 1;
        background: #fff;
        font-family: Helvetica, Arial, sans-serif;
        box-shadow: 0 0 4px rgba(0, 0, 0, 0.15);
      }

      label {
        display: inline-block;
        width: 140px;
      }
    </style>
  </head>

  <body>
    <div id="control-panel">
      <div>
        <label>Radius</label>
        <input id="radius" type="range" min="1000" max="20000" step="1000" value="1000"></input>
        <span id="radius-value"></span>
      </div>
      <div>
        <label>Coverage</label>
        <input id="coverage" type="range" min="0" max="1" step="0.1" value="1"></input>
        <span id="coverage-value"></span>
      </div>
      <div>
        <label>Upper Percentile</label>
        <input id="upperPercentile" type="range" min="90" max="100" step="1" value="100"></input>
        <span id="upperPercentile-value"></span>
      </div>
    </div>
  </body>

  <script type="text/javascript">

    const {DeckGL, HexagonLayer} = deck;

    const deckgl = new DeckGL({
      mapStyle: 'https://basemaps.cartocdn.com/gl/dark-matter-nolabels-gl-style/style.json',
      initialViewState: {
        longitude: -1.4157,
        latitude: 52.2324,
        zoom: 6,
        minZoom: 5,
        maxZoom: 15,
        pitch: 40.5
      },
      controller: true
    });

    let data = null;

    const OPTIONS = ['radius', 'coverage', 'upperPercentile'];

    const COLOR_RANGE = [
      [1, 152, 189],
      [73, 227, 206],
      [216, 254, 181],
      [254, 237, 177],
      [254, 173, 84],
      [209, 55, 78]
    ];

    OPTIONS.forEach(key => {
      document.getElementById(key).oninput = renderLayer;
    });

    function renderLayer () {
      const options = {};
      OPTIONS.forEach(key => {
        const value = document.getElementById(key).value;
        document.getElementById(key + '-value').innerHTML = value;
        options[key] = Number(value);
      });

      const hexagonLayer = new HexagonLayer({
        id: 'heatmap',
        colorRange: COLOR_RANGE,
        data,
        elevationRange: [0, 1000],
        elevationScale: 250,
        extruded: true,
        getPosition: d => d,
        ...options
      });

      deckgl.setProps({
        layers: [hexagonLayer]
      });
    }

    d3.csv('https://raw.githubusercontent.com/visgl/deck.gl-data/master/examples/3d-heatmap/heatmap-data.csv')
      .then(response => {
      data = response.map(d => [Number(d.lng), Number(d.lat)]);
      renderLayer();
    });

  </script>
</html>