Commit 34dbfd4d authored by Christian Paul's avatar Christian Paul 🦊

Ask both Nominatim and Overpass for information on new objects

parent 7eb4277a
...@@ -7,7 +7,7 @@ function askNominatim(elementIds) { ...@@ -7,7 +7,7 @@ function askNominatim(elementIds) {
var options = { var options = {
host: process.env.NOMINATIM_URL || 'nominatim.openstreetmap.org', host: process.env.NOMINATIM_URL || 'nominatim.openstreetmap.org',
port: '443', port: '443',
path: `/lookup?format=json&osm_ids=${elementIds.join(',')}&extratags=1`, path: `/lookup?format=json&osm_ids=${elementIds.join(',')}`,
method: 'GET', method: 'GET',
headers: { headers: {
'User-Agent': 'https://gitlab.com/webthings/webthing-osm/', 'User-Agent': 'https://gitlab.com/webthings/webthing-osm/',
...@@ -40,16 +40,5 @@ async function getElementById(type, id) { ...@@ -40,16 +40,5 @@ async function getElementById(type, id) {
return element; return element;
} }
function transformElement(nominatimElement) {
return {
id: nominatimElement.osm_id,
lat: nominatimElement.lat,
lon: nominatimElement.lon,
opening_hours: nominatimElement.extratags.opening_hours,
nominatimElement,
};
}
module.exports.askNominatim = askNominatim; module.exports.askNominatim = askNominatim;
module.exports.getElementById = getElementById; module.exports.getElementById = getElementById;
module.exports.transformElement = transformElement;
...@@ -24,16 +24,15 @@ function getOffsetToLocal(timezone) { ...@@ -24,16 +24,15 @@ function getOffsetToLocal(timezone) {
function requestOpeningHours(element) { function requestOpeningHours(element) {
try { try {
const oh = new opening_hours(element.opening_hours, element.nominatimElement); if (!element.opening_hours) throw new Error('No opening hours specified');
const oh = new opening_hours(element.opening_hours, element.nominatimObject);
const date = fakeTimezoneForDate(new Date(), element.timezone); const date = fakeTimezoneForDate(new Date(), element.timezone);
console.log(date);
return { return {
open: oh.getState(date), open: oh.getState(date),
unknown: oh.getUnknown(date), unknown: oh.getUnknown(date),
comment: oh.getComment(date) || '', comment: oh.getComment(date) || '',
}; };
} catch(error) { } catch(error) {
console.error(error);
return { return {
open: false, open: false,
unknown: true, unknown: true,
...@@ -43,7 +42,7 @@ function requestOpeningHours(element) { ...@@ -43,7 +42,7 @@ function requestOpeningHours(element) {
} }
router.get('/', async function (req, res) { router.get('/', async function (req, res) {
const name = (req.osmElement.nominatimElement.address || [])[0] || 'POI'; const name = req.osmElement.name || 'POI';
res.json({ res.json({
name: `${name}: Opening Hours`, name: `${name}: Opening Hours`,
type: 'thing', type: 'thing',
......
'use strict'; 'use strict';
const express = require('express'); const express = require('express');
const router = express.Router(); const router = express.Router();
const geoTz = require('geo-tz');
const { getElementById, transformElement } = require('../apis/nominatim.js'); const askNominatimById = require('../apis/nominatim.js').getElementById;
const { applyTimezone } = require('../helper/timezone.js'); const askOverpassById = require('../apis/overpass.js').getElementById;
const { loadFromFile, getFromCache, saveToCache } = require('./cache.js'); const { loadFromFile, getFromCache, saveToCache } = require('./cache.js');
loadFromFile(); loadFromFile();
...@@ -12,8 +13,17 @@ async function getOsmElement(type, id) { ...@@ -12,8 +13,17 @@ async function getOsmElement(type, id) {
let element = getFromCache(type, id); let element = getFromCache(type, id);
if (!element) { if (!element) {
console.log(`Asking server for ${type}(${id})`); console.log(`Asking server for ${type}(${id})`);
element = transformElement(await getElementById(type, id)); const nominatimObject = await askNominatimById(type, id);
applyTimezone(element); const osmObject = await askOverpassById(type, id);
const osmTags = osmObject.tags || {};
element = {
id: nominatimObject.osm_id,
name: osmTags.name,
opening_hours: osmTags.opening_hours,
timezone: geoTz(osmObject.lat, osmObject.lon)[0],
nominatimObject,
osmObject,
};
if (element) saveToCache(type, element); if (element) saveToCache(type, element);
} }
return element; return element;
......
'use strict';
const geoTz = require('geo-tz');
function applyTimezone(element) {
element.timezone = geoTz(element.lat, element.lon)[0];
}
module.exports.applyTimezone = applyTimezone;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment