2023-12-29 22:53:27 +08:00
|
|
|
import {dijkstra, haversine_distance, obvious_dijkstra, obvious_a_star} from "../tools/ShortestPath";
|
2023-12-27 14:34:03 +08:00
|
|
|
import {sill, sill_unwrap, noexcept} from "../tools/Debug";
|
|
|
|
import {get_row} from "../tools/Misc";
|
|
|
|
import benchmark from "../tools/PathBench";
|
2023-12-25 00:06:07 +08:00
|
|
|
|
2023-12-29 22:53:27 +08:00
|
|
|
const __spa = obvious_a_star;
|
2023-12-25 00:06:07 +08:00
|
|
|
|
|
|
|
function find_nearest_node_id(nodes, point) {
|
|
|
|
const [lat, lon] = point;
|
|
|
|
let min_distance = 1e100;
|
2023-12-27 14:34:03 +08:00
|
|
|
let res = '';
|
2023-12-25 00:06:07 +08:00
|
|
|
for (let node_id in nodes) {
|
|
|
|
const curr_distance = haversine_distance(nodes[node_id], point);
|
|
|
|
if (curr_distance < min_distance) {
|
|
|
|
min_distance = curr_distance;
|
|
|
|
res = node_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2023-12-27 11:06:29 +08:00
|
|
|
const shortest_path = noexcept((nodes, ways, start_point, end_point) => {
|
2023-12-27 14:34:03 +08:00
|
|
|
// const clean_nodes = nodes;
|
2023-12-27 11:06:29 +08:00
|
|
|
const count = {};
|
2023-12-27 14:34:03 +08:00
|
|
|
const aff = {};
|
2023-12-27 17:16:47 +08:00
|
|
|
const location = {};
|
2023-12-27 14:34:03 +08:00
|
|
|
const clean_nodes = {};
|
2023-12-27 17:16:47 +08:00
|
|
|
// TODO: delete this
|
|
|
|
let count1 = 0, tot = 0;
|
2023-12-27 11:06:29 +08:00
|
|
|
for (const way_id in ways) {
|
2023-12-27 14:34:03 +08:00
|
|
|
// sill_unwrap(way_id);
|
|
|
|
const st = new Set();
|
2023-12-27 11:06:29 +08:00
|
|
|
const [l, n] = get_row(ways, way_id);
|
|
|
|
for (let i = 0; i < n; ++i) {
|
2023-12-27 17:16:47 +08:00
|
|
|
l[i] = l[i].toString(); // critical
|
2023-12-27 14:34:03 +08:00
|
|
|
const curr = l[i];
|
|
|
|
if (st.has(curr)) continue;
|
|
|
|
st.add(curr);
|
2023-12-27 17:16:47 +08:00
|
|
|
if (location[curr]) {
|
|
|
|
location[curr][way_id] = i;
|
|
|
|
} else {
|
|
|
|
location[curr] = {[way_id]: i};
|
|
|
|
}
|
2023-12-27 14:34:03 +08:00
|
|
|
clean_nodes[curr] = nodes[curr];
|
|
|
|
if (count[curr]) {
|
2023-12-27 17:16:47 +08:00
|
|
|
if (count[curr] === 1) {
|
|
|
|
count1 -= 1;
|
|
|
|
}
|
2023-12-27 14:34:03 +08:00
|
|
|
++count[curr];
|
2023-12-27 11:06:29 +08:00
|
|
|
} else {
|
2023-12-27 14:34:03 +08:00
|
|
|
count[curr] = 1;
|
2023-12-27 17:16:47 +08:00
|
|
|
count1 += 1;
|
|
|
|
tot += 1;
|
2023-12-27 11:06:29 +08:00
|
|
|
}
|
2023-12-27 14:34:03 +08:00
|
|
|
if (aff[curr]) {
|
|
|
|
aff[curr][way_id] = true;
|
2023-12-27 11:06:29 +08:00
|
|
|
} else {
|
2023-12-27 14:34:03 +08:00
|
|
|
aff[curr] = {[way_id]: true};
|
2023-12-25 00:06:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-12-27 17:16:47 +08:00
|
|
|
sill(`One ratio: ${count1} / ${tot}`);
|
2023-12-27 14:36:43 +08:00
|
|
|
// sill_unwrap(aff);
|
2023-12-27 11:06:29 +08:00
|
|
|
const actual_start_node_id = find_nearest_node_id(clean_nodes, start_point);
|
|
|
|
const actual_end_node_id = find_nearest_node_id(clean_nodes, end_point);
|
2023-12-27 14:36:43 +08:00
|
|
|
// sill_unwrap(typeof (actual_start_node_id));
|
2023-12-27 14:34:03 +08:00
|
|
|
// sill(count);
|
|
|
|
const ch_dict = {};
|
2023-12-27 17:16:47 +08:00
|
|
|
const ch_dict_bench = {};
|
2023-12-27 14:34:03 +08:00
|
|
|
let f = 1;
|
|
|
|
for (const t in ways) {
|
2023-12-27 14:36:43 +08:00
|
|
|
// if (t === ways[aff[actual_start_node_id]]) sill('yes');
|
2023-12-27 14:34:03 +08:00
|
|
|
const [l, n] = get_row(ways, t);
|
2023-12-27 17:16:47 +08:00
|
|
|
// // TODO: delete this
|
|
|
|
// for (let i = 1; i < n; ++i) {
|
|
|
|
// const curr = l[i];
|
|
|
|
// const prev = l[i-1];
|
|
|
|
// const distance = haversine_distance(nodes[curr], nodes[prev]);
|
|
|
|
// if (ch_dict_bench[curr]) {
|
|
|
|
// ch_dict_bench[curr].push([prev, distance]);
|
|
|
|
// } else {
|
|
|
|
// ch_dict_bench[curr] = [[prev, distance]];
|
|
|
|
// }
|
|
|
|
// if (ch_dict_bench[prev]) {
|
|
|
|
// ch_dict_bench[prev].push([curr, distance]);
|
|
|
|
// } else {
|
|
|
|
// ch_dict_bench[prev] = [[curr, distance]];
|
|
|
|
// }
|
|
|
|
// }
|
2023-12-27 14:34:03 +08:00
|
|
|
let prev = '';
|
2023-12-27 17:16:47 +08:00
|
|
|
let distance = 0;
|
2023-12-27 14:34:03 +08:00
|
|
|
for (let i = 0; i < n; ++i) {
|
2023-12-27 17:16:47 +08:00
|
|
|
const curr = l[i];
|
|
|
|
if (i) distance += haversine_distance(nodes[curr], nodes[l[i-1]]);
|
2023-12-27 14:34:03 +08:00
|
|
|
// if(true) {
|
|
|
|
if (count[curr] > 1 || curr === actual_end_node_id || curr === actual_start_node_id) {
|
2023-12-27 14:36:43 +08:00
|
|
|
// if (curr === actual_start_node_id) sill(curr === actual_start_node_id);
|
2023-12-27 14:34:03 +08:00
|
|
|
if (prev !== '') {
|
|
|
|
if (ch_dict[curr]) {
|
2023-12-27 17:16:47 +08:00
|
|
|
ch_dict[curr].push([prev, distance]);
|
2023-12-27 14:34:03 +08:00
|
|
|
} else {
|
2023-12-27 17:16:47 +08:00
|
|
|
ch_dict[curr] = [[prev, distance]];
|
2023-12-27 14:34:03 +08:00
|
|
|
}
|
|
|
|
if (ch_dict[prev]) {
|
2023-12-27 17:16:47 +08:00
|
|
|
ch_dict[prev].push([curr, distance]);
|
2023-12-27 14:34:03 +08:00
|
|
|
} else {
|
2023-12-27 17:16:47 +08:00
|
|
|
ch_dict[prev] = [[curr, distance]];
|
2023-12-27 14:34:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
prev = curr;
|
2023-12-27 17:16:47 +08:00
|
|
|
distance = 0;
|
2023-12-27 14:34:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// const clean_nodes = {};
|
|
|
|
// Object.keys(nodes).forEach((node_id) => {
|
|
|
|
// if (ch_dict[node_id]) clean_nodes[node_id] = nodes[node_id];
|
|
|
|
// });
|
|
|
|
sill(`start distance: ${haversine_distance(nodes[actual_start_node_id], start_point)}`);
|
|
|
|
sill(`dest distance: ${haversine_distance(nodes[actual_end_node_id], end_point)}`);
|
2023-12-27 11:06:29 +08:00
|
|
|
sill("calling __spa...");
|
2023-12-27 17:16:47 +08:00
|
|
|
// // TODO: delete this
|
|
|
|
// const seq = __spa(nodes, clean_nodes, ways, location, ch_dict, ch_dict_bench, count, aff, actual_start_node_id, actual_end_node_id);
|
|
|
|
const seq = __spa(clean_nodes, ways, location, ch_dict, count, aff, actual_start_node_id, actual_end_node_id);
|
2023-12-27 11:06:29 +08:00
|
|
|
const res = [end_point];
|
|
|
|
seq.forEach((node_id) => {
|
|
|
|
if (clean_nodes[node_id]) res.push(clean_nodes[node_id]);
|
|
|
|
});
|
|
|
|
res.push(start_point);
|
|
|
|
return res;
|
|
|
|
});
|
2023-12-25 00:06:07 +08:00
|
|
|
|
2023-11-26 22:17:55 +08:00
|
|
|
|
2023-12-27 11:06:29 +08:00
|
|
|
export default function handler(req, res) {
|
|
|
|
const pts = JSON.parse(req.body);
|
|
|
|
const latRange = pts.map((row) => row[0]),
|
|
|
|
lonRange = pts.map((row) => row[1]);
|
|
|
|
const minlon = Math.min(...lonRange) - 0.01, minlat = Math.min(...latRange) - 0.01,
|
|
|
|
maxlon = Math.max(...lonRange) + 0.01, maxlat = Math.max(...latRange) + 0.01;
|
2023-12-27 14:34:03 +08:00
|
|
|
if (haversine_distance([minlat, minlon], [maxlat, maxlon]) > 100) {
|
|
|
|
res.status(500);
|
2023-12-27 17:16:47 +08:00
|
|
|
sill('rejected');
|
2023-12-27 14:34:03 +08:00
|
|
|
return;
|
|
|
|
}
|
2023-12-27 11:06:29 +08:00
|
|
|
const request_uri = `https://www.overpass-api.de/api/interpreter?data=[out:json];way[highway](${minlat},${minlon},${maxlat},${maxlon});(._;>;);out body;`;
|
|
|
|
sill(`Requesting ${request_uri}`);
|
|
|
|
const fetch_debug_response = fetch(request_uri).then((response) => {
|
2023-11-27 12:21:22 +08:00
|
|
|
return response.json();
|
2023-11-26 22:17:55 +08:00
|
|
|
});
|
2023-12-27 11:06:29 +08:00
|
|
|
fetch_debug_response.then((debug_response) => {
|
2023-12-27 14:34:03 +08:00
|
|
|
// sill(debug_response);
|
2023-12-25 00:06:07 +08:00
|
|
|
let ps = {};
|
|
|
|
let ws = {};
|
2023-12-27 11:06:29 +08:00
|
|
|
debug_response.elements.forEach((it) => {
|
2023-12-25 00:06:07 +08:00
|
|
|
if (it.type === "node") {
|
2023-12-27 11:06:29 +08:00
|
|
|
ps[it.id] = [it.lat, it.lon];
|
2023-12-25 00:06:07 +08:00
|
|
|
} else if (it.type === "way") {
|
|
|
|
ws[it.id] = it.nodes;
|
|
|
|
}
|
|
|
|
});
|
2023-12-27 11:06:29 +08:00
|
|
|
const path_found = shortest_path(ps, ws, pts[0], pts[pts.length - 1]);
|
2023-11-27 12:21:22 +08:00
|
|
|
res.status(200).json({
|
|
|
|
log: `Method: click\nArgs: ${pts}\nStatus: requested "${request_uri}", got response ${JSON.stringify(debug_response.elements)}`,
|
2023-12-25 00:06:07 +08:00
|
|
|
multipolyline: JSON.stringify(path_found),
|
|
|
|
// __debug_pts: ps
|
2023-11-27 12:21:22 +08:00
|
|
|
});
|
2023-12-27 11:06:29 +08:00
|
|
|
}).catch(e => {
|
|
|
|
console.debug(e);
|
2023-11-27 12:21:22 +08:00
|
|
|
res.status(500);
|
|
|
|
});
|
|
|
|
|
2023-11-26 22:17:55 +08:00
|
|
|
}
|