代码之家  ›  专栏  ›  技术社区  ›  Fractaliste

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

  •  12
  • Fractaliste  · 技术社区  · 6 年前

    我在用Mabbox GL JS v0.45进行POC集群。

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

    我明白了 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)
    });
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   Fractaliste    6 年前

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

    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)
    }
    
        2
  •  0
  •   Jivings    6 年前

    它看起来像是一个规则的减量。它将为每个点调用一个,并允许您使用点的属性为集群整体创建属性。

    所以如果你这样定义你的减量;

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

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