supercluster: added initial,map,reduce to Options

This commit is contained in:
kl4kennylee81
2017-11-05 01:01:46 -07:00
committed by Kenneth Lee
parent 99f93266bb
commit b3f06feb45
2 changed files with 39 additions and 3 deletions

View File

@@ -33,6 +33,27 @@ export interface Options {
* Whether timing info should be logged.
*/
log?: boolean;
/**
* a reduce function for calculating custom cluster properties
*
* @example
* function (accumulated, props) { accumulated.sum += props.sum; }
*/
reduce?: (accumulated: any, props: any) => void;
/**
* initial properties of a cluster (before running the reducer)
*
* @example
* function () { return {sum: 0}; }
*/
initial?: () => any;
/**
* properties to use for individual points when running the reducer
*
* @example
* function (props) { return {sum: props.my_value}; }
*/
map?: (props: any) => any;
}
export class Supercluster {

View File

@@ -2,21 +2,36 @@ import supercluster, { Point } from 'supercluster';
const point1: Point = {
type: 'Feature',
properties: {},
properties: {my_value: 2},
geometry: { type: 'Point', coordinates: [10, 20] }
};
const point2: Point = {
type: 'Feature',
properties: {},
properties: {my_value: 3},
geometry: { type: 'Point', coordinates: [20, 30] }
};
const points = [point1, point2];
const initialFunction = () => {
return {sum: 0};
};
const mapFunction = (props: any) => {
return {sum: props.my_value};
};
const reduceFunction = (accumulated: any, props: any) => {
accumulated.sum += props.sum;
};
const index = supercluster({
radius: 40,
maxZoom: 16,
extent: 256,
log: true
log: true,
initial: initialFunction,
map: mapFunction,
reduce: reduceFunction,
});
index.load(points);
const clusters = index.getClusters([-180, -85, 180, 85], 2);