update rest of parsers to allow property to be selected

This commit is contained in:
Dave Conway-Jones
2018-01-31 11:18:30 +00:00
parent f664fa0e31
commit b2de928860
10 changed files with 158 additions and 139 deletions

View File

@@ -1,5 +1,9 @@
<script type="text/x-red" data-template-name="geohash">
<div class="form-row">
<label for="node-input-property"><i class="fa fa-ellipsis-h"></i> <span data-i18n="node-red:common.label.property"></span></label>
<input type="text" id="node-input-property" style="width:70%;"/>
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
@@ -14,7 +18,6 @@
<p>If the <code>msg.payload</code> is an object with properties lat or latitude and lon or longitude
- it will add a <code>geohash</code> property to the payload.</p>
<p>The precision can be set by <code>msg.payload.precision</code> from 1 to 9.
<p><b>Note:</b> If the msg contains a .location property it will operate on that in preference to the .payload.</p>
</script>
<script type="text/javascript">
@@ -22,7 +25,8 @@
category: 'location',
color:"#DEBD5C",
defaults: {
name: {value:""}
name: {value:""},
property: {value:"payload",required:true}
},
inputs:1,
outputs:1,

View File

@@ -5,6 +5,7 @@ module.exports = function(RED) {
function GeohashNode(n) {
RED.nodes.createNode(this,n);
this.property = n.property||"payload";
var node = this;
var round = function(value, decimals) {
@@ -12,82 +13,71 @@ module.exports = function(RED) {
}
this.on("input", function(msg) {
if (msg.hasOwnProperty("location")) {
if (msg.location.hasOwnProperty("geohash")) {
var pos = geohash.decode(msg.location.geohash);
msg.location.lat = round(pos.latitude,5);
msg.location.lon = round(pos.longitude,5);
msg.location.error = { lat:round(pos.error.latitude,5), lon:round(pos.error.longitude,5) };
node.send(msg);
var value = RED.util.getMessageProperty(msg,node.property);
if (value !== undefined) {
if (typeof value === "string") {
// try to decode it...
var regexp = new RegExp('^[a-z0-9]{1,9}$'); // can only contain a-z or 0-9 and length 1-9
if (regexp.test(value)) {
var po = geohash.decode(value);
value = { lat:round(po.latitude,5), lon:round(po.longitude,5) };
value.error = { lat:round(po.error.latitude,5), lon:round(po.error.longitude,5) };
RED.util.setMessageProperty(msg,node.property,value);
node.send(msg);
}
else if (value.indexOf(",") !== -1) {
// has a comma so assume it's lat,lon(,precision)
var bits = value.split(",");
if ((bits.length === 2) || (bits.length === 3)) {
var li = 9;
if (bits.length === 3) {
li = parseInt(bits[2]);
if (li < 1) { li = 1; }
if (li > 9) { li = 9; }
}
var la = Number(bits[0]);
if (la < -90) { la = -90; }
if (la > 90) { la = 90; }
var lo = Number(bits[1]);
if (lo < -180) { lo = ((lo-180)%360)+180; }
if (lo > 180) { lo = ((lo+180)%360)-180; }
if (!isNaN(la) && !isNaN(lo)) {
value = geohash.encode(la, lo, li);
RED.util.setMessageProperty(msg,node.property,value);
node.send(msg);
}
else {
node.warn("Incorrect string format - should be lat,lon");
}
}
else { node.warn("Unexpected string format - should be lat,lon"); }
}
else { node.warn("Unexpected string format - should either be lat,lon or geohash"); }
}
else {
var lt = msg.location.lat;
var ln = msg.location.lon;
var le = parseInt(msg.location.precision || 9);
if (le < 1) { le = 1; }
if (le > 9) { le = 9; }
if (lt && ln) {
msg.location.geohash = geohash.encode(lt, ln, le);
else if (typeof value === "object") {
if (value.hasOwnProperty("geohash")) {
var pos = geohash.decode(value.geohash);
value.lat = round(pos.latitude,5);
value.lon = round(pos.longitude,5);
value.error = { lat:round(pos.error.latitude,5), lon:round(pos.error.longitude,5) };
RED.util.setMessageProperty(msg,node.property,value);
node.send(msg);
}
else {
node.warn("lat or lon missing from msg.location");
}
}
}
else if (typeof msg.payload === "string") {
// try to decode it...
var regexp = new RegExp('^[a-z0-9]{1,9}$'); // can only contain a-z or 0-9 and length 1-9
if (regexp.test(msg.payload)) {
var po = geohash.decode(msg.payload);
msg.payload = { lat:round(po.latitude,5), lon:round(po.longitude,5) };
msg.payload.error = { lat:round(po.error.latitude,5), lon:round(po.error.longitude,5) };
node.send(msg);
}
else if (msg.payload.indexOf(",") !== -1) {
// has a comma so assume it's lat,lon(,precision)
var bits = msg.payload.split(",");
if ((bits.length === 2) || (bits.length === 3)) {
var li = 9;
if (bits.length === 3) {
li = parseInt(bits[2]);
if (li < 1) { li = 1; }
if (li > 9) { li = 9; }
}
var la = Number(bits[0]);
if (la < -90) { la = -90; }
if (la > 90) { la = 90; }
var lo = Number(bits[1]);
if (lo < -180) { lo = ((lo-180)%360)+180; }
if (lo > 180) { lo = ((lo+180)%360)-180; }
if (!isNaN(la) && !isNaN(lo)) {
msg.payload = geohash.encode(la, lo, li);
var lat = value.lat || value.latitude;
var lon = value.lon || value.longitude;
var len = parseInt(value.precision || 9);
if (len < 1) { len = 1; }
if (len > 9) { len = 9; }
if (lat && lon) {
value.geohash = geohash.encode(lat, lon, len);
RED.util.setMessageProperty(msg,node.property,value);
node.send(msg);
}
else {
node.warn("Incorrect string format - should be lat,lon");
}
else { node.warn("lat or lon missing from msg."+node.property); }
}
else { node.warn("Unexpected string format - should be lat,lon"); }
}
else { node.warn("Unexpected string format - should either be lat,lon or geohash"); }
}
else if (typeof msg.payload === "object") {
var lat = msg.payload.lat || msg.payload.latitude;
var lon = msg.payload.lon || msg.payload.longitude;
var len = parseInt(msg.payload.precision || 9);
if (len < 1) { len = 1; }
if (len > 9) { len = 9; }
if (lat && lon) {
msg.payload.geohash = geohash.encode(lat, lon, len);
node.send(msg);
}
else {
node.warn("lat or lon missing from msg.payload");
}
}
else {
node.warn("This node only expects strings or objects.");
else { node.warn("This node only expects strings or objects."); }
}
});
}

View File

@@ -1,6 +1,6 @@
{
"name" : "node-red-node-geohash",
"version" : "0.0.7",
"version" : "0.1.7",
"description" : "A Node-RED node to encode and decode lat,lon pairs to a geohash.",
"dependencies" : {
"ngeohash" : "0.6.0"