Better nominatim
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
soraefir
2025-02-25 01:38:34 +01:00
parent b554eba76c
commit 0cc9d235ed
7 changed files with 149 additions and 195 deletions

View File

@@ -1,110 +1,142 @@
export const throttle = (func: () => void, wait: number) => {
let lastTime = 0;
let timeoutId: ReturnType<typeof setTimeout> | undefined;
let lastArgs: any[];
let lastTime = 0;
let timeoutId: ReturnType<typeof setTimeout> | undefined;
let lastArgs: any[];
return function (...args: any[]) {
const now = Date.now();
lastArgs = args;
return function (...args: any[]) {
const now = Date.now();
lastArgs = args;
if (now - lastTime >= wait) {
lastTime = now;
func.apply(this, lastArgs);
} else {
if (timeoutId) clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
lastTime = Date.now();
func.apply(this, lastArgs);
}, wait - (now - lastTime));
}
};
}
if (now - lastTime >= wait) {
lastTime = now;
func.apply(this, lastArgs);
} else {
if (timeoutId) clearTimeout(timeoutId);
timeoutId = setTimeout(
() => {
lastTime = Date.now();
func.apply(this, lastArgs);
},
wait - (now - lastTime)
);
}
};
};
export const load = (id: string) =>
fetch("/api/" + id).then(res => {
if (!res.ok) throw new Error('Error ' + res.statusText);
return res.json();
}).then((res) => {
for (let e of res.main) {
if (e.date_range) {
e.date_range[0] = new Date(e.date_range[0]);
e.date_range[1] = new Date(e.date_range[1]);
}
e.step_title = e.step_title || [];
}
return res;
})
fetch("/api/" + id)
.then((res) => {
if (!res.ok) throw new Error("Error " + res.statusText);
return res.json();
})
.then((res) => {
for (let e of res.main) {
if (e.date_range) {
e.date_range[0] = new Date(e.date_range[0]);
e.date_range[1] = new Date(e.date_range[1]);
}
e.day_title = e.day_title || [];
}
return res;
});
export const save = (id: string, v: journey) =>
fetch("/api/" + id, { method: "post", body: JSON.stringify(v) })
.then(res => {
if (!res.ok) throw new Error('Error ' + res.statusText);
return res.json();
}).then((_res) => {
console.log("Saved...");
})
fetch("/api/" + id, { method: "post", body: JSON.stringify(v) })
.then((res) => {
if (!res.ok) throw new Error("Error " + res.statusText);
return res.json();
})
.then((_res) => {
console.log("Saved...");
});
export const query_nominatim = (
q: string,
f: (v: string) => Boolean = () => true,
) =>
fetch("/api/place/" + q)
.then((res) => (res.status == 200) ? res.json() : [])
.then((res) => res.filter(f));
q: string,
bb: any,
f: (v: string) => Boolean = () => true
) => {
let url = new URL("/api/place/" + q, window.location.origin);
url.searchParams.append("id", q);
url.searchParams.append("bb", JSON.stringify(bb));
return fetch(url)
.then((res) => (res.status == 200 ? res.json() : []))
.then((res) => res.filter(f));
};
export const query_flight = (q: string) =>
fetch("/api/flight/" + q).then((res) => res.json());
fetch("/api/flight/" + q).then((res) => res.json());
type NominatimResult = {
type: string;
category: string;
display_name: string; // DEBUG ONLY
type: string;
category: string;
display_name: string; // DEBUG ONLY
};
export const is_restauration_type = (e: NominatimResult) =>
["restaurant", "cafe", "pub", "bar", "fast_food", "food_court"].indexOf(
e.type,
) != -1;
["restaurant", "cafe", "pub", "bar", "fast_food", "food_court"].indexOf(
e.type
) != -1;
export const is_attraction_type = (e: NominatimResult): boolean =>
[
"tourism",
"leisure",
"place",
"amenity",
"highway",
"historic",
"natural",
"waterway",
].indexOf(e.category) != -1 ||
[
"place_of_worship",
"national_park",
"nature_reserve",
"protected_area",
].indexOf(e.type) != -1;
[
"tourism",
"leisure",
"place",
"amenity",
"highway",
"historic",
"natural",
"waterway",
].indexOf(e.category) != -1 ||
[
"place_of_worship",
"national_park",
"nature_reserve",
"protected_area",
].indexOf(e.type) != -1;
export const icon_type = (item: NominatimResult): string => {
let t = item.type;
let c = item.category;
let types = {
"utensils": ["restaurant", "cafe", "pub", "bar", "fast_food", "food_court"],
"bed": ["hotel", "hostel", "guest_house"],
"landmark": ["museum", "historic", "place_of_worship", "attraction", "information", "university"],
"mountain": ["peak", "viewpoint"],
"parking": ["parking"],
"water": ["water", "river", "lake", "torrent", "aquarium"],
"building": ["community_center", "locality"],
"archway": ["bridge"],
"tree": ["woodland", "shieling", "national_park", "park", "zoo", "garden", "nature_reserve"],
"dice-five": ["water_park", "theme_park", "casino"],
"": ["?", "neighbourhood", "quarter", "highway"],
}
let t = item.type;
let c = item.category;
let types = {
utensils: [
"restaurant",
"cafe",
"pub",
"bar",
"fast_food",
"food_court",
],
bed: ["hotel", "hostel", "guest_house"],
landmark: [
"museum",
"historic",
"place_of_worship",
"attraction",
"information",
"university",
],
mountain: ["peak", "viewpoint"],
parking: ["parking"],
water: ["water", "river", "lake", "torrent", "aquarium"],
building: ["community_center", "locality"],
archway: ["bridge"],
tree: [
"woodland",
"shieling",
"national_park",
"park",
"zoo",
"garden",
"nature_reserve",
],
"dice-five": ["water_park", "theme_park", "casino"],
"": ["?", "neighbourhood", "quarter", "highway"],
};
for (let k in types) {
if (types[k].indexOf(t) >= 0 || types[k].indexOf(c) >= 0)
return k;
}
console.log(item.display_name, item.category, item.type);
return "question";
for (let k in types) {
if (types[k].indexOf(t) >= 0 || types[k].indexOf(c) >= 0) return k;
}
console.log(item.display_name, item.category, item.type);
return "question";
};

View File

@@ -37,14 +37,25 @@ const app = new Vue({
}
},
methods: {
start_journey: () => window.location.href = "/" + this.journey.id,
start_journey: function(){ window.location.href = "/" + this.journey.id},
compute_bb: function(){
const map = this.$refs.map[0].mapObject;
var bounds = map.getBounds();
var minLng = bounds.getSouthWest().lng;
var minLat = bounds.getSouthWest().lat;
var maxLng = bounds.getNorthEast().lng;
var maxLat = bounds.getNorthEast().lat;
return [[minLng,minLat],[maxLng,maxLat]]
},
search_nominatim: function (txt, f) {
if (txt == "") {
this.query.nominatim = [];
return Promise.resolve([]);
}
return api.query_nominatim(txt, f).catch((err) => []).then((results) => {
let bb = this.journey.leg_get();
return api.query_nominatim(txt,this.compute_bb(), f).catch((err) => []).then((results) => {
results.forEach((r) => {
r.latlon = [parseFloat(r.lat), parseFloat(r.lon)];
r.sname = r.display_name.split(",")[0];