Skip to content

Commit

Permalink
Add support for floor and ceiling limits based on altitude
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Conway-Jones committed Dec 19, 2020
1 parent 77870a7 commit 6af008b
Show file tree
Hide file tree
Showing 6 changed files with 9,224 additions and 9,219 deletions.
56 changes: 33 additions & 23 deletions geofence.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,29 @@
limitations under the License.
-->

<script type="text/x-red" data-template-name="geofence">
<script type="text/html" data-template-name="geofence">
<div>
<link rel="stylesheet" href="geofence/js/leaflet/leaflet.css" />
<link rel="stylesheet" href="geofence/js/Leaflet.draw/dist/leaflet.draw.css" />
<link rel="stylesheet" href="geofence/js/L.GeoSearch/src/css/l.geosearch.css" />
<div id="node-geofence-map" style="width: 100%; height: 400px"></div>
<link rel="stylesheet" href="geofence/js/leaflet/leaflet.css" />
<link rel="stylesheet" href="geofence/js/Leaflet.draw/dist/leaflet.draw.css" />
<link rel="stylesheet" href="geofence/js/L.GeoSearch/src/css/l.geosearch.css" />
<div id="node-geofence-map" style="width: 100%; height: 400px"></div>
</div>

<br/>
<div class="form-row">
<label for="node-input-inside"><i class="fa fa-sign-in"></i> Action</label>
<select id="node-input-inside" value="true" style="width: 70%;">
<option value="true">only points inside</option>
<option value="false">only points outside</option>
<option value="both">add "inarea" property</option>
</select>
<label for="node-input-floor"> _ Floor</label>
<input type="text" id="node-input-floor" style="width:25%;" placeholder="ground">
<span style="padding-left:30px; padding-right:8px"> &#8254; Ceiling</span>
<input type="text" id="node-input-ceiling" style="width:25%;" placeholder="infinity">
</div>

<div class="form-row">
<label for="node-input-inside"><i class="fa fa-sign-in"></i> Action</label>
<select id="node-input-inside" value="true" style="width: 70%;">
<option value="true">only points inside</option>
<option value="false">only points outside</option>
<option value="both">add "inarea" property</option>
</select>
</div>

<div class="form-row">
Expand All @@ -38,21 +46,21 @@
</script>


<script type="text/x-red" data-help-name="geofence">
<script type="text/html" data-help-name="geofence">
<p>A simple geofence filter node</p>
<p>It supports circle and polygons, and will filter all messages that either
fall inside or outside the region described depending on the selected
fall inside or outside the region described depending on the selected
mode.</p>
<p>This node requires input messages one of the following (checked in order):
<ul>
<li><i>msg.payload.lat</i> &amp; <i>msg.payload.lon</li>
<li><i>msg.location.lat</i> &amp; <i>msg.location.lon</i></li>
<li><i>msg.lat</i> &amp; <i>msg.lon</i></li>
<li><i>msg.payload.lat</i> &amp; <i>msg.payload.lon</li>
</p>
<p>Alternatively it will add <b>msg.location.inarea</b> to the msg. with values of true/false</p>
<p>In this mode if the node is has a name then <b>msg.location.isat</b> will be an array
<p>In this mode if the node has a name then <b>msg.location.isat</b> will be an array
containing a list of named regions that the point is inside of and <b>msg.location.distance</b>
will contain an object of name, distance pairs. Where distance is the distance in metres to
will contain an object of name, distance pairs. Where distance is the distance in metres to
from the point to the centroid of the region.</p>
</script>

