forked from developmentseed/geojson-polygon-aggregate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reducers.js
75 lines (62 loc) · 1.79 KB
/
reducers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
var area = require('turf-area')
var uniq = require('uniq')
module.exports.count = function () {
return function (c) { return (c || 0) + 1 }
}
/*
* Return an aggregation that collects the unique, primitive values of the given
* property into a (stringified) array. If the property value is a stringified
* array, it is unpacked--i.e., the array contents are collected rather than the
* array itself.
*/
module.exports.union = function (property) {
function collect (memo, feature) {
memo = (memo || [])
if (!(property in feature.properties)) { return memo }
var value
try {
value = JSON.parse(feature.properties[property])
} catch (e) {
value = feature.properties[property]
}
if (Array.isArray(value)) {
memo.push.apply(memo, value)
} else {
memo.push(value)
}
return memo
}
collect.finish = function (memo) {
return memo ? JSON.stringify(uniq(memo, false, false)) : '[]'
}
return collect
}
module.exports.totalArea = function () {
return function (a, feature) {
return (a || 0) + area(feature)
}
}
module.exports.sum = function (property) {
return function (s, feature) {
return (s || 0) + (feature.properties[property] || 0)
}
}
module.exports.areaWeightedSum = function (property) {
return function (s, feature) {
return (s || 0) + area(feature) * (feature.properties[property] || 0)
}
}
module.exports.areaWeightedMean = function (property) {
var ws = module.exports.areaWeightedSum(property)
var ta = module.exports.totalArea()
function weightedMean (memo, feature) {
memo = memo || {}
memo.sum = ws(memo.sum, feature)
memo.area = ta(memo.area, feature)
return memo
}
weightedMean.finish = function (memo) {
return memo ? memo.sum / memo.area : 0
}
return weightedMean
}