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) {
var options = {
host: process.env.NOMINATIM_URL || 'nominatim.openstreetmap.org',
port: '443',
path: `/lookup?format=json&osm_ids=${elementIds.join(',')}&extratags=1`,
path: `/lookup?format=json&osm_ids=${elementIds.join(',')}`,
method: 'GET',
headers: {
'User-Agent': 'https://gitlab.com/webthings/webthing-osm/',
......@@ -40,16 +40,5 @@ async function getElementById(type, id) {
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.getElementById = getElementById;
module.exports.transformElement = transformElement;
......@@ -24,16 +24,15 @@ function getOffsetToLocal(timezone) {
function requestOpeningHours(element) {
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);
console.log(date);
return {
open: oh.getState(date),
unknown: oh.getUnknown(date),
comment: oh.getComment(date) || '',
};
} catch(error) {
console.error(error);
return {
open: false,
unknown: true,
......@@ -43,7 +42,7 @@ function requestOpeningHours(element) {
}
router.get('/', async function (req, res) {
const name = (req.osmElement.nominatimElement.address || [])[0] || 'POI';
const name = req.osmElement.name || 'POI';
res.json({
name: `${name}: Opening Hours`,
type: 'thing',
......
'use strict';
const express = require('express');
const router = express.Router();
const geoTz = require('geo-tz');
const { getElementById, transformElement } = require('../apis/nominatim.js');
const { applyTimezone } = require('../helper/timezone.js');
const askNominatimById = require('../apis/nominatim.js').getElementById;
const askOverpassById = require('../apis/overpass.js').getElementById;
const { loadFromFile, getFromCache, saveToCache } = require('./cache.js');
loadFromFile();
......@@ -12,8 +13,17 @@ async function getOsmElement(type, id) {
let element = getFromCache(type, id);
if (!element) {
console.log(`Asking server for ${type}(${id})`);
element = transformElement(await getElementById(type, id));
applyTimezone(element);
const nominatimObject = await askNominatimById(type, id);
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);
}
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