Improve place routing and loading
* Normalize OSM POIs and always use and store the OSM type and tags * Pass place objects to place route, do not load from API if passed * Construct place URLs with osm prefix including the type * Load specific type from API when given
This commit is contained in:
@@ -31,7 +31,9 @@ out center;
|
||||
const res = await this.fetchWithRetry(url, { signal });
|
||||
if (!res.ok) throw new Error('Overpass request failed');
|
||||
const data = await res.json();
|
||||
return data.elements;
|
||||
|
||||
// Normalize data
|
||||
return data.elements.map(this.normalizePoi);
|
||||
} catch (e) {
|
||||
if (e.name === 'AbortError') {
|
||||
console.log('Overpass request aborted');
|
||||
@@ -41,6 +43,19 @@ out center;
|
||||
}
|
||||
}
|
||||
|
||||
normalizePoi(poi) {
|
||||
return {
|
||||
title: poi.tags?.name || poi.tags?.['name:en'] || 'Untitled Place',
|
||||
lat: poi.lat || poi.center?.lat,
|
||||
lon: poi.lon || poi.center?.lon,
|
||||
url: poi.tags?.website,
|
||||
osmId: String(poi.id),
|
||||
osmType: poi.type,
|
||||
osmTags: poi.tags || {},
|
||||
description: poi.tags?.description,
|
||||
};
|
||||
}
|
||||
|
||||
async fetchWithRetry(url, options = {}, retries = 3) {
|
||||
try {
|
||||
const res = await fetch(url, options);
|
||||
@@ -64,22 +79,25 @@ out center;
|
||||
}
|
||||
}
|
||||
|
||||
async getPoiById(id) {
|
||||
// Assuming 'id' is just the numeric ID.
|
||||
// Overpass needs type(id). But we might not know the type (node, way, relation).
|
||||
// We can query all types for this ID.
|
||||
// However, typical usage often passes just the numeric ID.
|
||||
// A query for just ID(numeric) is tricky without type.
|
||||
// Let's assume 'node' first or try to query all three types by ID.
|
||||
async getPoiById(id, type = null) {
|
||||
// If type is provided, we can be specific.
|
||||
// If not, we query both node and way.
|
||||
let query;
|
||||
|
||||
const query = `
|
||||
if (type === 'node') {
|
||||
query = `[out:json][timeout:25];node(${id});out center;`;
|
||||
} else if (type === 'way') {
|
||||
query = `[out:json][timeout:25];way(${id});out center;`;
|
||||
} else {
|
||||
query = `
|
||||
[out:json][timeout:25];
|
||||
(
|
||||
node(${id});
|
||||
way(${id});
|
||||
);
|
||||
out center;
|
||||
`.trim();
|
||||
`.trim();
|
||||
}
|
||||
|
||||
const url = `https://overpass-api.de/api/interpreter?data=${encodeURIComponent(
|
||||
query
|
||||
@@ -87,6 +105,7 @@ out center;
|
||||
const res = await this.fetchWithRetry(url);
|
||||
if (!res.ok) throw new Error('Overpass request failed');
|
||||
const data = await res.json();
|
||||
return data.elements[0]; // Return the first match
|
||||
if (!data.elements[0]) return null;
|
||||
return this.normalizePoi(data.elements[0]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user