node-red-contrib-web-worldmap 2.21.3 → 2.21.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,6 @@
1
1
  ### Change Log for Node-RED Worldmap
2
2
 
3
+ - v2.21.4 - fix speed leader length. Add transparentPixels option.
3
4
  - v2.21.3 - Add zoom to bounds action. Adjust map layers max zoom levels.
4
5
  - v2.21.2 - Expand ship nav to ship navigation.
5
6
  - v2.21.1 - Fix ui check callback to not use .
package/README.md CHANGED
@@ -11,6 +11,7 @@ map web page for plotting "things" on.
11
11
 
12
12
  ### Updates
13
13
 
14
+ - v2.21.4 - fix speed leader length. Add transparentPixels option..
14
15
  - v2.21.3 - Add zoom to bounds action. Adjust map layers max zoom levels.
15
16
  - v2.21.2 - Expand ship nav to ship navigation.
16
17
  - v2.21.1 - Fix ui check callback to not use .
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-red-contrib-web-worldmap",
3
- "version": "2.21.3",
3
+ "version": "2.21.4",
4
4
  "description": "A Node-RED node to provide a web page of a world map for plotting things on.",
5
5
  "dependencies": {
6
6
  "@turf/bezier-spline": "~6.5.0",
@@ -72,6 +72,7 @@
72
72
  <script src="leaflet/leaflet.latlng-graticule.js"></script>
73
73
  <script src="leaflet/VectorTileLayer.umd.min.js"></script>
74
74
  <script src="leaflet/Semicircle.js"></script>
75
+ <script src="leaflet/L.TileLayer.PixelFilter.js"></script>
75
76
  <script src="leaflet/dialog-polyfill.js"></script>
76
77
 
77
78
  <script src="images/emoji.js"></script>
@@ -0,0 +1,152 @@
1
+ /*
2
+ * L.TileLayer.PixelFilter
3
+ * https://github.com/greeninfo/L.TileLayer.PixelFilter
4
+ * http://greeninfo-network.github.io/L.TileLayer.PixelFilter/
5
+ */
6
+ L.tileLayerPixelFilter = function (url, options) {
7
+ return new L.TileLayer.PixelFilter(url, options);
8
+ }
9
+
10
+ L.TileLayer.PixelFilter = L.TileLayer.extend({
11
+ // the constructor saves settings and throws a fit if settings are bad, as typical
12
+ // then adds the all-important 'tileload' event handler which basically "detects" an unmodified tile and performs the pxiel-swap
13
+ initialize: function (url, options) {
14
+ options = L.extend({}, L.TileLayer.prototype.options, {
15
+ matchRGBA: null,
16
+ missRGBA: null,
17
+ pixelCodes: [],
18
+ crossOrigin: 'Anonymous', // per issue 15, this is how you do it in Leaflet 1.x
19
+ }, options);
20
+ L.TileLayer.prototype.initialize.call(this, url, options);
21
+ L.setOptions(this, options);
22
+
23
+ // go ahead and save our settings
24
+ this.setMatchRGBA(this.options.matchRGBA);
25
+ this.setMissRGBA(this.options.missRGBA);
26
+ this.setPixelCodes(this.options.pixelCodes);
27
+
28
+ // and add our tile-load event hook which triggers us to do the pixel-swap
29
+ this.on('tileload', function (event) {
30
+ this.applyFiltersToTile(event.tile);
31
+ });
32
+ },
33
+
34
+ // settings setters
35
+ setMatchRGBA: function (rgba) {
36
+ // save the setting
37
+ if (rgba !== null && (typeof rgba !== 'object' || typeof rgba.length !== 'number' || rgba.length !== 4) ) throw "L.TileLayer.PixelSwap expected matchRGBA to be RGBA [r,g,b,a] array or else null";
38
+ this.options.matchRGBA = rgba;
39
+
40
+ // force a redraw, which means new tiles, which mean new tileload events; the circle of life
41
+ this.redraw(true);
42
+ },
43
+ setMissRGBA: function (rgba) {
44
+ // save the setting
45
+ if (rgba !== null && (typeof rgba !== 'object' || typeof rgba.length !== 'number' || rgba.length !== 4) ) throw "L.TileLayer.PixelSwap expected missRGBA to be RGBA [r,g,b,a] array or else null";
46
+ this.options.missRGBA = rgba;
47
+
48
+ // force a redraw, which means new tiles, which mean new tileload events; the circle of life
49
+ this.redraw(true);
50
+ },
51
+ setPixelCodes: function (pixelcodes) {
52
+ // save the setting
53
+ if (typeof pixelcodes !== 'object' || typeof pixelcodes.length !== 'number') throw "L.TileLayer.PixelSwap expected pixelCodes to be a list of triplets: [ [r,g,b], [r,g,b], ... ]";
54
+ this.options.pixelCodes = pixelcodes;
55
+
56
+ // force a redraw, which means new tiles, which mean new tileload events; the circle of life
57
+ this.redraw(true);
58
+ },
59
+
60
+ // extend the _createTile function to add the .crossOrigin attribute, since loading tiles from a separate service is a pretty common need
61
+ // and the Canvas is paranoid about cross-domain image data. see issue #5
62
+ // this is really only for Leaflet 0.7; as of 1.0 L.TileLayer has a crossOrigin setting which we define as a layer option
63
+ _createTile: function () {
64
+ var tile = L.TileLayer.prototype._createTile.call(this);
65
+ tile.crossOrigin = "Anonymous";
66
+ return tile;
67
+ },
68
+
69
+ // the heavy lifting to do the pixel-swapping
70
+ // called upon 'tileload' and passed the IMG element
71
+ // tip: when the tile is saved back to the IMG element that counts as a tileload event too! thus an infinite loop, as wel as comparing the pixelCodes against already-replaced pixels!
72
+ // so, we tag the already-swapped tiles so we know when to quit
73
+ // if the layer is redrawn, it's a new IMG element and that means it would not yet be tagged
74
+ applyFiltersToTile: function (imgelement) {
75
+ // already processed, see note above
76
+ if (imgelement.getAttribute('data-PixelFilterDone')) return;
77
+
78
+ // copy the image data onto a canvas for manipulation
79
+ var width = imgelement.width;
80
+ var height = imgelement.height;
81
+ var canvas = document.createElement("canvas");
82
+ canvas.width = width;
83
+ canvas.height = height;
84
+ var context = canvas.getContext("2d");
85
+ context.drawImage(imgelement, 0, 0);
86
+
87
+ // create our target imagedata
88
+ var output = context.createImageData(width, height);
89
+
90
+ // extract out our RGBA trios into separate numbers, so we don't have to use rgba[i] a zillion times
91
+ var matchRGBA = this.options.matchRGBA, missRGBA = this.options.missRGBA;
92
+ if (matchRGBA !== null) {
93
+ var match_r = matchRGBA[0], match_g = matchRGBA[1], match_b = matchRGBA[2], match_a = matchRGBA[3];
94
+ }
95
+ if (missRGBA !== null) {
96
+ var miss_r = missRGBA[0], miss_g = missRGBA[1], miss_b = missRGBA[2], miss_a = missRGBA[3];
97
+ }
98
+
99
+ // go over our pixel-code list and generate the list of integers that we'll use for RGB matching
100
+ // 1000000*R + 1000*G + B = 123123123 which is an integer, and finding an integer inside an array is a lot faster than finding an array inside an array
101
+ var pixelcodes = [];
102
+ for (var i=0, l=this.options.pixelCodes.length; i<l; i++) {
103
+ var value = 1000000 * this.options.pixelCodes[i][0] + 1000 * this.options.pixelCodes[i][1] + this.options.pixelCodes[i][2];
104
+ pixelcodes.push(value);
105
+ }
106
+
107
+ // iterate over the pixels (each one is 4 bytes, RGBA)
108
+ // and see if they are on our list (recall the "addition" thing so we're comparing integers in an array for performance)
109
+ // per issue #5 catch a failure here, which is likely a cross-domain problem
110
+ try {
111
+ var pixels = context.getImageData(0, 0, width, height).data;
112
+ } catch(e) {
113
+ throw "L.TileLayer.PixelFilter getImageData() failed. Likely a cross-domain issue?";
114
+ }
115
+ for(var i = 0, n = pixels.length; i < n; i += 4) {
116
+ var r = pixels[i ];
117
+ var g = pixels[i+1];
118
+ var b = pixels[i+2];
119
+ var a = pixels[i+3];
120
+
121
+ // bail condition: if the alpha is 0 then it's already transparent, likely nodata, and we should skip it
122
+ if (a == 0) {
123
+ output.data[i ] = 255;
124
+ output.data[i+1] = 255;
125
+ output.data[i+2] = 255;
126
+ output.data[i+3] = 0;
127
+ continue;
128
+ }
129
+
130
+ // default to matching, so that if we are not in fact filtering by code it's an automatic hit
131
+ // number matching trick: 1000000*R + 1000*G + 1*B = 123,123,123 a simple number that either is or isn't on the list
132
+ var match = true;
133
+ if (pixelcodes.length) {
134
+ var sum = 1000000 * r + 1000 * g + b;
135
+ if (-1 === pixelcodes.indexOf(sum)) match = false;
136
+ }
137
+
138
+ // did it match? either way we push a R, a G, and a B onto the image blob
139
+ // if the target RGBA is a null, then we push exactly the same RGBA as we found in the source pixel
140
+ output.data[i ] = match ? (matchRGBA===null ? r : match_r) : (missRGBA===null ? r : miss_r);
141
+ output.data[i+1] = match ? (matchRGBA===null ? g : match_g) : (missRGBA===null ? g : miss_g);
142
+ output.data[i+2] = match ? (matchRGBA===null ? b : match_b) : (missRGBA===null ? b : miss_b);
143
+ output.data[i+3] = match ? (matchRGBA===null ? a : match_a) : (missRGBA===null ? a : miss_a);
144
+ }
145
+
146
+ // write the image back to the canvas, and assign its base64 back into the on-screen tile to visualize the change
147
+ // tag the tile as having already been updated, so we don't process a 'load' event again and re-process a tile that surely won't match any target RGB codes, in an infinite loop!
148
+ context.putImageData(output, 0, 0);
149
+ imgelement.setAttribute('data-PixelFilterDone', true);
150
+ imgelement.src = canvas.toDataURL();
151
+ }
152
+ });
@@ -110,7 +110,12 @@ var handleData = function(data) {
110
110
  }
111
111
  }
