Add endpoint for retrieving real-time data as GeoJSON

This commit is contained in:
D. Berge
2020-09-01 10:58:37 +02:00
parent 557d87de72
commit eded667d79
7 changed files with 136 additions and 1 deletions

View File

@@ -134,6 +134,9 @@ app.map({
// },
'/navdata/': {
get: [ mw.navdata.get ],
'gis/:featuretype(line|point)': {
get: [ mw.gis.navdata.get ]
}
}
//
// '/user': {

View File

@@ -1,3 +1,4 @@
module.exports = {
project: require('./project')
project: require('./project'),
navdata: require('./navdata')
};

View File

@@ -0,0 +1,40 @@
const { gis } = require('../../../../lib/db');
function makeOptions (query) {
const options = {};
if (query.hasOwnProperty("limit")) {
options.limit = Math.abs(Number(query.limit));
}
if (query.hasOwnProperty("bbox")) {
options.bbox = query.bbox
.split(",")
.slice(0, 4)
.map( (n, i) => (i % 2) ? Number(n) % 90 : Number(n) % 180 );
if (options.bbox.some(n => isNaN(n))) {
delete options.bbox;
}
}
return options;
}
module.exports = async function (req, res, next) {
const handler = req.params.featuretype == "point"
? gis.navdata.get.points
: gis.navdata.get.lines;
const options = makeOptions(req.query);
try {
res.status(200).send(await handler(options));
next();
} catch (err) {
next(err);
}
};

View File

@@ -0,0 +1,3 @@
module.exports = {
get: require('./get')
};

View File

@@ -1,5 +1,6 @@
module.exports = {
project: require('./project'),
navdata: require('./navdata')
// line: require('./line')
};

View File

@@ -0,0 +1,83 @@
const { pool } = require('../../connection');
async function lines (options = {}) {
const client = await pool.connect();
const text = `
SELECT ST_AsGeoJSON(ST_MakeLine(geometry)) geojson
FROM (
SELECT geometry
FROM real_time_inputs
WHERE
($1 IS true
AND geometry && ST_MakeEnvelope($2, $3, $4, $5, 4326)
)
OR ($1 IS false AND true)
ORDER BY tstamp DESC
LIMIT $6
) t
`;
// const values = [ options.limit || 1000, opts.offset || 0 ];
const values = [
"bbox" in options,
...(options.bbox||[null, null, null, null]),
options.limit || 5000
];
const res = await client.query(text, values);
client.release();
if (res.rows && res.rows.length) {
return res.rows.map(r => JSON.parse(r.geojson));
} else {
throw {status: 404};
}
}
async function points (options = {}) {
const client = await pool.connect();
const values = [
"bbox" in options,
...(options.bbox||[null, null, null, null]),
options.limit || 5000
];
const text = `
SELECT json_build_object(
'type', 'Feature',
'geometry', ST_AsGeoJSON(s.geometry)::json,
'properties', s.meta
) geojson
FROM (
SELECT geometry, meta
FROM real_time_inputs
WHERE
($1 IS true
AND geometry && ST_MakeEnvelope($2, $3, $4, $5, 4326)
)
OR ($1 IS false AND true)
ORDER BY tstamp DESC
LIMIT $6
) s;
`;
const res = await client.query(text, values);
client.release();
if (res.rows && res.rows.length) {
return res.rows.map(r => r.geojson);
} else {
throw {status: 404};
}
}
module.exports = {
lines,
points
};

View File

@@ -0,0 +1,4 @@
module.exports = {
get: require('./get'),
};