Commit e6ec033c authored by Athanasios's avatar Athanasios
Browse files

add temporary layers

parent bab06581
......@@ -4,6 +4,7 @@ const port = config.port || 9000;
const home = require("./routes/home");
const v1 = require("./routes/v1");
const assets = require("./routes/assets");
const tempassets = require("./routes/tempassets");
const cors = require("./src/cors");
const app = express();
......@@ -11,6 +12,7 @@ app.use(cors);
app.use("/", home);
app.use("/service/v1", v1);
app.use("/assets", assets);
app.use("/tempassets", tempassets);
app.listen(port, err => {
if (err) {
......
[
{
"name": "(tmp) Building_Montreal",
"id": "99cea271-a47e-4bdc-b811-0cc234737b31",
"boundingbox": [
-73.60385570826058,
45.485815767958044,
-73.52044398972039,
45.539693141743676
],
"url": "https://tiles.arcgis.com/tiles/P3ePLMYs2RVChkJx/arcgis/rest/services/Building_Montreal/SceneServer/layers/0",
"creationTime": 1595007956608,
"scope": "public"
},
{
"name": "(tmp) Auckland",
"id": "4e4f8e6b-036d-401e-9394-2512132c0783",
"boundingbox": [
174.26421462290025,
-37.408063232703356,
175.54784137097715,
-36.14886498368073
],
"url": "https://tiles.arcgis.com/tiles/P3ePLMYs2RVChkJx/arcgis/rest/services/Buildings_Auckland/SceneServer/layers/0",
"creationTime": 1595008048649,
"scope": "public"
},
{
"name": "(tmp) Brest",
"id": "8a0fbb84-b1d7-4e74-80fe-0b6317440656",
"boundingbox": [
-4.573570959155898,
48.35651896244263,
-4.429143100105653,
48.45617712129358
],
"url": "https://tiles.arcgis.com/tiles/P3ePLMYs2RVChkJx/arcgis/rest/services/Buildings_Brest/SceneServer/layers/0",
"creationTime": 1595008152396,
"scope": "public"
},
{
"name": "(tmp) Building_Vancouver",
"id": "b1438326-c339-4ff2-813f-4268ed295a69",
"boundingbox": [
-123.14571375310894,
49.27071479410105,
-123.09975877538741,
49.29431568066567
],
"url": "https://tiles.arcgis.com/tiles/P3ePLMYs2RVChkJx/arcgis/rest/services/Building_Vancouver/SceneServer/layers/0",
"creationTime": 1595008263764,
"scope": "public"
},
{
"name": "(tmp) Linz",
"id": "301b1d78-a270-4aab-a4e3-5a1fe3a90f7e",
"boundingbox": [
14.246374126000035,
48.21488432500007,
14.399654755000029,
48.37778204300008
],
"url": "https://tiles.arcgis.com/tiles/P3ePLMYs2RVChkJx/arcgis/rest/services/Building_Linz_17/SceneServer/layers/0",
"creationTime": 1595008313873,
"scope": "public"
}
]
\ No newline at end of file
const config = {
port: 9000,
boundingBoxBufferPercent: 15
boundingBoxBufferPercent: 15,
tempLayers: {
expiry: {
seconds: 86400
}
}
}
module.exports = config;
\ No newline at end of file
This diff is collapsed.
......@@ -19,11 +19,13 @@
},
"homepage": "https://gitlab.com/tomeof/node-3dps-extended#README",
"dependencies": {
"body-parser": "^1.19.0",
"box-intersect": "^1.0.2",
"express": "^4.17.1",
"express-validator": "^6.4.0",
"node-fetch": "^2.6.0",
"pug": "^2.0.4"
"pug": "^2.0.4",
"uuid": "^8.2.0"
},
"devDependencies": {
"nodemon": "^2.0.3"
......
const express = require("express");
const bodyParser = require("body-parser");
const tempAssets = require("../src/tempAssets");
let router = express.Router();
router.route("/")
.post(bodyParser.json(), (req, res, next) => {
tempAssets.addTempLayer(req, res);
});
module.exports = router;
\ No newline at end of file
const fs = require("fs");
const fsprom = require('fs').promises;
const boxIntersect = require("box-intersect");
const path = require('path');
const getLayers = (layers) => {
return new Promise((resolve, reject) => {
fs.readFile("./assets/assets.json", (err, data) => {
if (err) {
reject();
}
let assets = JSON.parse(data);
let requestedLayers = layers.split(",");
// const getLayers = (layers) => {
// return new Promise((resolve, reject) => {
// fs.readFile("./assets/assets.json", (err, data) => {
// if (err) {
// reject();
// }
// let assets = JSON.parse(data);
// let requestedLayers = layers.split(",");
// assets = assets.filter(asset => requestedLayers.includes(asset.id));
// assets = assets.filter(asset => asset.scope == "public");
// resolve(assets);
// });
// });
// }
const getLayers = async (layers) => {
let assets = await getAllAssets();
let requestedLayers = layers.split(",");
assets = assets.filter(asset => requestedLayers.includes(asset.id));
assets = assets.filter(asset => asset.scope == "public");
return assets;
}
const getAllAssets = async () => {
try {
let assetsJson = await fsprom.readFile('./assets/assets.json', 'utf8');
let assets = JSON.parse(assetsJson);
let tempassetsJson = await fsprom.readFile('./assets/temporaryAssets.json', 'utf8');
let tempassets = JSON.parse(tempassetsJson);
assets.push(...tempassets);
return assets;
} catch (error) {
console.log(error);
return error;
}
assets = assets.filter(asset => requestedLayers.includes(asset.id));
assets = assets.filter(asset => asset.scope == "public");
resolve(assets);
});
});
}
const getIntersectingLayers = (bb, layers) => {
......@@ -83,37 +117,61 @@ const getIntersecting = (bb) => {
}
const allLayersExist = (layers) => {
// const allLayersExist = (layers) => {
if (!layers) return Promise.reject();
// if (!layers) return Promise.reject();
return new Promise((resolve, reject) => {
fs.readFile("./assets/assets.json", (err, data) => {
if (err) {
reject();
}
let assets = JSON.parse(data);
layers = layers.split(",");
layers.forEach(layer => {
if (assets.findIndex(asset => asset.id == layer) == -1) {
reject();
}
});
resolve();
});
});
// return new Promise((resolve, reject) => {
// fs.readFile("./assets/assets.json", (err, data) => {
// if (err) {
// reject();
// }
// let assets = JSON.parse(data);
// layers = layers.split(",");
// layers.forEach(layer => {
// if (assets.findIndex(asset => asset.id == layer) == -1) {
// reject();
// }
// });
// resolve();
// });
// });
}
// }
const getPublicLayers = () => {
const allLayersExist = async (layers) => {
return new Promise((resolve, reject) => {
fs.readFile("./assets/assets.json", (err, data) => {
let assets = JSON.parse(data);
assets = assets.filter(asset => asset.scope == "public");
resolve(assets);
});
if (!layers) return Promise.reject();
let assets = await getAllAssets();
layers = layers.split(",");
layers.forEach(layer => {
if (assets.findIndex(asset => asset.id == layer) == -1) {
return Promise.reject()
}
});
return;
}
// const getPublicLayers = () => {
// return new Promise((resolve, reject) => {
// fs.readFile("./assets/assets.json", (err, data) => {
// let assets = JSON.parse(data);
// assets = assets.filter(asset => asset.scope == "public");
// resolve(assets);
// });
// });
// }
const getPublicLayers = async () => {
let assets = await getAllAssets();
assets = assets.filter(asset => asset.scope == "public");
return assets;
}
......
const fs = require('fs').promises;
const { v4: uuidv4 } = require('uuid');
const config = require("../config");
const fetch = require("node-fetch");
const addTempLayer = (req, res) => {
(async () => {
try {
await removeExpired();
let doc = await fetch(req.body.url);
let nd = await doc.json();
let tempLayer = {
name: `(tmp) ${req.body.name}`,
id: uuidv4(),
boundingbox: nd.store.extent,
url: req.body.url,
creationTime: Date.now(),
scope: "public"
};
let tempassetsJson = await fs.readFile('./assets/temporaryAssets.json', 'utf8');
let tempassets = JSON.parse(tempassetsJson);
tempassets.push(tempLayer);
let updatedTempassets = JSON.stringify(tempassets, null, 2);
await fs.writeFile('./assets/temporaryAssets.json', updatedTempassets, 'utf8');
res.status(200).send("layer added");
} catch (error) {
console.log(error);
res.status(400).send(error);
}
})();
}
const removeExpired = async () => {
try {
let tempassetsJson = await fs.readFile('./assets/temporaryAssets.json', 'utf8');
let tempassets = JSON.parse(tempassetsJson);
let currentTime = Date.now();
let nonExpired = tempassets.filter(tempasset => {
let diffInSeconds = (currentTime - tempasset.creationTime) / 1000;
return (diffInSeconds < config.tempLayers.expiry.seconds);
});
let updatedTempassets = JSON.stringify(nonExpired, null, 2);
await fs.writeFile('./assets/temporaryAssets.json', updatedTempassets, 'utf8');
} catch (error) {
console.log(error);
}
}
module.exports = { addTempLayer };
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment