0

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

Zoe
  • 27,060
  • 21
  • 118
  • 148
user10496245
  • 217
  • 3
  • 17

1 Answers1

2

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;
    },
Mike
  • 16,042
  • 2
  • 14
  • 30
  • Hi @Mike, thank you so much for your reply. I am using clustered features same as your example but want to show points counter in the same area when zoom out, I have added an image as an example.Please check my edited portion. – user10496245 Nov 04 '18 at 08:40
  • You would need two layers using clusterSource, a heatmap as above and a normal vector layer with a style function as in the openlayers example for clustered features. Specify a maxResolution in the heatmap options and the same value as minResolution for the other layer. – Mike Nov 04 '18 at 10:09
  • Could you please update your code to reflect the same as you are saying, I will accept your answer. – user10496245 Nov 04 '18 at 11:14
  • I have implemented your code but when I add minResolution for cluster layer and maxResolution for heatmap layer only heatmap layer works not the clusters layer with count style, but when I remove minResolution from clusters then the count down style works but the heatmap doesn't work, what could be the reason of happening such thing? – user10496245 Nov 05 '18 at 06:24
  • I have found the problem I have view set such way: center: maxZoom: 19, zoom: 8, minZoom: 3, and I also need these value set so what need to do in this case? – user10496245 Nov 05 '18 at 08:02
  • Hi @Mike, if you browse this example: https://openlayers.org/en/latest/examples/heatmap-earthquakes.html and zoom in each point with blue color, is it possible to do the same using your code? – user10496245 Nov 06 '18 at 08:11
  • Weight has to be between 0 and 1. The the OL example they subtract 5 from the quake magnitude so 5.1 is always 0.1 weight. In my examples I have divided cluser size by 500 or 1000 because the biggest clusters were about that size but when you zoom in the clusters reduce to 1 so the weight will only be 0.001 or 0.002. There's no reason why you couldn't replace the dividing number with something configurable or one calculated based on zoom level or resolution. – Mike Nov 06 '18 at 09:48
  • Hi @Mike, in my case the size of points come randomly depending on search criteria so I don't know by which number I can divide. I have used for weight here: weight: function(feature) { return feature.get('features').length/2; } – user10496245 Nov 06 '18 at 10:03