Improving Fires publish
This commit is contained in:
parent
c7b78e76ad
commit
ccf0716d3e
3 changed files with 40 additions and 81 deletions
|
|
@ -1,12 +1,15 @@
|
||||||
|
/* global Counter */
|
||||||
|
/* eslint-disable import/no-absolute-path */
|
||||||
|
|
||||||
import { Meteor } from 'meteor/meteor';
|
import { Meteor } from 'meteor/meteor';
|
||||||
import { check, Match } from 'meteor/check';
|
import { check, Match } from 'meteor/check';
|
||||||
|
import IPGeocoder from '/imports/startup/server/IPGeocoder';
|
||||||
import ActiveFires from '../ActiveFires';
|
import ActiveFires from '../ActiveFires';
|
||||||
|
|
||||||
var counter = new Counter('countActiveFires', ActiveFires.find({}));
|
const counter = new Counter('countActiveFires', ActiveFires.find({}));
|
||||||
|
|
||||||
Meteor.publish('activefirestotal', function() {
|
|
||||||
return counter;
|
Meteor.publish('activefirestotal', () => counter);
|
||||||
});
|
|
||||||
|
|
||||||
const validZoom = Match.Where((zoom) => {
|
const validZoom = Match.Where((zoom) => {
|
||||||
// http://wiki.openstreetmap.org/wiki/Zoom_levels
|
// http://wiki.openstreetmap.org/wiki/Zoom_levels
|
||||||
|
|
@ -15,74 +18,80 @@ const validZoom = Match.Where((zoom) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
// https://github.com/3stack-software/meteor-match-library
|
// https://github.com/3stack-software/meteor-match-library
|
||||||
var NumberBetween = function (min, max) {
|
function NumberBetween(min, max) {
|
||||||
return Match.Where(function (x) {
|
return Match.Where((x) => {
|
||||||
check(x, Number);
|
check(x, Number);
|
||||||
return min <= x && x <= max;
|
return min <= x && x <= max;
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
var NullOr = function (type) {
|
function NullOr(type) {
|
||||||
return Match.Where(function (x) {
|
return Match.Where((x) => {
|
||||||
if (x === null) {
|
if (x === null) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
check(x, type);
|
check(x, type);
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
// http://wiki.openstreetmap.org/wiki/Zoom_levels
|
// http://wiki.openstreetmap.org/wiki/Zoom_levels
|
||||||
// http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Resolution_and_Scale
|
// http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Resolution_and_Scale
|
||||||
const zoomMetersPerPixel = [156412, 78206, 39103, 19551, 9776, 4888, 2444, 1222, 610.984, 305.492, 152.746, 76.373, 38.187, 19.093, 9.547, 4.773, 2.387, 1.193, 0.596, 0.298];
|
|
||||||
|
|
||||||
// http://cwestblog.com/2012/11/12/javascript-degree-and-radian-conversion/
|
// http://cwestblog.com/2012/11/12/javascript-degree-and-radian-conversion/
|
||||||
Math.radians = function(degrees) {
|
Math.radians = (degrees) => {
|
||||||
return degrees * Math.PI / 180;
|
const rad = (degrees * Math.PI) / 180;
|
||||||
|
return rad;
|
||||||
};
|
};
|
||||||
|
|
||||||
var activefires = function(zoom, lat, lng, height, width) {
|
const activefires = (zoom, lat, lng, height, width) => {
|
||||||
// latitude -90 and 90 and the longitude between -180 and 180
|
// latitude -90 and 90 and the longitude between -180 and 180
|
||||||
check(lat, NumberBetween(-90, 90));
|
check(lat, NumberBetween(-90, 90));
|
||||||
check(lng, NumberBetween(-180, 180));
|
check(lng, NumberBetween(-180, 180));
|
||||||
|
|
||||||
// console.log("Zoom: " + zoom + " lat: " + lat + " lng: " + lng);
|
// console.log("Zoom: " + zoom + " lat: " + lat + " lng: " + lng);
|
||||||
var resolution = 156543.03 * Math.cos(Math.radians(lat)) / Math.pow(2,zoom);
|
const resolution = (156543.03 * Math.cos(Math.radians(lat))) / (2 ** zoom);
|
||||||
// console.log(`Meters per pixel ${zoomMetersPerPixel[zoom]}, resolution ${resolution} meters x pixel`);
|
const distUnt = resolution * Math.max(height, width);
|
||||||
var distance = Math.trunc(Math.max(resolution*height, resolution*width));
|
const distance = Math.trunc(distUnt);
|
||||||
// console.log(`so ${height}x${width} gives ${Math.trunc(resolution*height/1000)} x ${Math.trunc(resolution*width/1000)} km, so looking in ${distance}`);
|
// console.log(`so ${height}x${width} gives ${Math.trunc(resolution*height/1000)} x ${Math.trunc(resolution*width/1000)} km, so looking in ${distance}`);
|
||||||
|
console.log(`so ${height}x${width} gives ${resolution} of resolution, so looking in ${distance}`);
|
||||||
|
|
||||||
// console.log(geoData);
|
|
||||||
return ActiveFires.find({
|
return ActiveFires.find({
|
||||||
ourid: {
|
ourid: {
|
||||||
$near : {
|
$near: {
|
||||||
$geometry: {
|
$geometry: {
|
||||||
type: "Point",
|
type: 'Point',
|
||||||
coordinates: [ lng, lat]
|
coordinates: [lng, lat]
|
||||||
},
|
},
|
||||||
$minDistance: 0,
|
$minDistance: 0,
|
||||||
$maxDistance: distance
|
$maxDistance: distance
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}, {
|
||||||
|
fields: {
|
||||||
|
lat: 1,
|
||||||
|
lon: 1,
|
||||||
|
scan: 1
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
Meteor.publish('activefiresmyloc', function(zoom, lat, lng, height, width) {
|
Meteor.publish('activefiresmyloc', (zoom, lat, lng, height, width) => {
|
||||||
check(zoom, validZoom);
|
check(zoom, validZoom);
|
||||||
check(lat, NullOr(Number));
|
check(lat, NullOr(Number));
|
||||||
check(lng, NullOr(Number));
|
check(lng, NullOr(Number));
|
||||||
check(height, NullOr(Number));
|
check(height, NullOr(Number));
|
||||||
check(width, NullOr(Number));
|
check(width, NullOr(Number));
|
||||||
console.log(`Check active fires in ${lat},${lng} with zoom ${zoom} pixels in ${height}x${width} map`)
|
console.log(`Check active fires in ${lat},${lng} with zoom ${zoom} pixels in ${height}x${width} map`);
|
||||||
if (lat === null || lng === null) {
|
if (lat === null || lng === null) {
|
||||||
var clientIP = this.connection.clientAddress;
|
let clientIP = this.connection.clientAddress;
|
||||||
if (clientIP === '127.0.0.1') {
|
if (clientIP === '127.0.0.1') {
|
||||||
clientIP = '80.58.61.250' // Some Spain IP address
|
clientIP = '80.58.61.250'; // Some Spain IP address
|
||||||
}
|
}
|
||||||
var geoData = IPGeocoder.geocode(clientIP);
|
// https://www.npmjs.com/package/maxmind
|
||||||
var location = geoData.location;
|
const location = IPGeocoder.get(clientIP);
|
||||||
lat = location.latitude;
|
console.log(location);
|
||||||
lng = location.longitude;
|
return activefires(zoom, location.latitude, location.longitude, height, width);
|
||||||
}
|
}
|
||||||
return activefires(zoom, lat, lng, height, width);
|
return activefires(zoom, lat, lng, height, width);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,50 +0,0 @@
|
||||||
import { Meteor } from 'meteor/meteor';
|
|
||||||
import maxmind from 'maxmind';
|
|
||||||
|
|
||||||
|
|
||||||
// https://stackoverflow.com/questions/13969655/how-do-you-check-whether-the-given-ip-is-internal-or-not
|
|
||||||
function isPrivateIP(ip) {
|
|
||||||
var parts = ip.split('.');
|
|
||||||
return parts[0] === '10' ||
|
|
||||||
parts[0] === '127' ||
|
|
||||||
(parts[0] === '172' && (parseInt(parts[1], 10) >= 16 && parseInt(parts[1], 10) <= 31)) ||
|
|
||||||
(parts[0] === '192' && parts[1] === '168');
|
|
||||||
}
|
|
||||||
|
|
||||||
Meteor.methods({
|
|
||||||
geo: function() {
|
|
||||||
|
|
||||||
// https://stackoverflow.com/questions/14843232/how-to-get-the-user-ip-address-in-meteor-server/22657421#22657421
|
|
||||||
var clientIP = this.connection.clientAddress;
|
|
||||||
|
|
||||||
if (isPrivateIP(clientIP)) {
|
|
||||||
clientIP = '80.58.61.250' // Some Spain IP address
|
|
||||||
}
|
|
||||||
// console.log(`Geolocating ${clientIP}`);
|
|
||||||
|
|
||||||
// https://developers.google.com/web/fundamentals/primers/promises
|
|
||||||
var promise = new Promise(function(resolve, reject) {
|
|
||||||
// do a thing, possibly async, then…
|
|
||||||
// TODO: cron download GeoLite-City
|
|
||||||
// http://dev.maxmind.com/geoip/geoip2/geolite2/
|
|
||||||
maxmind.open(process.env.PWD + '/private/GeoLite2-City.mmdb', (err, cityLookup) => {
|
|
||||||
if (err) {
|
|
||||||
reject(console.error(`Failed to geolite ${clientIP}, ${err}`));
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
var city = cityLookup.get(clientIP);
|
|
||||||
// console.log(city);
|
|
||||||
resolve(city);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
})
|
|
||||||
return promise.await();
|
|
||||||
},
|
|
||||||
getMapKey: function () {
|
|
||||||
// http://meteorpedia.com/read/Environment_Variables
|
|
||||||
// https://developers.google.com/maps/documentation/javascript/get-api-key
|
|
||||||
// https://console.developers.google.com/
|
|
||||||
// export GMAPS_KEY=SomeGMapsKey
|
|
||||||
return process.env.GMAPS_KEY || Meteor.settings.gmaps.key;;
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
@ -3,4 +3,4 @@ import './accounts';
|
||||||
import './api';
|
import './api';
|
||||||
import './fixtures';
|
import './fixtures';
|
||||||
import './email';
|
import './email';
|
||||||
import './geolocation';
|
import './IPGeocoder';
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue