mirror of
https://gitlab.com/wgp/dougal/software.git
synced 2025-12-06 13:37:07 +00:00
Add endpoint for retrieving real-time data as GeoJSON
This commit is contained in:
@@ -134,6 +134,9 @@ app.map({
|
||||
// },
|
||||
'/navdata/': {
|
||||
get: [ mw.navdata.get ],
|
||||
'gis/:featuretype(line|point)': {
|
||||
get: [ mw.gis.navdata.get ]
|
||||
}
|
||||
}
|
||||
//
|
||||
// '/user': {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
module.exports = {
|
||||
project: require('./project')
|
||||
project: require('./project'),
|
||||
navdata: require('./navdata')
|
||||
};
|
||||
|
||||
40
lib/www/server/api/middleware/gis/navdata/get.js
Normal file
40
lib/www/server/api/middleware/gis/navdata/get.js
Normal 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);
|
||||
}
|
||||
|
||||
};
|
||||
3
lib/www/server/api/middleware/gis/navdata/index.js
Normal file
3
lib/www/server/api/middleware/gis/navdata/index.js
Normal file
@@ -0,0 +1,3 @@
|
||||
module.exports = {
|
||||
get: require('./get')
|
||||
};
|
||||
@@ -1,5 +1,6 @@
|
||||
|
||||
module.exports = {
|
||||
project: require('./project'),
|
||||
navdata: require('./navdata')
|
||||
// line: require('./line')
|
||||
};
|
||||
|
||||
83
lib/www/server/lib/db/gis/navdata/get.js
Normal file
83
lib/www/server/lib/db/gis/navdata/get.js
Normal 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
|
||||
};
|
||||
4
lib/www/server/lib/db/gis/navdata/index.js
Normal file
4
lib/www/server/lib/db/gis/navdata/index.js
Normal file
@@ -0,0 +1,4 @@
|
||||
|
||||
module.exports = {
|
||||
get: require('./get'),
|
||||
};
|
||||
Reference in New Issue
Block a user