todos-contra-el-fuego-web/imports/api/ActiveFires/server/publications.js
2018-02-17 08:43:01 +01:00

84 lines
2.3 KiB
JavaScript

/* global Counter */
/* eslint-disable import/no-absolute-path */
/* eslint-disable prefer-arrow-callback */
import { Meteor } from 'meteor/meteor';
import { check } from 'meteor/check';
import L from 'leaflet-headless';
import { NumberBetween } from '/imports/modules/server/other-checks';
import FalsePositives from '/imports/api/FalsePositives/FalsePositives';
import calcUnion from '/imports/ui/components/Maps/SubsUnion/Unify';
import ActiveFires from '../ActiveFires';
const counter = new Counter('countActiveFires', ActiveFires.find({}));
Meteor.publish('activefirestotal', function total() {
return counter;
});
const falsePositives = (fires) => {
const falsePos = FalsePositives.find({
geo: {
$geoWithin: {
$geometry: fires.geometry
}
}
}, {
fields: {
geo: 1,
// type: 1,
// when: 1,
fireId: 1
}
});
/* console.log(`False positive total: ${falsePos.count()}`);
console.log(`False positives: ${JSON.stringify(falsePos.fetch())}`); */
return falsePos;
};
const activefires = (northEastLng, northEastLat, southWestLng, southWestLat, withMarks) => {
const fires = ActiveFires.find({
ourid: {
$geoWithin: {
$box: [
[southWestLng, southWestLat],
[northEastLng, northEastLat]
]
}
}
}, {
fields: {
lat: 1,
lon: 1,
when: 1,
scan: 1
}
});
// console.log(JSON.stringify(result));
// console.log(`Fires total: ${fires.count()}`);
if (withMarks && fires.fetch().length > 0) {
const group = new L.FeatureGroup();
const remap = fires.fetch().map(function remap(doc) {
return { location: { lat: doc.lat, lon: doc.lon }, distance: doc.scan };
});
const result = calcUnion(remap, group, sub => sub);
const falsePos = falsePositives(result[0]);
return [fires, falsePos];
}
return fires;
};
Meteor.publish('activefiresmyloc', function activeInMyLoc(northEastLng, northEastLat, southWestLng, southWestLat, withMarks) {
// latitude -90 and 90 and the longitude between -180 and 180
check(northEastLng, NumberBetween(-180, 180));
check(southWestLat, NumberBetween(-90, 90));
check(southWestLng, NumberBetween(-180, 180));
check(northEastLat, NumberBetween(-90, 90));
check(withMarks, Boolean);
return activefires(northEastLng, northEastLat, southWestLng, southWestLat, withMarks);
});