Add GeoJSON output of planned lines

This commit is contained in:
D. Berge
2020-10-09 13:58:11 +02:00
parent 60ff4f57b1
commit 72922560d2
4 changed files with 60 additions and 14 deletions

View File

@@ -1,14 +0,0 @@
const { plan } = require('../../../lib/db');
module.exports = async function (req, res, next) {
try {
res.status(200).send(await plan.list(req.params.project, req.query));
next();
} catch (err) {
next(err);
}
};

View File

@@ -0,0 +1,23 @@
const { plan } = require('../../../../lib/db');
const geojson = async function (req, res, next) {
try {
const plans = await plan.list(req.params.project, req.query);
const response = plans.filter(plan => plan.geometry).map(plan => {
const feature = {
type: "Feature",
geometry: plan.geometry,
properties: plan
};
delete feature.properties.geometry;
return feature;
});
res.status(200).send(response);
next();
} catch (err) {
next(err);
}
}
module.exports = geojson;

View File

@@ -0,0 +1,23 @@
const json = require('./json');
const geojson = require('./geojson');
module.exports = async function (req, res, next) {
try {
const handlers = {
"application/json": json,
"application/geo+json": geojson,
};
const mimetype = req.accepts(Object.keys(handlers));
if (mimetype) {
res.set("Content-Type", mimetype);
await handlers[mimetype](req, res, next);
} else {
res.status(406).send();
next();
}
} catch (err) {
next(err);
}
}

View File

@@ -0,0 +1,14 @@
const { plan } = require('../../../../lib/db');
const json = async function (req, res, next) {
try {
const response = await plan.list(req.params.project, req.query);
res.status(200).send(response);
next();
} catch (err) {
next(err);
}
};
module.exports = json;