Skip to content

Instantly share code, notes, and snippets.

@harllos
Created March 15, 2019 00:06
Show Gist options
  • Save harllos/0ea2883bee30b43b83fcf098e8ef9dbd to your computer and use it in GitHub Desktop.
Save harllos/0ea2883bee30b43b83fcf098e8ef9dbd to your computer and use it in GitHub Desktop.
//Supercluster with property aggregation
 var cluster = supercluster({
 radius: clusterRadius,
 maxZoom: clusterMaxZoom,
 initial: function() {
 return {
 count: 0,
 sum: 0,
 min: Infinity,
 max: -Infinity
 };
 },
 map: function(properties) {
 return {
 count: 1,
 sum: Number(properties[propertyToAggregate]),
 min: Number(properties[propertyToAggregate]),
 max: Number(properties[propertyToAggregate])
 };
 },
 reduce: function(accumulated, properties) {
 accumulated.sum += Math.round(properties.sum * 100) / 100;
 accumulated.count += properties.count;
 accumulated.min = Math.round(Math.min(accumulated.min, properties.min) * 100) / 100;
 accumulated.max = Math.round(Math.max(accumulated.max, properties.max) * 100) / 100;
 accumulated.avg = Math.round(100 * accumulated.sum / accumulated.count) / 100;
 }
 });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment