如何使用Mapbox的集群设置reduce选项?

Fra*_*ste 13 javascript mapbox

我正在使用Mabpox-gl-js v0.45对聚类进行POC.

我想自定义我的集群的属性(实际默认值是point_count和point_count_abbreviated).我的每个点(每个城市一个)都有一个表面属性(整数),我希望在点聚集时求和.

我在mapbox的源代码中看到了一个reduce函数的引用来计算自定义属性:

SuperCluster.prototype = {
    options: {
        minZoom: 0,   // min zoom to generate clusters on
        // .....
        log: false,   // whether to log timing info

        // a reduce function for calculating custom cluster properties
        reduce: null, // function (accumulated, props) { accumulated.sum += props.sum; }

        // initial properties of a cluster (before running the reducer)
        initial: function () { return {}; }, // function () { return {sum: 0}; },

        // properties to use for individual points when running the reducer
        map: function (props) { return props; } // function (props) { return {sum: props.my_value}; },
    },
Run Code Online (Sandbox Code Playgroud)

但我在文档中没有看到任何关于它的提及.我该如何设置这些选项?

Mapbox似乎不会发布这些接口(请参阅集群的文档),并且没有提及提供的示例:

map.addSource("earthquakes", {
    type: "geojson",
    // Point to GeoJSON data. This example visualizes all M1.0+ earthquakes
    // from 12/22/15 to 1/21/16 as logged by USGS' Earthquake hazards program.
    data: "/mapbox-gl-js/assets/earthquakes.geojson",
    cluster: true,
    clusterMaxZoom: 14, // Max zoom to cluster points on
    clusterRadius: 50 // Radius of each cluster when clustering points (defaults to 50)
});
Run Code Online (Sandbox Code Playgroud)

Fra*_*ste 3

有人给了我一个解决方法:不要使用嵌入式超集群,而是创建自己的超集群并将其用作源:

var myCluster = supercluster({
    radius: 40,
    maxZoom: 16,
    reduce: function(p) { /* I can use reduce/map/... functions! */ }
});

// My clustered source is created without Mapbox's clusters since I managed my clusters outside Mapbox library.
map.addSource("earthquakes", {
    type: "geojson",
    data : {
      "type" : "FeatureCollection",
      "features" : []
    }
});

function loadRemoteGeoJson() {
    var features
    // Do what you want, when you want to retrieve remote features...
    // ...
    // In the end set features into your supercluster
    myCluster.load(features)
    pushClusterIntoMapbox(map)
}

// Function to call when you load remote data AND when you zoom in or out !
function pushClusterIntoMapbox(map) {
  // I maybe should be bounded here...
  var clusters = myCluster.getClusters([ -180.0000, -90.0000, 180.0000, 90.0000 ], Math
      .floor(map.getZoom()))

  // My colleague advice me to use http://turfjs.org as helper but I think it's quite optionnal
  var features = turf.featureCollection(clusters)
  map.getSource("earthquakes").setData(features)
}
Run Code Online (Sandbox Code Playgroud)