Skip to content

描述 ​

归约 GeoJSON 要素中的聚类,类似于 Array.reduce()

参数 ​

名称类型描述
geojsonFeatureCollectionGeoJSON 要素集
propertystring | number用于创建聚类的 GeoJSON 属性键值
callbackclusterReduceCallback接收 (previousValue, cluster, clusterValue, currentIndex) 的方法
initialValue?any作为回调函数第一个参数首次调用的值。

返回值 ​

any 归约结果值。

示例 ​

javascript
var geojson = turf.featureCollection([
  turf.point([0, 0]),
  turf.point([2, 4]),
  turf.point([3, 6]),
  turf.point([5, 1]),
  turf.point([4, 2]),
]);

// 使用 K-Means 创建聚类(向 GeoJSON 属性添加 `cluster`)
var clustered = turf.clustersKmeans(geojson);

// 遍历每个聚类并执行计算
var initialValue = 0;
turf.clusterReduce(
  clustered,
  "cluster",
  function (previousValue, cluster, clusterValue, currentIndex) {
    //=previousValue
    //=cluster
    //=clusterValue
    //=currentIndex
    return previousValue++;
  },
  initialValue,
);

// 计算聚类总数
var total = turf.clusterReduce(
  clustered,
  "cluster",
  function (previousValue) {
    return previousValue++;
  },
  0,
);

// 创建包含从 'cluster' 属性检索的所有值的数组
var values = turf.clusterReduce(
  clustered,
  "cluster",
  function (previousValue, cluster, clusterValue) {
    return previousValue.concat(clusterValue);
  },
  [],
);

安装 ​

javascript
$ npm install @turf/clusters

import { clusterReduce } from "@turf/clusters";
const result = clusterReduce(...);
javascript
$ npm install @turf/turf

import * as turf from "@turf/turf";
const result = turf.clusterReduce(...);