File size: 5,060 Bytes
4b876a7 | 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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | import config from '../config';
export default class PointReducer{
constructor(map, binTableData, valueIdx, statsForAllPoints){
this._map = map;
this._binTableData = binTableData;
this._valueIdx = valueIdx;
this._statsForAllPoints = statsForAllPoints;
this._stats = {min: Infinity, max: -Infinity, data: [], sum: 0, mean: undefined, sd: undefined};
this._latIdx = binTableData.indices.latitude;
this._lngIdx = binTableData.indices.longitude;
this._dateIdx = binTableData.indices.date;
this._bounds = {};
this._pointsInBbox = [];
this._pointsInWorld = [];
this._pointCount = 0;
this._reducedPoints = [];
this._adjustedMinMax = undefined;
this.main();
}
main(){
this.calculateBounds();
if (this._statsForAllPoints){
this.getAllPoints();
this.calculateStatistics();
this.filterByBbox();
} else {
this.getPointsInBbox();
this.calculateStatistics();
}
this.reducePoints();
delete this._map;
delete this._binTableData;
delete this._bounds;
// delete this._pointsInBbox;
delete this._pointsInWorld;
}
calculateBounds(){
const mapBounds = this._map.getBounds();
const latMin = mapBounds.getSouth() < -85.06 ? -85.06 : mapBounds.getSouth();
const latMax = mapBounds.getNorth() > 85.06 ? 85.06 : mapBounds.getNorth();
const lngMin = mapBounds.getWest();
const lngMax = mapBounds.getEast();
this._bounds = {latMin, latMax, lngMin, lngMax};
}
getAllPoints(){
this._pointsInWorld = this._binTableData.allData.reduce((acc, curr, originalIdx) => {
if (!isNaN(curr[this._valueIdx])){
this._stats.min = Math.min(this._stats.min, curr[this._valueIdx]);
this._stats.max = Math.max(this._stats.max, curr[this._valueIdx]);
this._stats.sum += curr[this._valueIdx];
this._stats.data.push(curr[this._valueIdx]);
// We need the original index for communication with the graph
acc.push(curr.concat([originalIdx]));
}
return acc;
}, []);
this._pointCount = this._pointsInWorld.length;
}
filterByBbox(){
this._pointsInBbox = this._pointsInWorld.reduce((acc, curr) => {
if (this.isInside(curr)) acc.push(curr);
return acc;
}, []);
}
isInside(point){
return point[this._latIdx] >= this._bounds.latMin
&& point[this._latIdx] <= this._bounds.latMax
&& point[this._lngIdx] >= this._bounds.lngMin
&& point[this._lngIdx] <= this._bounds.lngMax;
}
getPointsInBbox(){
this._pointsInBbox = this._binTableData.allData.reduce((acc, curr, originalIdx) => {
if (this.isInside(curr) && !isNaN(curr[this._valueIdx])){
this._stats.min = Math.min(this._stats.min, curr[this._valueIdx]);
this._stats.max = Math.max(this._stats.max, curr[this._valueIdx]);
this._stats.sum += curr[this._valueIdx];
this._stats.data.push(curr[this._valueIdx]);
// We need the original index for communication with the graph
acc.push(curr.concat([originalIdx]));
}
return acc;
}, []);
this._pointCount = this._pointsInBbox.length;
}
calculateStatistics(){
this._stats.pointCount = this._pointCount;
this._stats.mean = this._stats.sum / this._pointCount;
const sqrdSum = this._stats.data.reduce((acc, curr) => {
acc += Math.pow(curr - this._stats.mean, 2);
return acc;
}, 0);
this._stats.sd = Math.sqrt(sqrdSum / this._stats.data.length);
delete this._stats.sum;
delete this._stats.data;
if (this._statsForAllPoints) {
const outliers = [];
const outlierThreshold = Math.abs(this._stats.sd * config.outlierSDFactor);
this._pointsInWorld.forEach((p, idx) => {
const otherIdx = idx > 0 ? idx - 1 : idx + 1;
const span = Math.abs(this._pointsInWorld[otherIdx][this._valueIdx] - p[this._valueIdx]);
if (span > outlierThreshold && Math.abs(p[this._valueIdx]) < outlierThreshold) {
outliers.push({idx, values: p});
}
});
if (outliers.length) {
const dataArr = this._pointsInWorld.map(p => p[this._valueIdx]);
outliers.reverse().forEach(outlier => dataArr.splice(outlier.idx, 1));
dataArr.sort();
this._adjustedMinMax = {min: dataArr[0], max: dataArr[dataArr.length - 1]};
}
}
}
reducePoints(){
if (this._pointsInBbox.length <= config.pointsInMapThreshHold){
this._reducedPoints = this._pointsInBbox;
return;
}
const factor = Math.ceil(this._pointsInBbox.length / config.pointsInMapThreshHold);
this._reducedPoints = this._pointsInBbox.filter((p, idx) => {
const direction = idx > 0 ? -1 : 1;
const span = Math.abs(this._pointsInBbox[idx + direction][this._valueIdx] - p[this._valueIdx]);
return idx % factor === 0 || span > this._stats.sd * config.percentSD;
});
}
get pointCount(){
return this._pointCount;
}
get pointCountInBbox(){
return this._pointsInBbox.length;
}
get reducedPoints(){
return this._reducedPoints;
}
get valueIdx(){
return this._valueIdx;
}
get latIdx(){
return this._latIdx;
}
get lngIdx(){
return this._lngIdx;
}
get dateIdx(){
return this._dateIdx;
}
get stats(){
return this._stats;
}
get adjustedMinMax(){
return this._adjustedMinMax;
}
} |