From ac3732be892164129585334a8ba569104b2519da Mon Sep 17 00:00:00 2001 From: Matthias Pfeil Date: Fri, 24 Jan 2020 15:53:57 +0100 Subject: [PATCH 1/2] add geojson format --- README.md | 4 ++++ index.js | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/README.md b/README.md index 2b7908c..d2677e2 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,10 @@ Returns detailed information about a specific LUQS station Source: https://www.lanuv.nrw.de/luqs/messorte/steckbrief.php?ort={KUERZEL} +Optionally accepts an `options` object as first parameter: + +- `format: json|geojson` return the details in the specified format. + ### luqs.aktuell() Returns the current measurements for all LUQS stations. diff --git a/index.js b/index.js index 257c47b..622d28a 100644 --- a/index.js +++ b/index.js @@ -11,6 +11,11 @@ const messwerteUrl = 'https://www.lanuv.nrw.de/fileadmin/lanuv/luft/immissionen/ const MIN_KUERZEL_LENGTH = 4 const MAX_KUERZEL_LENGTH = 6 +const geojson = { + type: 'FeatureCollection', + features: [] +} + /** * Helper function to query a url and load * response with cheeriojs @@ -80,6 +85,7 @@ const luqs = (options = {}) => { * details of the station. * * @param string kuerzel + * @param options.format {String=json,geojson} returns the selected format * @returns Promise resolves with an object */ luqs.station = (kuerzel, options = {}) => { @@ -119,9 +125,28 @@ luqs.station = (kuerzel, options = {}) => { steckbrief.start_messung, steckbrief.ende_messung ] = tmpSteckbrief + steckbrief.image = `${messortBildUrl}${steckbrief.kuerzel.toUpperCase()}.jpg` steckbrief.longitude = steckbrief.longitude.replace(',', '.') steckbrief.latitude = steckbrief.latitude.replace(',', '.') + + if (options.format === 'geojson') { + geojson.features.push({ + type: 'Feature', + geometry: { + type: 'Point', + coordinates: [ + parseFloat(steckbrief.longitude), + parseFloat(steckbrief.latitude) + ] + }, + properties: { + ...steckbrief + } + }) + resolve(geojson) + } + resolve([steckbrief]) }) .catch(error => { From b28873c09142960b1bbf83f272f5a1c5c80f9c54 Mon Sep 17 00:00:00 2001 From: Matthias Pfeil Date: Fri, 24 Jan 2020 16:52:17 +0100 Subject: [PATCH 2/2] add review suggestions --- index.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 622d28a..f461a2f 100644 --- a/index.js +++ b/index.js @@ -11,11 +11,6 @@ const messwerteUrl = 'https://www.lanuv.nrw.de/fileadmin/lanuv/luft/immissionen/ const MIN_KUERZEL_LENGTH = 4 const MAX_KUERZEL_LENGTH = 6 -const geojson = { - type: 'FeatureCollection', - features: [] -} - /** * Helper function to query a url and load * response with cheeriojs @@ -131,13 +126,18 @@ luqs.station = (kuerzel, options = {}) => { steckbrief.latitude = steckbrief.latitude.replace(',', '.') if (options.format === 'geojson') { + const geojson = { + type: 'FeatureCollection', + features: [] + } + geojson.features.push({ type: 'Feature', geometry: { type: 'Point', coordinates: [ - parseFloat(steckbrief.longitude), - parseFloat(steckbrief.latitude) + Number(steckbrief.longitude), + Number(steckbrief.latitude) ] }, properties: { @@ -145,6 +145,7 @@ luqs.station = (kuerzel, options = {}) => { } }) resolve(geojson) + return } resolve([steckbrief])