Add endpoints to download GeoJSON data for associations/regions

master
Manuel Kasper 2021-06-01 16:38:49 +02:00
rodzic 325ca0b702
commit 8f065a5cc8
1 zmienionych plików z 73 dodań i 0 usunięć

Wyświetl plik

@ -48,6 +48,24 @@ router.get('/associations/:association.kml', (req, res) => {
});
});
router.get('/associations/:association.geojson', (req, res) => {
res.cacheControl = {
noCache: true
};
res.set('Content-Type', 'application/geo+json');
res.set('Content-Disposition', 'attachment; filename="' + req.params.association + '.geojson"');
geoJsonForQuery('^' + req.params.association + '/', req.query, (err, geoJson) => {
if (err) {
console.error(err);
res.status(500).end();
return;
}
res.json(geoJson).end();
});
});
router.get('/regions/:association/:region.gpx', (req, res) => {
res.cacheControl = {
noCache: true
@ -89,6 +107,24 @@ router.get('/regions/:association/:region.kml', (req, res) => {
});
});
router.get('/regions/:association/:region.geojson', (req, res) => {
res.cacheControl = {
noCache: true
};
res.set('Content-Type', 'application/geo+json');
res.set('Content-Disposition', 'attachment; filename="' + req.params.association + '_' + req.params.region + '.geojson"');
geoJsonForQuery('^' + req.params.association + '/' + req.params.region + '-', req.query, (err, geoJson) => {
if (err) {
console.error(err);
res.status(500).end();
return;
}
res.json(geoJson).end();
});
});
function gpxForQuery(query, name, options, callback) {
let filter = {code: {$regex: query}};
@ -259,6 +295,43 @@ function kmlForRegion(associationCode, regionCode, options, callback) {
});
}
function geoJsonForQuery(query, options, callback) {
let filter = {code: {$regex: query}};
if (!options.inactive) {
filter.validFrom = {$lte: new Date()};
filter.validTo = {$gte: new Date()};
}
db.getDb().collection('summits').find(filter).sort({code: 1}).toArray((err, summits) => {
if (err) {
callback(err);
return;
}
let features = summits.map(summit => {
return {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [summit.coordinates.longitude, summit.coordinates.latitude]
},
id: summit.code,
properties: {
code: summit.code,
name: summit.name,
title: summitName(summit, options)
}
}
})
let geojson = {
type: 'FeatureCollection',
features
}
callback(null, geojson);
});
}
function summitName(summit, options) {
let name = summit.code;
let nameopts = [];