如何使用Mapbox的聚类设置减少选项?

15

我正在使用Mabpox-gl-js v0.45对集群进行测试。

我想要自定义我的集群属性(实际默认值为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}; },
    },

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

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)
});
2个回答

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)
}

这看起来很有趣。不过,我在尝试用简单的geojson示例实现它时遇到了困难。我刚刚发布了一个相关数据集的问题,如果你有时间可以看一下:https://stackoverflow.com/questions/51937339/mapbox-clusters-data-driven-styling - Lucien S.
Mapbox仍然没有集成supercluster,因此我们只能使用Mapbox。我也想使用supercluster,但我能找到的唯一一个map/reduce函数的示例是有问题的(当缩放时)并且没有包括注释,所以有点难以理解。当缩放级别足够高时,它也不会显示未聚类的点。我在一个新的问题中提到了所有这些。你介意看一下吗: https://stackoverflow.com/questions/52359737/data-driven-cluster-colour-with-mapboxgl?noredirect=1#comment91840406_52359737 - LBes

0

看起来它的工作方式类似于常规的reduce。它将为每个点调用一次,并允许您使用点的属性创建聚类整体的属性。

因此,如果您像这样定义您的reduce;

supercluster({
  reduce: (clusterProps, pointProps) => {
    clusterProps.sum += pointProps.surface;
  }
});

然后,集群上的sum属性将是所有点上surface属性的总和。


我可能在我的问题上不太清楚,我想知道如何设置这些方法,这些方法似乎没有被Mapbox的API公开。 - Fractaliste

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接