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

Posted

技术标签:

【中文标题】如何使用 Mapbox 的集群设置 reduce 选项?【英文标题】:How to set the reduce options with Mapbox's clusters? 【发布时间】:2018-11-02 04:14:23 【问题描述】:

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

我想自定义集群的属性(实际默认值为 point_count 和 point_count_abbrevied)。我的每个点(每个城市一个)都有一个表面属性(一个整数),我想在点聚集时对其求和。

我在mapbox's sources 中看到了对用于计算自定义属性的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 似乎没有发布这些接口 (see cluster's documentation),也没有提及 provided exemple:

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

【问题讨论】:

【参考方案1】:

它看起来就像一个普通的 reduce 一样工作。它将为每个点调用一个,并允许您使用该点的属性为整个集群创建属性。

所以如果你这样定义你的reduce;

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

那么集群上的sum 属性将是点上所有surface 属性的总和。

【讨论】:

我的问题可能不太清楚,我想知道如何设置这些方法,这些方法似乎不会被 Mapbox 的 API 暴露给用户。【参考方案2】:

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

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 示例来实现它。我刚刚问了一个与数据集相关的问题,如果你有时间看看:***.com/questions/51937339/… Mapbox 仍然没有集成超集群,因此我们只能使用 Mapbox 来实现。我也想使用 supercluster,但是我能找到的唯一一个 map/reduce 函数的例子是坏的(放大时)并且不包括 cmets,所以有点难以理解。当缩放级别足够高时,它也根本不显示未聚集的点。我在一个新问题中提到了所有这些。你介意看看吗:***.com/questions/52359737/…

以上是关于如何使用 Mapbox 的集群设置 reduce 选项?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 mapbox 中为 Geoserver 图层设置动画?

如何确定 Hadoop map和reduce的个数

在远程集群上使用 Hbase 运行 Map Reduce

特征层的 Mapbox 设置图标

hive执行作业时reduce任务个数设置为多少合适

如何在 MapBox(Z-Order)上的地图标记后面设置当前位置指示器?