2
votes

So i am currently clustering a grid of points on my map with help from the Mapbox example here: Create and style clusters

The example code from Mapbox counts the number of points and displays it like this - using the variable point_count_abbreviated:

map.addLayer({
      id: "cluster-count",
      ...        
      layout: {
        "text-field": "{point_count_abbreviated}",
         },
       ...
    })

My geoJSON source looks something like this:

  {
  "type": "Feature",
  "geometry": { ... },
  "properties": {
    "location": { ... },
    "id": 111,
    ...
    "value": {
      "count": 2
    }
  }
}

What I am trying to do is to add together all the "properties.value.count", and display the sum on each cluster. I have tried the examples in the documentation about clusterProperties but since my count variable is nested inside the value object, I am having a hard time making it work.

So I guess something like this in the cluster source definition:

clusterProperties: {"sum": ["+", ["object", "value", ['get, 'count']]]}

And display it in the layer:

layout: {
            "text-field": ['get', 'sum'],
             },

Could someone please point me in the right direction? :)

2

2 Answers

2
votes

I managed to make this work. The logic goes like this: get count variable inside value-object inside properties-object. Properties object is a reserved expression described here.

In definition of source:

clusterProperties: {
  sum: ["+", ["get", "count", ["get", "value", ["properties"]]]],
},

In the layer that displays the sum:

"text-field": ["get", "sum"],
0
votes

Thanks for sharing this method. I want to add one detail for them who are working with converted (CSV to GeoJSON) data. I spent hours to find solution for converting numeric field to integers. All data was converted as a string and there is no information in csv2geojson's official web page how to convert it. Finally I added -> numericFields: 'customers', option and it worked for me.

  function makeGeoJSON(csvData) {
    csv2geojson.csv2geojson(csvData, {
      latfield: 'Latitude',
      lonfield: 'Longitude',          
      delimiter: ',',
      numericFields: 'customers',
      cluster: true,
      clusterMaxZoom: 14,
      clusterRadius: 50,

    }, function (err, data) {
      map.on('load', function () {
        map.addSource('geopoints', {
          type: 'geojson',
          data: data,
          cluster: true,
          clusterMaxZoom: 14,
          clusterRadius: 50, 
          clusterProperties: {
            sum: ["+", ["get", "customers", ["properties"]]],
          },
        });