isPointInCircle(lat, lng, latCenter, lngCenter, radius) { return geolib.isPointInCircle( {latitude: lat, longitude: lng}, {latitude: latCenter, longitude: lngCenter}, radius ); }
var results = _.reduce(sites,function(result,s){ if(s.coordinates){ var latlng = {latitude:s.coordinates[1],longitude:s.coordinates[0]}; if(geolib.isPointInCircle(latlng,center,radius)){ console.log("WOO!"); result.push(s); } } return result; },[]);
var isInRange = function(playerCoord, pokemonCoord, maxRange){ var lat = playerCoord.split("/")[0]; var lon = playerCoord.split("/")[1]; var pokeLat = pokemonCoord.split("/")[0]; var pokeLon = pokemonCoord.split("/")[1]; var inRange = geolib.isPointInCircle( {latitude: lat, longitude: lon}, {latitude: pokeLat, longitude: pokeLon}, maxRange ); return inRange; }
function checkPosition (app, context, normalizedDeltaData) { const vessel = _.get(app.signalk.root, normalizedDeltaData.context) const position = _.get(vessel, 'navigation.position') const subsPosition = _.get(context, 'position') if ( position && subsPosition && subsPosition.latitude && subsPosition.longitude ) { return geolib.isPointInCircle(position.value, subsPosition, context.radius) } return false }
User.findById(socket.decoded_token.id).populate('level').exec(function(err, resPlayer){ if(err) { socket.emit('action error') } else { if(resActionPoint == null || resSector == null || resPlayer == null) { socket.emit('action error') } else { console.log("resActionPoint", resActionPoint) var latlng = data.position console.log("latlng", latlng) var center = { longitude: resActionPoint.geometry.coordinates[0], latitude: resActionPoint.geometry.coordinates[1] } console.log('center', center) var radius = resActionPoint.properties.actionRadius console.log('radius', radius) var pointIsInCircle = Geolib.isPointInCircle(latlng, center, radius) console.log('pointIsInCircle', pointIsInCircle) if(!pointIsInCircle) { socket.emit('not near action') } else if ((resActionPoint.properties.lastPerformed + resActionPoint.properties.coolDown) > Math.round((new Date()).getTime() / 1000)) { socket.emit('action in cooldown') } else { GameCore.updateInfluence(resActionPoint, resSector, resPlayer, function(updatedSector){ GameCore.updateXP(resActionPoint, resSector, resPlayer, socket, function(updatedPlayer){ GameCore.updateNbActionToPerformedInSector(resActionPoint, resSector, resPlayer, socket, function(){ GameCore.makeActionPoint(resActionPoint, function(actionPerformed){ broadcastMobile('action point performed', Converter.actionPoint(actionPerformed)) Sector.findById(data.sector_id) .populate('properties.character') .populate('properties.actionsPoint') .populate(' properties.actionsPolygon') .exec(function(err, resSector){ broadcastDesktop('action polygon performed', Converter.sectorUnique(resSector)) }) User.findById(socket.decoded_token.id).populate('level').exec(function(err, resPlayer){ socket.emit('user update', Converter.userFull(resPlayer)) }) }) }) }) }) } } } })
app.whereAll("Query", function(err, queries) { if (err) res.status(500).json({success: false, message: err}); else { var queryQueue = new TaskQueue(); var queriesFound = []; for (var i = 0; i < queries.length; i++) { // get query and check if in radius var query = queries[i]; var inRadius = geolib.isPointInCircle( {latitude: query.get("lat"), longitude: query.get("lon")}, {latitude: parseFloat(req.query.lat), longitude: parseFloat(req.query.lon)}, 5000 ); if (inRadius) { (function(query) { queryQueue.queue(function (done) { query.toArrayModel("public", function(data) { data.distance = (geolib.getDistance( {latitude: data.lat, longitude: data.lon}, {latitude: req.query.lat, longitude: req.query.lon} ) / 1000).toFixed(1); queriesFound.push(data); done(); }); }); })(query); } } queryQueue.executeAll(function() { res.status(200).json({success: true, message: "Queries listed", queries: queriesFound}); }); } });
var foundTrucks = _.filter(self._allLocations, function(truckLocation) { if (truckLocation.latitude && truckLocation.longitude) { return geolib.isPointInCircle(truckLocation, latLong, 500); } });
node.on('input', function(msg) { var loc = undefined; if (msg.hasOwnProperty('location') && msg.location.hasOwnProperty('lat') && msg.location.hasOwnProperty('lon')) { loc = { latitude: msg.location.lat, longitude: msg.location.lon }; } else if (msg.hasOwnProperty('lon') && msg.hasOwnProperty('lat')) { loc = { latitude: msg.lat, longitude: msg.lon }; } else if (typeof(msg.payload) === 'object' && msg.payload.hasOwnProperty('lat') && msg.payload.hasOwnProperty('lon')) { loc = { latitude: msg.payload.lat, longitude: msg.payload.lon }; } if (loc) { var inout = false; if (node.mode === 'circle') { inout = geolib.isPointInCircle( loc, node.centre, Math.round(node.radius) ); } else { inout = geolib.isPointInside( loc, node.points ); } if (inout && (node.inside === "true")) { if (node.name) { if (!msg.location) { msg.location = {}; } msg.location.isat = msg.location.isat || []; msg.location.isat.push(node.name); } node.send(msg); } if (!inout && (node.inside === "false")) { node.send(msg); } if (node.inside === "both") { if (!msg.hasOwnProperty("location")) { msg.location = {}; } msg.location.inarea = inout; if (node.name) { // if there is a name msg.location.isat = msg.location.isat || []; if (inout) { // if inside then add name to an array msg.location.isat.push(node.name); } else { // if outside remove name from array if (msg.location.hasOwnProperty("isat")) { var i = msg.location.isat.indexOf(node.name); if (i > -1) { msg.location.isat.splice(i, 1); } } } //add distrance to centroid of area var distance; if (node.mode === 'circle') { distance = geolib.getDistance(node.centre, loc); } else { var centroid = geolib.getCenter(node.points); distance = geolib.getDistance(centroid, loc); } msg.location.distances = msg.location.distances || {}; msg.location.distances[node.name] = distance; } node.send(msg); } } });
var findTrip = function(rows, c, position, next) { // check if a row exists if (rows.length >= 1) { // decide by triptype to check what to do switch (rows[0].tripType) { // every day a new trip case 0: // not tour yet, so there definitly has to be one created if (rows[0].currentTrip == null) { var newTrip = { "key": shortid.generate(), "boat": position.boat, "type": 0, "start": position.timestamp, "finish": null }; // ... create new trip c.query("INSERT INTO trips SET ?", newTrip, function(err, tripResult) { if (err) { console.log(err); return next(null); } var newTripId = tripResult.insertId; c.query("UPDATE boats SET currentTrip = " + newTripId + " WHERE id = " + position.boat); return next(newTripId); }); } // there is a tour yet, should it be closed and // a new one, for a new day beeing started else { // find last boat position c.query("SELECT start FROM trips WHERE id = " + rows[0].currentTrip, function(err, lastTrip) { if (!err && lastTrip.length == 1) { var startTripLocalTime = moment.utc(lastTrip[0].start); var currLocalTime = moment.utc(position.timestamp); var tzOffsetHours = tzwhere.tzOffsetAt(position["latitude"], position["longitude"]); //console.log("offset hours", tzOffsetHours); // add or subtract the offset to local time if (tzOffsetHours > 0) { startTripLocalTime = startTripLocalTime.add("ms", tzOffsetHours); currLocalTime = currLocalTime.add("ms", tzOffsetHours); } else if (tzOffsetHours < 0) { startTripLocalTime = startTripLocalTime.subtract("ms", tzOffsetHours); currLocalTime = currLocalTime.subtract("ms", tzOffsetHours); } // check if a new day started, or the last position is longer then 24 hours old ... if (startTripLocalTime.dayOfYear() != currLocalTime.dayOfYear()) { // store analysis on the old tour tripAnalysis(rows, c); var newTrip = { "key": shortid.generate(), "boat": position.boat, "type": 0, "start": position.timestamp, "finish": null }; // ... create new trip c.query("INSERT INTO trips SET ?", newTrip, function(err, tripResult) { if (err) { console.log(err); return next(null); } var newTripId = tripResult.insertId; c.query("UPDATE trips SET finish = '" + position.timestamp + "' WHERE id = " + rows[0].currentTrip); c.query("UPDATE boats SET currentTrip = " + newTripId + " WHERE id = " + position.boat); return next(newTripId); }); } // no new day started else { return next(rows[0].currentTrip); } } else { console.log(err); return next(null); } }); } break; // leaving mooring case 1: var isAtMooring = geolib.isPointInCircle( position, rows[0], rows[0].radius ); // boat is not at the mooring ... if (!isAtMooring) { // ... and currently not on a trip, ... if (rows[0].currentTrip == null) { var newTrip = { "key": shortid.generate(), "boat": position.boat, "type": 1, "start": position.timestamp, "finish": null }; // ... create new trip c.query("INSERT INTO trips SET ?", newTrip, function(err, tripResult) { var newTripId = tripResult.insertId; c.query("UPDATE boats SET currentTrip = " + newTripId + " WHERE id = " + position.boat); return next(newTripId); }); } // there is currently a trip going on else { return next(rows[0].currentTrip); } } // boat is back at the mooring ... else { // ... and there is currently a trip going on, ... if (rows[0].currentTrip != null) { // do some calculations on the trip tripAnalysis(rows, c); // ... stop it c.query("UPDATE boats SET currentTrip = NULL WHERE id = " + position.boat); c.query("UPDATE trips SET finish = '" + position.timestamp + "' WHERE id = " + rows[0].currentTrip); return next(null); } } break; // manually case 2: return next(null); break; } } else return next(null); };