Skip to main content
Version: 7.2.0

clusterReduce

描述

¥Description

减少 GeoJSON 要素中的聚类,类似于 Array.reduce()

¥Reduce clusters in GeoJSON Features, similar to Array.reduce()

参数

¥Parameters

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

返回

¥Returns

任意 缩减后的值。

¥any The value that results from the reduction.

示例

¥Examples

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

// Create a cluster using K-Means (adds `cluster` to GeoJSON properties)
var clustered = turf.clustersKmeans(geojson);

// Iterate over each cluster and perform a calculation
var initialValue = 0;
turf.clusterReduce(
clustered,
"cluster",
function (previousValue, cluster, clusterValue, currentIndex) {
//=previousValue
//=cluster
//=clusterValue
//=currentIndex
return previousValue++;
},
initialValue,
);

// Calculate the total number of clusters
var total = turf.clusterReduce(
clustered,
"cluster",
function (previousValue) {
return previousValue++;
},
0,
);

// Create an Array of all the values retrieved from the 'cluster' property
var values = turf.clusterReduce(
clustered,
"cluster",
function (previousValue, cluster, clusterValue) {
return previousValue.concat(clusterValue);
},
[],
);

安装

¥Installation

$ npm install @turf/clusters

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

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