112
112
  if (data.command) { doCommand(data.command); delete data.command; }
113
- if (data.hasOwnProperty("type") && data.type.indexOf("Feature") === 0) { doGeojson("geojson",data); }
113
+ if (data.hasOwnProperty("type") && data.type.indexOf("Feature") === 0) {
114
+ if (data.hasOwnProperty('properties') && data.properties.hasOwnProperty('title')) {
115
+ doGeojson(data.properties.title,data)
116
+ }
117
+ else { doGeojson("geojson",data); }
118
+ }
114
119
  else if (data.hasOwnProperty("name")) { setMarker(data); }
115
120
  else {
116
121
  if (JSON.stringify(data) !== '{}') {
@@ -1983,7 +1988,7 @@ function setMarker(data) {
1983
1988
  else if (data.heading !== undefined) { track = data.heading; }
1984
1989
  else if (data.bearing !== undefined) { track = data.bearing; }
1985
1990
  if (track != undefined) { // if there is a heading
1986
- if (data.speed != null && !data.length) { // and a speed - lets convert to a leader length
1991
+ if (data.speed != null && data.length === undefined) { // and a speed - lets convert to a leader length
1987
1992
  data.length = parseFloat(data.speed || "0") * 60;
1988
1993
  var re1 = new RegExp('kn|knot|kt','i');
1989
1994
  var re2 = new RegExp('kph|kmh','i');
@@ -1992,7 +1997,7 @@ function setMarker(data) {
1992
1997
  else if ( re2.test(""+data.speed) ) { data.length = data.length * 0.44704; }
1993
1998
  else if ( re3.test(""+data.speed) ) { data.length = data.length * 0.277778; }
1994
1999
  }
1995
- if (data.length != null) {
2000
+ if (data.length !== undefined) {
1996
2001
  if (polygons[data.name] != null && !polygons[data.name].hasOwnProperty("_layers")) {
1997
2002
  map.removeLayer(polygons[data.name]);
1998
2003
  }
@@ -2452,6 +2457,11 @@ function doCommand(cmd) {
2452
2457
  else if (cmd.map.url.slice(-4).toLowerCase() === ".pbf") {
2453
2458
  overlays[cmd.map.overlay] = VectorTileLayer(cmd.map.url, cmd.map.opt);
2454
2459
  }
2460
+ else if (cmd.map.hasOwnProperty("transparentPixels")) {
2461
+ cmd.map.opt.pixelCodes = cmd.map.transparentPixels;
2462
+ cmd.map.opt.matchRGBA = [ 0,0,0,0 ];
2463
+ overlays[cmd.map.overlay] = L.tileLayerPixelFilter(cmd.map.url, cmd.map.opt);
2464
+ }
2455
2465
  else {
2456
2466
  overlays[cmd.map.overlay] = L.tileLayer(cmd.map.url, cmd.map.opt);
2457
2467
  }
@@ -2566,6 +2576,7 @@ function doCommand(cmd) {
2566
2576
 
2567
2577
  // handle any incoming GEOJSON directly - may style badly
2568
2578
  function doGeojson(n,g,l,o) {
2579
+ //console.log("GEOJSON",n,g,l,o)
2569
2580
  var lay = l || g.name || "unknown";
2570
2581
  // if (!basemaps[lay]) {
2571
2582
  var opt = { style: function(feature) {
@@ -2597,12 +2608,29 @@ function doGeojson(n,g,l,o) {
2597
2608
  return st;
2598
2609
  }}
2599
2610
  opt.pointToLayer = function (feature, latlng) {
2600
- var myMarker = L.VectorMarkers.icon({
2601
- icon: feature.properties["marker-symbol"] || "circle",
2602
- markerColor: (feature.properties["marker-color"] || "#910000"),
2603
- prefix: 'fa',
2604
- iconColor: 'white'
2605
- });
2611
+ var myMarker;
2612
+ if (feature.properties.hasOwnProperty("SIDC")) {
2613
+ myMarker = new ms.Symbol( feature.properties.SIDC.toUpperCase(), {
2614
+ uniqueDesignation:unescape(encodeURIComponent(feature.properties.title)) ,
2615
+ country:feature.properties.country,
2616
+ direction:feature.properties.bearing,
2617
+ additionalInformation:feature.properties.modifier,
2618
+ size:24
2619
+ });
2620
+ myMarker = L.icon({
2621
+ iconUrl: myMarker.toDataURL(),
2622
+ iconAnchor: [myMarker.getAnchor().x, myMarker.getAnchor().y],
2623
+ className: "natoicon",
2624
+ });
2625
+ }
2626
+ else {
2627
+ myMarker = L.VectorMarkers.icon({
2628
+ icon: feature.properties["marker-symbol"] || "circle",
2629
+ markerColor: (feature.properties["marker-color"] || "#910000"),
2630
+ prefix: 'fa',
2631
+ iconColor: 'white'
2632
+ });
2633
+ }
2606
2634
  if (!feature.properties.hasOwnProperty("title")) {
2607
2635
  feature.properties.title = feature.properties["marker-symbol"];
2608
2636
  }
@@ -42,6 +42,7 @@
42
42
  <script src="leaflet/Leaflet.Coordinates.js"></script>
43
43
  <script src="leaflet/leaflet.latlng-graticule.js"></script>
44
44
  <script src="leaflet/Semicircle.js"></script>
45
+ <script src="leaflet/L.TileLayer.PixelFilter.js"></script>
45
46
  <script src="leaflet/dialog-polyfill.js"></script>
46
47
 
47
48
  <script src="images/emoji.js"></script>