How to show count in heatmap instead of magnitude?

1.5k views Asked by At

I am trying to implement an openlayers heatmap example below:

https://openlayers.org/en/latest/examples/heatmap-earthquakes.html

Instead of magnitude is it possible to show count (how many points are in the same area)?

Edited: pointer count and show when zoom out:
pointer count and show when zoom out

1

There are 1 answers

8
Mike On BEST ANSWER

If you mean showing clustered features as a heatmap, it can be done. This is a combination of the Clustered Features example and the Heatmap example.

<!DOCTYPE html>
<html>
  <head>
    <title>Clustered Features</title>
    <link rel="stylesheet" href="https://openlayers.org/en/v4.6.5/css/ol.css" type="text/css">
    <!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
    <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
    <script src="https://openlayers.org/en/v4.6.5/build/ol.js"></script>
  </head>
  <body>
    <div id="map" class="map"></div>
    <form>
      <label>cluster distance</label>
      <input id="distance" type="range" min="0" max="100" step="1" value="40"/>
      <label>radius size</label>
      <input id="radius" type="range" min="1" max="50" step="1" value="5"/>
      <label>blur size</label>
      <input id="blur" type="range" min="1" max="50" step="1" value="15"/>
    </form>
    <script>
      var distance = document.getElementById('distance');
      var blur = document.getElementById('blur');
      var radius = document.getElementById('radius');

      var count = 20000;
      var features = new Array(count);
      var e = 4500000;
      for (var i = 0; i < count; ++i) {
        var coordinates = [2 * e * Math.random() - e, 2 * e * Math.random() - e];
        features[i] = new ol.Feature(new ol.geom.Point(coordinates));
      }

      var source = new ol.source.Vector({
        features: features
      });

      var clusterSource = new ol.source.Cluster({
        distance: parseInt(distance.value, 10),
        source: source
      });

      var styleCache = {};
      var vector = new ol.layer.Heatmap({
        source: clusterSource,
        weight: function(feature) { return feature.get('features').length/1000; },
        blur: parseInt(blur.value, 10),
        radius: parseInt(radius.value, 10)
      });

      var raster = new ol.layer.Tile({
        source: new ol.source.OSM()
      });

      var map = new ol.Map({
        layers: [raster, vector],
        target: 'map',
        view: new ol.View({
          center: [0, 0],
          zoom: 2
        })
      });

      distance.addEventListener('input', function() {
        clusterSource.setDistance(parseInt(distance.value, 10));
      });

      blur.addEventListener('input', function() {
        vector.setBlur(parseInt(blur.value, 10));
      });

      radius.addEventListener('input', function() {
        vector.setRadius(parseInt(radius.value, 10));
      });

    </script>
  </body>
</html>

This version shows a normal cluster layer at zoom levels 0, 1 and 2 and a heatmap at zoom levels 3 or more

<!DOCTYPE html>
<html>
  <head>
    <title>Clustered Features</title>
    <link rel="stylesheet" href="https://openlayers.org/en/v4.6.5/css/ol.css" type="text/css">
    <!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
    <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
    <script src="https://openlayers.org/en/v4.6.5/build/ol.js"></script>
  </head>
  <body>
    <div id="map" class="map"></div>
    <form>
      <label>cluster distance</label>
      <input id="distance" type="range" min="0" max="100" step="1" value="60"/>
      <label>radius size</label>
      <input id="radius" type="range" min="1" max="50" step="1" value="40"/>
      <label>blur size</label>
      <input id="blur" type="range" min="1" max="50" step="1" value="15"/>
    </form>
    <script>
      var distance = document.getElementById('distance');
      var blur = document.getElementById('blur');
      var radius = document.getElementById('radius');

      var count = 20000;
      var features = new Array(count);
      var e = 4500000;
      for (var i = 0; i < count; ++i) {
        var coordinates = [2 * e * Math.random() - e, 2 * e * Math.random() - e];
        features[i] = new ol.Feature(new ol.geom.Point(coordinates));
      }

      var source = new ol.source.Vector({
        features: features
      });

      var clusterSource = new ol.source.Cluster({
        distance: parseInt(distance.value, 10),
        source: source
      });

      var styleCache = {};
      var clusters = new ol.layer.Vector({
        source: clusterSource,
        style: function(feature) {
          var size = feature.get('features').length;
          var style = styleCache[size];
          if (!style) {
            style = new ol.style.Style({
              image: new ol.style.Circle({
                radius: 15,
                stroke: new ol.style.Stroke({
                  color: '#fff'
                }),
                fill: new ol.style.Fill({
                  color: '#3399CC'
                })
              }),
              text: new ol.style.Text({
                text: size.toString(),
                fill: new ol.style.Fill({
                  color: '#fff'
                })
              })
            });
            styleCache[size] = style;
          }
          return style;
        },
        minResolution: ol.tilegrid.createXYZ().getResolution(3) + 1
      });

      var vector = new ol.layer.Heatmap({
        source: clusterSource,
        weight: function(feature) { return feature.get('features').length/500; },
        blur: parseInt(blur.value, 10),
        radius: parseInt(radius.value, 10),
        maxResolution: ol.tilegrid.createXYZ().getResolution(3) + 1
      });

      var raster = new ol.layer.Tile({
        source: new ol.source.OSM()
      });

      var map = new ol.Map({
        layers: [raster, clusters, vector],
        target: 'map',
        view: new ol.View({
          center: [0, 0],
          zoom: 3
        })
      });

      distance.addEventListener('input', function() {
        clusterSource.setDistance(parseInt(distance.value, 10));
      });

      blur.addEventListener('input', function() {
        vector.setBlur(parseInt(blur.value, 10));
      });

      radius.addEventListener('input', function() {
        vector.setRadius(parseInt(radius.value, 10));
      });

    </script>
  </body>
</html>

If you data is random you could find the current largest cluster size and use that when dividing to calculate the weight:

    weight: function(feature) { 
       var maxSize = 0;
       clusterSource.forEachFeature( function(feature) { maxSize = Math.max(maxSize, feature.get('features').length); } );
       return feature.get('features').length/maxSize;
    },