Expand All @@ -66,7 +74,9 @@
inside: {value: "true"},
rad: {value: 0},
points: {value: []},
centre: {value: {latitude:0.0, longitude: 0.0}}
centre: {value: {latitude:0.0, longitude: 0.0}},
floor: {value: ""},
ceiling: {value : ""}
},
inputs:1,
outputs:1,
Expand All @@ -79,7 +89,7 @@
},
oneditprepare: function () {
function setupMap(node) {
var map = L.map('node-geofence-map').setView([50.9025, -1.4042], 9);
var map = L.map('node-geofence-map').setView([51.0, -1.4], 6);

window.node_geofence_map = map;
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
Expand Down Expand Up @@ -164,17 +174,16 @@
drawControl2.addTo(map);
}

} else {
}
else {
if (node.points.length >= 3) {
//console.log("found polygon ");
var corners = [];
for (x in node.points) {
var latlng = L.latLng(node.points[x].latitude, node.points[x].longitude);
corners.push(latlng);
}
var poly = L.polygon(
corners
);
var poly = L.polygon(corners);
poly.addTo(drawnItems);
map.fitBounds(
poly.getBounds(),
Expand Down Expand Up @@ -238,7 +247,8 @@
n.centre = {latitude: layer._latlng.lat, longitude: layer._latlng.lng};
n.rad = layer._mRadius;
n.points = [];
} else {
}
else {
n.mode ="polyline";
n.points = [];
n.rad = 0;
Expand Down
28 changes: 17 additions & 11 deletions geofence.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
module.exports = function(RED) {
"use strict";
var geolib = require('geolib');
var path= require('path');

function geofenceNode(n) {
RED.nodes.createNode(this,n);
Expand All @@ -27,6 +26,8 @@ module.exports = function(RED) {
this.points = n.points;
this.radius = n.rad;
this.inside = n.inside;
this.floor = parseInt(n.floor || "");
this.ceiling = parseInt(n.ceiling || "");
var node = this;

node.on('input', function(msg, send, done) {
Expand All @@ -37,30 +38,37 @@ module.exports = function(RED) {
if (msg.hasOwnProperty('location') && msg.location.hasOwnProperty('lat') && msg.location.hasOwnProperty('lon')) {
loc = {
latitude: msg.location.lat,
longitude: msg.location.lon
longitude: msg.location.lon,
elevation: msg.location.alt || msg.location.altitude
};
} else if (msg.hasOwnProperty('lon') && msg.hasOwnProperty('lat')) {
loc = {
latitude: msg.lat,
longitude: msg.lon
longitude: msg.lon,
elevation: msg.alt || msg.altitude
};
} else if (typeof(msg.payload) === 'object' && msg.payload.hasOwnProperty('lat') && msg.payload.hasOwnProperty('lon')) {
loc = {
latitude: msg.payload.lat,
longitude: msg.payload.lon
longitude: msg.payload.lon,
elevation: msg.payload.alt || msg.payload.altitude
};
}

if (loc) {
var inout = false;
if (node.mode === 'circle') {
inout = geolib.isPointInCircle( loc, node.centre, Math.round(node.radius) );
} else {
}
else {
inout = geolib.isPointInside( loc, node.points );
}

if (loc.elevation && !isNaN(node.floor) && loc.elevation < node.floor) { inout = false; }
if (loc.elevation && !isNaN(node.ceiling) && loc.elevation > node.ceiling) { inout = false; }

if (inout && (node.inside === "true")) {
if (node.name) {
if (node.name) {
if (!msg.location) {
msg.location = {};
}
Expand Down Expand Up @@ -115,12 +123,11 @@ module.exports = function(RED) {
msg.location.distances[node.name] = distance;
}
send(msg);
if (done) {
done();
}
if (done) { done(); }
return;
}
} else {
}
else {
//no location
if (done) {
done("No location found in message")
Expand All @@ -137,7 +144,6 @@ module.exports = function(RED) {
root: __dirname + '/static/',
dotfiles: 'deny'
};

res.sendFile(req.params[0], options);

// var filename = path.join(__dirname , 'static', req.params[0]);
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "node-red-node-geofence",
"version": "0.1.4",
"version": "0.2.0",
"description": "A simple node to filter based on location",
"dependencies" :{
"geolib": "^2.0.0"
"geolib": "^2.0.24"
},
"repository" : {
"type":"git",
Expand Down
Loading

0 comments on commit 6af008b

Please sign in to comment.