From 30f90c003906c78e9615e5b64cf5ebfb4da2d466 Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Wed, 29 Nov 2023 13:53:10 +0000 Subject: [PATCH] Fixup pmtiles loading from other directories --- CHANGELOG.md | 3 ++- README.md | 5 +++++ package.json | 2 +- worldmap.js | 16 ++++++++++++++-- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2187047..a6e687a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ### Change Log for Node-RED Worldmap - - v4.5.0 - Fix pmtiles to look for maps in userdir rather than modules + - v4.5.2 - Tidy up when pmtiles removed. + - v4.5.0 - Fix pmtiles to look for maps in userdir rather than modules. - v4.4.0 - Add quad(copter) drone icon. - v4.3.3 - Fix for objects changing layers. - v4.3.2 - Fix geojson popup missing label name. diff --git a/README.md b/README.md index 059e674..b6acb22 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ Feel free to [![](https://img.shields.io/static/v1?label=Sponsor&message=%E2%9D% ### Updates +- v4.5.2 - Tidy up when pmtiles removed. - v4.5.0 - Fix pmtiles to look for maps in userdir rather than modules - v4.4.0 - Add quad(copter) drone icon. - v4.3.3 - Fix for objects changing layers. @@ -723,6 +724,10 @@ You can use a PMtiles format map archive file from [Protomaps](https://docs.prot Copy your .pmtiles file(s) into your `~/.node-red` user directory. On re-starting Node-RED the node will detect the file(s) and add them to the base map layer menu, using the file name as the layer name. +You can also load them dynamically with a command like + + msg.payload = {"command":{"map":{"name":"MyMap","pmtiles":"/path/to/mymap.pmtiles"}}} + ### Using a Docker Map Server I have found the easiest to use mapserver for decent generic map to be Tileserver-gl. It uses mbtiles format maps - for example from [MapTiler Data](https://data.maptiler.com/downloads/planet/). You can download your mbtiles file into a directory and then from that directory run diff --git a/package.json b/package.json index 954c2a1..bc43177 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-red-contrib-web-worldmap", - "version": "4.5.1", + "version": "4.5.2 ", "description": "A Node-RED node to provide a web page of a world map for plotting things on.", "dependencies": { "@turf/bezier-spline": "~6.5.0", diff --git a/worldmap.js b/worldmap.js index 4e8859f..33e7f50 100644 --- a/worldmap.js +++ b/worldmap.js @@ -13,8 +13,9 @@ module.exports = function(RED) { if (fs.existsSync((__dirname + '/mapserv'))) { RED.httpNode.use("/cgi-bin/mapserv", require('cgi')(__dirname + '/mapserv')); } - //var pmtiles = fs.readdirSync(__dirname + '/worldmap').filter(fn => fn.endsWith('.pmtiles')); - var pmtiles = fs.readdirSync(RED.settings.userDir).filter(fn => fn.endsWith('.pmtiles')); + var pmtiles = fs.readdirSync(__dirname + '/worldmap').filter(fn => fn.endsWith('.pmtiles')); + pmtiles.forEach(file => { fs.unlinkSync(__dirname + '/worldmap/'+file); }) + pmtiles = fs.readdirSync(RED.settings.userDir).filter(fn => fn.endsWith('.pmtiles')); function worldMap(node, n) { var allPoints = {}; @@ -163,6 +164,17 @@ module.exports = function(RED) { node.on('input', function(msg) { if (!msg.hasOwnProperty("payload")) { node.warn("Missing payload"); return; } + + if (msg.payload.hasOwnProperty("command") && msg.payload.command.hasOwnProperty("map") && msg.payload.command.map.hasOwnProperty("pmtiles")) { + if (msg.payload.command.map.pmtiles.indexOf("http") !== 0) { + fs.symlink(msg.payload.command.map.pmtiles, __dirname+'/worldmap/'+msg.payload.command.map.name+'.pmtiles', 'file', (err) => { + if (err) { + if (err.code !== "EEXIST") { console.log(err); } + } + }); + msg.payload.command.map.pmtiles = msg.payload.command.map.name+'.pmtiles'; + } + } if (msg.hasOwnProperty("_sessionid")) { if (clients.hasOwnProperty(msg._sessionid)) { clients[msg._sessionid].write(JSON.stringify(msg.payload));