Skip to content
This repository has been archived by the owner on May 4, 2024. It is now read-only.

Commit

Permalink
fix valhalla error (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
Seb-sti1 committed Dec 16, 2023
1 parent f983665 commit 181f0cc
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 34 deletions.
15 changes: 15 additions & 0 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"nestjs-knex": "^2.0.0",
"papaparse": "^5.4.1",
"pg": "^8.11.3",
"polyline": "^0.2.0",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1",
"sharp": "^0.33.0",
Expand Down
13 changes: 9 additions & 4 deletions backend/src/upload/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,15 @@ export function extractCoordinatesFromRSP(rspFilePath: string): LatLng[] {

for (let i = 0; i < datas.length; i++) {
const row = datas[i];
coordinates.push({
lng: Number(`${parseFloat(row[6])}`),
lat: Number(`${parseFloat(row[5])}`),
});
// to fix bugged RSP file of Ballerup
const exist = coordinates.find(
(c) => c.lat == parseFloat(row[5]) && c.lng == parseFloat(row[6]),
);
if (!exist)
coordinates.push({
lng: Number(`${parseFloat(row[6])}`),
lat: Number(`${parseFloat(row[5])}`),
});
}

return coordinates;
Expand Down
101 changes: 71 additions & 30 deletions backend/src/upload/valhalla.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { LatLng } from '../models';
import * as polyline from 'polyline';

/**
* Get the coordinates of the point at the given distance along the path
Expand Down Expand Up @@ -101,41 +102,81 @@ export async function valhalla(
shape: data.map((v) => ({ lat: v.lat, lon: v.lng })),
costing: 'auto',
shape_match: 'map_snap',
filters: {
attributes: [
'edge.way_id',
'edge.length',
'matched.distance_from_trace_point',
'matched.point',
'matched.type',
'matched.edge_index',
'matched.distance_along_edge',
],
action: 'include',
directions_options: {
units: 'km',
},
// filters: {
// attributes: [
// 'edge.way_id',
// 'edge.length',
// 'matched.distance_from_trace_point',
// 'matched.point',
// 'matched.type',
// 'matched.edge_index',
// 'matched.distance_along_edge',
// 'edge.begin_shape_index',
// 'edge.end_shape_index',
// ],
// action: 'include',
// },
};

return fetch(process.env.VALHALLA_ENDPOINT + '/trace_attributes', {
method: 'POST',
body: JSON.stringify(request),
headers: { 'Content-Type': 'application/json' },
}).then(async (res) => {
const json = await res.json();

if (json.error) return false;

return json.matched_points.map(
(p: {
edge_index: number;
distance_along_edge: number;
lat: number;
lon: number;
}) => ({
way_id: json.edges[p.edge_index]?.way_id,
distance_way:
p.distance_along_edge * json.edges[p.edge_index]?.length * 1000,
coordinates: { lat: p.lat, lng: p.lon },
}),
);
});
})
.then(async (res) => {
const json = await res.json();

if (json.error) return false;

let lastValidEdgeIndex = 0;

let shape = polyline.decode(json.shape, 6);
console.debug(shape);

return json.matched_points.map(
(p: {
edge_index: number;
distance_along_edge: number;
lat: number;
lon: number;
}) => {
// homemade fix for https://github.com/valhalla/valhalla/issues/3699
if (p.edge_index != 18446744073709551615)
lastValidEdgeIndex = p.edge_index;

const edge: null | {
way_id: number;
begin_shape_index: number;
end_shape_index: number;
length: number;
} = json.edges[lastValidEdgeIndex];

const distanceOfEdgeToBeginningOfWay = json.edges.filter(
(e: {
way_id: number;
begin_shape_index: number;
end_shape_index: number;
length: number;
}) =>
e.way_id == json.edges[lastValidEdgeIndex]?.way_id &&
e.end_shape_index <= edge.begin_shape_index,
);

return {
way_id: edge?.way_id,
distance_way:
distanceOfEdgeToBeginningOfWay +
p.distance_along_edge * edge?.length * 1000,
coordinates: { lat: p.lat, lng: p.lon },
};
},
);
})
.catch((err) => {
console.error("Error while fetching Valhalla's API: ", err);
return false;
});
}

0 comments on commit 181f0cc

Please sign in to comment.