add simpler geojson support

This commit is contained in:
Dave Conway-Jones 2020-03-24 11:53:54 +00:00
parent 2e500c39d8
commit e29f06f1e8
No known key found for this signature in database
GPG Key ID: 302A6725C594817F
4 changed files with 27 additions and 12 deletions

View File

@ -1,5 +1,6 @@
### Change Log for Node-RED Worldmap
- v2.3.2 - Add better geojson support - name plus geojson properties
- v2.3.1 - Stop adding point when you add a circle
- v2.3.0 - Add colour options for drawing layer
- v2.2.1 - Better implementation of legend create/show/hide

View File

@ -208,6 +208,12 @@ a number of degrees.
msg.payload = { "name":"Bristol Channel", "lat":51.5, "lon":-2.9, "radius":[30000,70000], "tilt":45 };
### GeoJSON
If the msg.payload contains a **geojson** property, and no **lat** and **lon**, then rather than draw a point
it will render the geojson. Other optional properties (see below) can be used to style the geojson.
### Options
Areas, Rectangles, Lines, Circles and Ellipses can also specify more optional properties:

View File

@ -1,6 +1,6 @@
{
"name": "node-red-contrib-web-worldmap",
"version": "2.3.1",
"version": "2.3.2",
"description": "A Node-RED node to provide a web page of a world map for plotting things on.",
"dependencies": {
"cgi": "0.3.1",

View File

@ -292,15 +292,15 @@ function doTidyUp(l) {
if ((l && (l == markers[m].lay)) || typeof markers[m].ts != "undefined") {
if ((l && (l == markers[m].lay)) || (markers[m].hasOwnProperty("ts") && (Number(markers[m].ts) < d) && (markers[m].lay !== "_drawing"))) {
//console.log("STALE :",m);
layers[markers[m].lay].removeLayer(markers[m]);
if (typeof polygons[m] != "undefined") {
layers[markers[m].lay].removeLayer(polygons[m]);
delete polygons[m];
}
if (typeof polygons[m+"_"] != "undefined") {
layers[polygons[m+"_"].lay].removeLayer(polygons[m+"_"]);
delete polygons[m+"_"];
}
if (typeof polygons[m] != "undefined") {
layers[markers[m].lay].removeLayer(polygons[m]);
delete polygons[m];
}
layers[markers[m].lay].removeLayer(markers[m]);
delete markers[m];
}
}
@ -1104,6 +1104,9 @@ function setMarker(data) {
polygons[data.name] = rangerings(new L.LatLng((data.lat*1), (data.lon*1)), data.arc);
}
}
else if (data.hasOwnProperty("geojson")) {
doGeojson(data.geojson,(data.layer || "geojson"),opt);
}
if (polygons[data.name] !== undefined) {
polygons[data.name].lay = lay;
@ -2021,14 +2024,18 @@ function doCommand(cmd) {
}
// handle any incoming GEOJSON directly - may style badly
function doGeojson(g) {
console.log("GEOJSON",g);
if (!basemaps["geojson"]) {
function doGeojson(g,l,o) {
var glayer = l || "geojson";
if (!basemaps[glayer]) {
var opt = { style: function(feature) {
var st = { stroke:true, color:"#910000", weight:2, fill:true, fillColor:"#910000", fillOpacity:0.3 };
st = Object.assign(st,o);
if (feature.hasOwnProperty("properties")) {
console.log("GPROPS", feature.properties)
}
if (feature.hasOwnProperty("geometry") && feature.geometry.hasOwnProperty("type") && feature.geometry.type === "LineString") {
st.fill = false;
}
if (feature.hasOwnProperty("style")) {
console.log("GSTYLE", feature.style)
}
@ -2037,10 +2044,11 @@ function doGeojson(g) {
opt.onEachFeature = function (f,l) {
if (f.properties) { l.bindPopup('<pre>'+JSON.stringify(f.properties,null,' ').replace(/[\{\}"]/g,'')+'</pre>'); }
}
overlays["geojson"] = L.geoJson(g,opt);
layercontrol.addOverlay(overlays["geojson"],"geojson");
overlays[glayer] = L.geoJson(g,opt);
//layercontrol.addOverlay(overlays[glayer],glayer);
map.addLayer(overlays[glayer]);
}
overlays["geojson"].addData(g);
overlays[glayer].addData(g);
}
connect();