UNPKG

node-red-contrib-snap4city-user

Version:

Nodes for Snap4city project, targeted to standard user (no developer)

76 lines (59 loc) 2.7 kB
/* NODE-RED-CONTRIB-SNAP4CITY-USER Copyright (C) 2018 DISIT Lab http://www.disit.org - University of Florence This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ module.exports = function (RED) { function PointWithinPolygon(config) { var wellknown = require("wellknown"); RED.nodes.createNode(this, config); var node = this; node.on('input', function (msg) { var polygon = (msg.payload.polygon ? msg.payload.polygon : config.polygon); var pointlatitude = parseFloat(msg.payload.pointlatitude ? msg.payload.pointlatitude : config.pointlatitude); var pointlongitude = parseFloat(msg.payload.pointlongitude ? msg.payload.pointlongitude : config.pointlongitude); geoJSONPolygon = wellknown.parse(polygon); msg.payload = inside([pointlongitude, pointlatitude], geoJSONPolygon.coordinates[0]); node.send(msg); }); } function inside(point, vs) { // ray-casting algorithm based on // http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html var x = point[0], y = point[1]; var inside = false; for (var i = 0, j = vs.length - 1; i < vs.length; j = i++) { var xi = vs[i][0], yi = vs[i][1]; var xj = vs[j][0], yj = vs[j][1]; var intersect = ((yi > y) != (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi); if (intersect) inside = !inside; } return inside; }; RED.nodes.registerType("point-within-polygon", PointWithinPolygon); RED.httpAdmin.get('/s4c/js/*', function (req, res) { var options = { root: __dirname + '/lib/js/', dotfiles: 'deny' }; res.sendFile(req.params[0], options); }); RED.httpAdmin.get('/s4c/css/*', function (req, res) { var options = { root: __dirname + '/lib/css/', dotfiles: 'deny' }; res.sendFile(req.params[0], options); }); }