feat: (broken) implement route map and address autocomplete widgets with associated infrastructure testing scripts

This commit is contained in:
ElPoyo
2026-06-05 11:10:32 +02:00
parent 8c01a21728
commit 21d7bc8b87
40 changed files with 21078 additions and 30 deletions
+79
View File
@@ -0,0 +1,79 @@
const axios = require('axios');
const polylineLib = require('@mapbox/polyline');
require('dotenv').config({ path: '.env' });
const { _distKm } = require('./src/travel.js'); // Not exported, I'll copy the logic
function distKm(lat1, lng1, lat2, lng2) {
const dLat = (lat2 - lat1) * Math.PI / 180;
const dLng = (lng2 - lng1) * Math.PI / 180;
const a = Math.sin(dLat/2)**2 + Math.cos(lat1*Math.PI/180) * Math.cos(lat2*Math.PI/180) * Math.sin(dLng/2)**2;
return 6371 * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
}
async function bulkRateTest() {
const apiKey = process.env.API_MAPS;
const origin = "25 Impasse du Puits du Suc, Saint-Martin-en-Haut, France";
const destination = "Toulouse, France";
const routesUrl = 'https://routes.googleapis.com/directions/v2:computeRoutes';
const resToll = await axios.post(routesUrl, {
travelMode: 'DRIVE', routingPreference: 'TRAFFIC_UNAWARE',
origin: { address: origin }, destination: { address: destination },
}, { headers: { 'Content-Type': 'application/json', 'X-Goog-Api-Key': apiKey, 'X-Goog-FieldMask': 'routes.polyline.encodedPolyline' } });
const poly = resToll.data.routes[0].polyline.encodedPolyline;
const polylineCoords = polylineLib.decode(poly, 5);
const fs = require('fs');
const path = require('path');
const csvPath = path.join(__dirname, 'travel', 'gares_peage_export.csv');
const rawCsv = fs.readFileSync(csvPath, 'utf8');
const stations = [];
const lines = rawCsv.split('\n');
for (let i = 1; i < lines.length; i++) {
const l = lines[i].trim();
if (!l) continue;
const parts = l.split(',');
if (parts.length >= 4) {
const idStr = String(parts[0]).padStart(5, '0');
stations.push({
id: idStr,
operatorId: idStr.substring(0, 2),
tollId: idStr.substring(2, 5),
name: parts[1],
lat: parseFloat(parts[2]),
lon: parseFloat(parts[3]),
});
}
}
const candidates = [];
stations.forEach(s => {
let minDist = Infinity;
let minIndex = -1;
for (let i = 0; i < polylineCoords.length; i++) {
const d = distKm(s.lat, s.lon, polylineCoords[i][0], polylineCoords[i][1]);
if (d < minDist) { minDist = d; minIndex = i; }
}
if (minDist < 2) {
candidates.push({ ...s, polyIndex: minIndex });
}
});
candidates.sort((a, b) => a.polyIndex - b.polyIndex);
const passages = candidates.map(c => ({
toll: { operatorId: c.operatorId, tollId: c.tollId },
passageDate: new Date().toISOString()
}));
try {
console.log(`Sending ${passages.length} passages to Ulys...`);
const res = await axios.post('https://api-ulys.azure-api.net/tollstation/v1/rate', {
vehicleCategory: "2", paymentOption: 2, tollPassages: passages
});
console.log(JSON.stringify(res.data, null, 2));
} catch(e) {
console.log(e.response ? e.response.data : e.message);
}
}
bulkRateTest();
+17
View File
@@ -0,0 +1,17 @@
const travel = require('./src/travel.js');
require('dotenv').config({ path: '.env' });
const auth = require('./utils/auth.js');
auth.authenticateUser = async () => ({ uid: 'dummy' });
async function test() {
const req = {
headers: { authorization: 'Bearer dummy' },
body: { origin: "25 Impasse du Puits du Suc, Saint-Martin-en-Haut, France", destination: "Grenoble, France", vehicleCategory: "2" }
};
const res = {
status: function() { return this; },
json: function(data) { console.log(JSON.stringify(data, null, 2)); }
};
await travel.googleMapsComputeRoute(req, res);
}
test();
+32
View File
@@ -0,0 +1,32 @@
const axios = require('axios');
const polylineLib = require('@mapbox/polyline');
require('dotenv').config({ path: '.env' });
const { googleMapsComputeRoute } = require('./src/travel.js');
async function testGrenobleDetailed() {
const origin = "25 Impasse du Puits du Suc, Saint-Martin-en-Haut, France";
const destination = "Grenoble, France";
const req = {
headers: { authorization: 'Bearer MOCK' },
body: { origin, destination, vehicleTollCategory: 2 }
};
let resultBody = null;
const res = {
set: () => {}, status: () => res,
json: (data) => { resultBody = data; return res; },
send: (data) => { resultBody = data; return res; }
};
const auth = require('./utils/auth');
auth.authenticateUser = async () => {};
await googleMapsComputeRoute(req, res);
if (resultBody.error) {
console.error(`Error: ${resultBody.error}`);
} else {
console.log(JSON.stringify(resultBody.routes[0], null, 2));
}
}
testGrenobleDetailed();
+14
View File
@@ -0,0 +1,14 @@
const axios = require('axios');
async function getUlysRate(vehicleCategory, passages) {
const payload = {
vehicleCategory: String(vehicleCategory),
paymentOption: 2,
tollPassages: passages.map((p) => ({
toll: { operatorId: p.operatorId, tollId: p.tollId },
passageDate: new Date().toISOString(),
})),
};
const res = await axios.post('https://api-ulys.azure-api.net/tollstation/v1/rate', payload);
console.log(JSON.stringify(res.data, null, 2));
}
getUlysRate(2, [{operatorId: '03', tollId: '001'}, {operatorId: '03', tollId: '003'}]);
+37
View File
@@ -0,0 +1,37 @@
const axios = require('axios');
const polylineLib = require('@mapbox/polyline');
require('dotenv').config({ path: '.env' });
async function testHalfPolyline() {
const apiKey = process.env.API_MAPS;
const resToll = await axios.post('https://routes.googleapis.com/directions/v2:computeRoutes', {
travelMode: 'DRIVE', routingPreference: 'TRAFFIC_UNAWARE',
origin: { address: "25 Impasse du Puits du Suc, Saint-Martin-en-Haut, France" },
destination: { address: "Toulouse, France" },
}, { headers: { 'Content-Type': 'application/json', 'X-Goog-Api-Key': apiKey, 'X-Goog-FieldMask': 'routes.polyline.encodedPolyline' } });
const mainPoly = resToll.data.routes[0].polyline.encodedPolyline;
const mainCoords = polylineLib.decode(mainPoly, 5);
const halfCoords = mainCoords.slice(0, Math.floor(mainCoords.length / 2));
const halfPoly = polylineLib.encode(halfCoords, 5);
console.log(`Sending first half (${halfCoords.length} points)`);
const ulysUrl = `https://api-ulys.azure-api.net/placemark/v2/legs?precision=5&includeLayersIds=GaresPeage`;
try {
const res = await axios.post(ulysUrl, JSON.stringify(halfPoly), {
headers: { 'Content-Type': 'application/json' }
});
const feats = res.data.features || res.data;
console.log(`Found ${feats.length} gates.`);
feats.forEach(f => {
const pm = f.Placemark || f.placemark || {};
console.log(pm.Preview || pm.preview || "Gate");
});
} catch(e) {
console.log("Error:", e.message);
}
}
testHalfPolyline();
+66
View File
@@ -0,0 +1,66 @@
const axios = require('axios');
const polylineLib = require('@mapbox/polyline');
require('dotenv').config({ path: '.env' });
function distKm(lat1, lng1, lat2, lng2) {
const dLat = (lat2 - lat1) * Math.PI / 180;
const dLng = (lng2 - lng1) * Math.PI / 180;
const a = Math.sin(dLat/2)**2 + Math.cos(lat1*Math.PI/180) * Math.cos(lat2*Math.PI/180) * Math.sin(dLng/2)**2;
return 6371 * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
}
function interpolatePolyline(coords, maxDistKm = 0.05) {
const newCoords = [];
if(coords.length === 0) return newCoords;
newCoords.push(coords[0]);
for(let i=1; i<coords.length; i++) {
const p1 = coords[i-1];
const p2 = coords[i];
const d = distKm(p1[0], p1[1], p2[0], p2[1]);
if(d > maxDistKm) {
const steps = Math.ceil(d / maxDistKm);
for(let step=1; step<steps; step++) {
const fraction = step / steps;
const lat = p1[0] + (p2[0] - p1[0]) * fraction;
const lng = p1[1] + (p2[1] - p1[1]) * fraction;
newCoords.push([lat, lng]);
}
}
newCoords.push(p2);
}
return newCoords;
}
async function testInterpolatedToulouse() {
const apiKey = process.env.API_MAPS;
const resToll = await axios.post('https://routes.googleapis.com/directions/v2:computeRoutes', {
travelMode: 'DRIVE', routingPreference: 'TRAFFIC_UNAWARE',
origin: { address: "25 Impasse du Puits du Suc, Saint-Martin-en-Haut, France" },
destination: { address: "Toulouse, France" },
}, { headers: { 'Content-Type': 'application/json', 'X-Goog-Api-Key': apiKey, 'X-Goog-FieldMask': 'routes.polyline.encodedPolyline' } });
const poly = resToll.data.routes[0].polyline.encodedPolyline;
const coords = polylineLib.decode(poly, 5);
const interpolated = interpolatePolyline(coords, 0.05); // 50 meters
console.log(`Original points: ${coords.length}, Interpolated: ${interpolated.length}`);
const polyInt = polylineLib.encode(interpolated, 5);
const ulysUrl = `https://api-ulys.azure-api.net/placemark/v2/legs?precision=5&includeLayersIds=GaresPeage`;
try {
const res = await axios.post(ulysUrl, JSON.stringify(polyInt), {
headers: { 'Content-Type': 'application/json' }
});
const feats = res.data.features || res.data;
console.log(`Found ${feats.length} gates.`);
feats.forEach(f => {
const pm = f.Placemark || f.placemark || {};
console.log(pm.Preview || pm.preview || "Gate");
});
} catch(e) {
console.log("Error:", e.message);
}
}
testInterpolatedToulouse();
+147
View File
@@ -0,0 +1,147 @@
const axios = require('axios');
const fs = require('fs');
const csv = require('csv-parser');
const polylineLib = require('@mapbox/polyline');
require('dotenv').config({ path: '.env' });
function loadTollStations() {
return new Promise((resolve) => {
const csvPath = './travel/gares_peage_export.csv';
const results = [];
fs.createReadStream(csvPath)
.pipe(csv())
.on('data', (row) => {
if (row.id_gare && row.lat && row.lon) {
results.push({
id: row.id_gare,
operatorId: row.id_gare.substring(0, 2),
tollId: row.id_gare.substring(2, 5),
name: row.nom || '',
lat: parseFloat(row.lat),
lon: parseFloat(row.lon),
});
}
})
.on('end', () => resolve(results));
});
}
function _distKm(lat1, lng1, lat2, lng2) {
const dLat = (lat2 - lat1) * Math.PI / 180;
const dLng = (lng2 - lng1) * Math.PI / 180;
const a = Math.sin(dLat/2)**2 + Math.cos(lat1*Math.PI/180) * Math.cos(lat2*Math.PI/180) * Math.sin(dLng/2)**2;
return 6371 * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
}
async function getUlysRate(vehicleCategory, passages) {
try {
const payload = {
vehicleCategory: String(vehicleCategory),
paymentOption: 2,
tollPassages: passages.map((p) => ({
toll: { operatorId: p.operatorId, tollId: p.tollId },
passageDate: new Date().toISOString(),
})),
};
const res = await axios.post('https://api-ulys.azure-api.net/tollstation/v1/rate', payload);
const data = res.data;
if (Array.isArray(data) && data.length > 0) {
if (passages.length === 2) {
if (data.length !== 1 || !data[0].exitToll) return null;
return data[0].price > 0 ? data[0].price : null;
} else {
if (data.length === 1 && data[0].price > 0) return data[0].price;
const total = data.reduce((sum, d) => sum + (d.price || 0), 0);
return total > 0 ? total : null;
}
}
return null;
} catch (e) { return null; }
}
async function test() {
const origin = "25 Impasse du Puits du Suc, Saint-Martin-en-Haut, France";
const destination = "Nice, France";
const apiKey = process.env.API_MAPS;
const routesUrl = 'https://routes.googleapis.com/directions/v2:computeRoutes';
const res = await axios.post(routesUrl, {
travelMode: 'DRIVE', routingPreference: 'TRAFFIC_UNAWARE', origin: { address: origin }, destination: { address: destination },
}, { headers: { 'Content-Type': 'application/json', 'X-Goog-Api-Key': apiKey, 'X-Goog-FieldMask': 'routes.polyline.encodedPolyline' }});
const poly = res.data.routes[0].polyline.encodedPolyline;
const coords = polylineLib.decode(poly, 5);
const safePolyline = poly;
const url = 'https://api-ulys.azure-api.net/placemark/v2/legs?precision=5&includeLayersIds=GaresPeage';
const ulysRes = await axios.post(url, JSON.stringify(safePolyline), { headers: { 'Content-Type': 'application/json' } });
const items = Array.isArray(ulysRes.data) ? ulysRes.data : (ulysRes.data.features || []);
const stations = await loadTollStations();
const gates = [];
for (const item of items) {
const pm = item.Placemark || item.placemark || {};
const tags = pm.Tags || pm.tags || {};
let idStr = tags.ID_PEAGE;
if (!idStr && pm.Code) idStr = pm.Code.split('_')[0];
const s = stations.find(s => s.id === idStr);
if (s && !gates.find(g => g.id === idStr)) gates.push(s);
}
// Fallback for missing first system
let missingSystemPrice = 0;
if (gates.length > 0) {
const originLat = coords[0][0];
const originLng = coords[0][1];
const firstGate = gates[0];
const distToFirstGate = _distKm(originLat, originLng, firstGate.lat, firstGate.lon);
if (distToFirstGate > 50) {
console.log(`First gate ${firstGate.name} is ${Math.round(distToFirstGate)}km from origin. Checking for missing system...`);
// Find all geometric gates within 2km of the route, UP TO the firstGate
let firstGateIndex = 0;
for (let i = 0; i < coords.length; i++) {
if (_distKm(coords[i][0], coords[i][1], firstGate.lat, firstGate.lon) < 1) {
firstGateIndex = i; break;
}
}
const candidates = [];
stations.forEach(s => {
let minDist = Infinity;
let minIndex = -1;
for (let i = 0; i < firstGateIndex; i++) {
const d = _distKm(s.lat, s.lon, coords[i][0], coords[i][1]);
if (d < minDist) { minDist = d; minIndex = i; }
}
if (minDist < 2) {
candidates.push({ ...s, polyIndex: minIndex });
}
});
candidates.sort((a, b) => a.polyIndex - b.polyIndex);
if (candidates.length >= 2) {
// Try combinations from furthest to closest to find the longest closed system
let found = false;
for (let i = 0; i < Math.min(10, candidates.length); i++) {
for (let j = candidates.length - 1; j > i && j > candidates.length - 10; j--) {
if (candidates[i].operatorId !== candidates[j].operatorId) continue;
const price = await getUlysRate(2, [candidates[i], candidates[j]]);
if (price) {
console.log(`Found missing system: ${candidates[i].name} -> ${candidates[j].name} = ${price}`);
missingSystemPrice += price;
found = true;
break;
}
}
if (found) break;
}
}
}
}
console.log(`Missing system price: ${missingSystemPrice}`);
}
test();
+44
View File
@@ -0,0 +1,44 @@
const axios = require('axios');
const polylineLib = require('@mapbox/polyline');
require('dotenv').config({ path: '.env' });
async function directTestToulouse() {
const origin = "25 Impasse du Puits du Suc, Saint-Martin-en-Haut, France";
const destination = "Toulouse, France";
const apiKey = process.env.API_MAPS;
const routesUrl = 'https://routes.googleapis.com/directions/v2:computeRoutes';
const fieldMask = 'routes.distanceMeters,routes.duration,routes.polyline.encodedPolyline,routes.travelAdvisory.tollInfo';
const resToll = await axios.post(routesUrl, {
travelMode: 'DRIVE',
routingPreference: 'TRAFFIC_UNAWARE',
origin: { address: origin },
destination: { address: destination },
routeModifiers: { avoidTolls: false }
}, {
headers: {
'Content-Type': 'application/json',
'X-Goog-Api-Key': apiKey,
'X-Goog-FieldMask': fieldMask,
}
});
const poly = resToll.data.routes[0].polyline.encodedPolyline;
const decoded = polylineLib.decode(poly, 5);
const poly6 = polylineLib.encode(decoded, 6);
const ulysUrl = `https://api-ulys.azure-api.net/placemark/v2/legs?precision=6&includeLayersIds=GaresPeage`;
try {
const res = await axios.post(ulysUrl, JSON.stringify(poly6), {
headers: { 'Content-Type': 'application/json' }
});
console.log("Ulys Response (precision=6):");
console.log(res.data);
} catch(e) {
console.log("Ulys Error:", e.message);
if(e.response && e.response.data) console.log(e.response.data);
}
}
directTestToulouse();
+23
View File
@@ -0,0 +1,23 @@
const axios = require('axios');
async function testRate() {
const passages = [
{ operatorId: '04', tollId: '201' }, // VIENNE
{ operatorId: '04', tollId: '457' } // TOULOUSE-NORD/OUEST
];
const payload = {
vehicleCategory: "2",
paymentOption: 2,
tollPassages: passages.map((p) => ({
toll: { operatorId: p.operatorId, tollId: p.tollId },
passageDate: new Date().toISOString(),
})),
};
try {
const res = await axios.post('https://api-ulys.azure-api.net/tollstation/v1/rate', payload);
console.log("Rate:");
console.log(JSON.stringify(res.data, null, 2));
} catch (e) {
console.error(e.response ? e.response.data : e.message);
}
}
testRate();
+21
View File
@@ -0,0 +1,21 @@
const axios = require('axios');
async function testRate() {
const passages = [
{ operatorId: '04', tollId: '178' },
{ operatorId: '09', tollId: '079' }
];
const payload = {
vehicleCategory: "2", paymentOption: 2,
tollPassages: passages.map((p) => ({
toll: { operatorId: p.operatorId, tollId: p.tollId }, passageDate: new Date().toISOString(),
})),
};
try {
const res = await axios.post('https://api-ulys.azure-api.net/tollstation/v1/rate', payload);
console.log("Rate:");
console.log(JSON.stringify(res.data, null, 2));
} catch (e) {
console.error(e.response ? e.response.data : e.message);
}
}
testRate();
+23
View File
@@ -0,0 +1,23 @@
const axios = require('axios');
async function testRateVienneToulouseEst() {
const passages = [
{ operatorId: '04', tollId: '178' }, // MONTBRISON (04178)
{ operatorId: '04', tollId: '456' } // TOULOUSE-EST (04456)
];
const payload = {
vehicleCategory: "2",
paymentOption: 2,
tollPassages: passages.map((p) => ({
toll: { operatorId: p.operatorId, tollId: p.tollId },
passageDate: new Date().toISOString(),
})),
};
try {
const res = await axios.post('https://api-ulys.azure-api.net/tollstation/v1/rate', payload);
console.log("Rate MONTBRISON -> TOULOUSE-EST:");
console.log(JSON.stringify(res.data, null, 2));
} catch (e) {
console.error(e.response ? e.response.data : e.message);
}
}
testRateVienneToulouseEst();
+23
View File
@@ -0,0 +1,23 @@
const axios = require('axios');
async function testRateVienneToulouseEst() {
const passages = [
{ operatorId: '04', tollId: '201' }, // VIENNE (04201)
{ operatorId: '04', tollId: '456' } // TOULOUSE-EST (04456)
];
const payload = {
vehicleCategory: "2",
paymentOption: 2,
tollPassages: passages.map((p) => ({
toll: { operatorId: p.operatorId, tollId: p.tollId },
passageDate: new Date().toISOString(),
})),
};
try {
const res = await axios.post('https://api-ulys.azure-api.net/tollstation/v1/rate', payload);
console.log("Rate VIENNE -> TOULOUSE-EST:");
console.log(JSON.stringify(res.data, null, 2));
} catch (e) {
console.error(e.response ? e.response.data : e.message);
}
}
testRateVienneToulouseEst();
+40
View File
@@ -0,0 +1,40 @@
const axios = require('axios');
const polylineLib = require('@mapbox/polyline');
require('dotenv').config({ path: '.env' });
const { googleMapsComputeRoute } = require('./src/travel.js');
async function testRoute(destination, expectedPrice) {
const origin = "25 Impasse du Puits du Suc, Saint-Martin-en-Haut, France";
console.log(`\nTesting ${destination}...`);
const req = {
headers: { authorization: 'Bearer MOCK' },
body: { origin, destination, vehicleTollCategory: 2 }
};
let resultBody = null;
const res = {
set: () => {}, status: () => res,
json: (data) => { resultBody = data; return res; },
send: (data) => { resultBody = data; return res; }
};
await googleMapsComputeRoute(req, res);
if (resultBody.error) {
console.error(`Error: ${resultBody.error}`);
} else {
const toll = resultBody.routes && resultBody.routes.length > 0 ? resultBody.routes[0].tollCost : 0;
console.log(`Toll: ${toll}€ (Expected: ${expectedPrice}€)`);
}
}
async function run() {
// Mock Firebase auth specifically for this test
const auth = require('./utils/auth');
auth.authenticateUser = async () => {};
await testRoute("Saint-Denis, France", 64.3);
await testRoute("Grenoble, France", 21.7);
await testRoute("Nice, France", 77.2);
}
run();
+130
View File
@@ -0,0 +1,130 @@
const axios = require('axios');
const fs = require('fs');
const path = require('path');
require('dotenv').config({ path: '.env' });
const { _distKm } = require('./src/travel.js');
function distKm(lat1, lng1, lat2, lng2) {
const dLat = (lat2 - lat1) * Math.PI / 180;
const dLng = (lng2 - lng1) * Math.PI / 180;
const a = Math.sin(dLat/2)**2 + Math.cos(lat1*Math.PI/180) * Math.cos(lat2*Math.PI/180) * Math.sin(dLng/2)**2;
return 6371 * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
}
async function testTollSegments() {
const apiKey = process.env.API_MAPS;
const resToll = await axios.post('https://routes.googleapis.com/directions/v2:computeRoutes', {
travelMode: 'DRIVE', routingPreference: 'TRAFFIC_UNAWARE',
origin: { address: "25 Impasse du Puits du Suc, Saint-Martin-en-Haut, France" },
destination: { address: "Nice, France" },
}, { headers: { 'Content-Type': 'application/json', 'X-Goog-Api-Key': apiKey, 'X-Goog-FieldMask': 'routes.legs.steps.navigationInstruction,routes.legs.steps.distanceMeters,routes.legs.steps.startLocation,routes.legs.steps.endLocation,routes.legs.steps.polyline.encodedPolyline' } });
const steps = resToll.data.routes[0].legs[0].steps;
const rawCsv = fs.readFileSync(path.join(__dirname, 'travel', 'gares_peage_export.csv'), 'utf8');
const stations = [];
const lines = rawCsv.split('\n');
for (let i = 1; i < lines.length; i++) {
const l = lines[i].trim();
if (!l) continue;
const parts = l.split(',');
if (parts.length >= 4) {
const idStr = String(parts[0]).padStart(5, '0');
stations.push({
id: idStr, operatorId: idStr.substring(0, 2), tollId: idStr.substring(2, 5),
name: parts[1], lat: parseFloat(parts[2]), lon: parseFloat(parts[3]),
});
}
}
function getClosestGate(lat, lng) {
let minDist = Infinity;
let closest = null;
for(let s of stations) {
const d = distKm(lat, lng, s.lat, s.lon);
if(d < minDist) { minDist = d; closest = s; }
}
return minDist < 5 ? closest : null;
}
const segments = [];
let currentSegment = null;
for(let i=0; i<steps.length; i++) {
const step = steps[i];
const inst = step.navigationInstruction ? step.navigationInstruction.instructions : '';
const isToll = inst.toLowerCase().includes('péage') || inst.toLowerCase().includes('toll');
if (isToll) {
if (!currentSegment) {
currentSegment = { steps: [] };
}
currentSegment.steps.push(step);
} else {
if (currentSegment) {
segments.push(currentSegment);
currentSegment = null;
}
}
}
if (currentSegment) segments.push(currentSegment);
const polylineLib = require('@mapbox/polyline');
let totalToll = 0;
for (let i=0; i<segments.length; i++) {
const seg = segments[i];
let segCoords = [];
for(let step of seg.steps) {
if(step.polyline && step.polyline.encodedPolyline) {
segCoords = segCoords.concat(polylineLib.decode(step.polyline.encodedPolyline, 5));
}
}
const candidates = [];
stations.forEach(s => {
let minDist = Infinity;
let minIndex = -1;
for (let j = 0; j < segCoords.length; j++) {
const d = distKm(s.lat, s.lon, segCoords[j][0], segCoords[j][1]);
if (d < minDist) { minDist = d; minIndex = j; }
}
if (minDist < 2) { // must be within 2km of the segment
candidates.push({ ...s, polyIndex: minIndex });
}
});
candidates.sort((a, b) => a.polyIndex - b.polyIndex);
let entry = null, exit = null;
if (candidates.length > 0) {
entry = candidates[0];
exit = candidates[candidates.length - 1];
}
console.log(`Segment ${i+1}: points=${segCoords.length}, candidates=${candidates.length}, Entry=${entry?entry.name:'none'}, Exit=${exit?exit.name:'none'}`);
if (entry && exit) {
try {
const passages = [
{ operatorId: entry.operatorId, tollId: entry.tollId },
{ operatorId: exit.operatorId, tollId: exit.tollId }
];
const payload = {
vehicleCategory: "2", paymentOption: 2,
tollPassages: passages.map((p) => ({ toll: { operatorId: p.operatorId, tollId: p.tollId }, passageDate: new Date().toISOString() })),
};
const res = await axios.post('https://api-ulys.azure-api.net/tollstation/v1/rate', payload);
const data = res.data;
let price = 0;
if (data.length === 1 && data[0].price > 0) price = data[0].price;
if (data.length > 1) {
const pItem = data.find(d => d.price > 0);
if (pItem) price = pItem.price;
}
console.log(` -> Price: ${price}`);
totalToll += price;
} catch(e) { console.log(` -> Ulys Error`); }
}
}
console.log(`Total Toll: ${totalToll}`);
}
testTollSegments();
+21
View File
@@ -0,0 +1,21 @@
const axios = require('axios');
require('dotenv').config({ path: '.env' });
async function testSteps() {
const apiKey = process.env.API_MAPS;
const resToll = await axios.post('https://routes.googleapis.com/directions/v2:computeRoutes', {
travelMode: 'DRIVE', routingPreference: 'TRAFFIC_UNAWARE',
origin: { address: "25 Impasse du Puits du Suc, Saint-Martin-en-Haut, France" },
destination: { address: "Toulouse, France" },
}, { headers: { 'Content-Type': 'application/json', 'X-Goog-Api-Key': apiKey, 'X-Goog-FieldMask': 'routes.legs.steps.navigationInstruction,routes.legs.steps.distanceMeters,routes.legs.steps.startLocation,routes.legs.steps.endLocation' } });
const steps = resToll.data.routes[0].legs[0].steps;
for(let i=0; i<steps.length; i++) {
const step = steps[i];
const inst = step.navigationInstruction ? step.navigationInstruction.instructions : '';
if(inst.toLowerCase().includes('péage') || inst.toLowerCase().includes('toll')) {
console.log(`Step ${i}: ${inst} (Dist: ${step.distanceMeters}m)`);
}
}
}
testSteps();
+45
View File
@@ -0,0 +1,45 @@
const axios = require('axios');
const polylineLib = require('@mapbox/polyline');
require('dotenv').config({ path: '.env' });
async function testStepsPolyline() {
const apiKey = process.env.API_MAPS;
const resToll = await axios.post('https://routes.googleapis.com/directions/v2:computeRoutes', {
travelMode: 'DRIVE', routingPreference: 'TRAFFIC_UNAWARE',
origin: { address: "25 Impasse du Puits du Suc, Saint-Martin-en-Haut, France" },
destination: { address: "Toulouse, France" },
}, { headers: { 'Content-Type': 'application/json', 'X-Goog-Api-Key': apiKey, 'X-Goog-FieldMask': 'routes.polyline.encodedPolyline,routes.legs.steps.polyline.encodedPolyline' } });
const mainPoly = resToll.data.routes[0].polyline.encodedPolyline;
const mainCoords = polylineLib.decode(mainPoly, 5);
const steps = resToll.data.routes[0].legs[0].steps;
let stepCoords = [];
for(let step of steps) {
if(step.polyline && step.polyline.encodedPolyline) {
stepCoords = stepCoords.concat(polylineLib.decode(step.polyline.encodedPolyline, 5));
}
}
console.log(`Main polyline points: ${mainCoords.length}`);
console.log(`Steps combined points: ${stepCoords.length}`);
const combinedPoly = polylineLib.encode(stepCoords, 5);
const ulysUrl = `https://api-ulys.azure-api.net/placemark/v2/legs?precision=5&includeLayersIds=GaresPeage`;
try {
const res = await axios.post(ulysUrl, JSON.stringify(combinedPoly), {
headers: { 'Content-Type': 'application/json' }
});
const feats = res.data.features || res.data;
console.log(`Found ${feats.length} gates.`);
feats.forEach(f => {
const pm = f.Placemark || f.placemark || {};
console.log(pm.Preview || pm.preview || "Gate");
});
} catch(e) {
console.log("Error:", e.message);
}
}
testStepsPolyline();
+31
View File
@@ -0,0 +1,31 @@
const axios = require('axios');
require('dotenv').config({ path: '.env' });
const { googleMapsComputeRoute } = require('./src/travel.js');
async function testToulouse() {
const origin = "25 Impasse du Puits du Suc, Saint-Martin-en-Haut, France";
const destination = "Toulouse, France";
const req = {
headers: { authorization: 'Bearer MOCK' },
body: { origin, destination, vehicleTollCategory: 2 }
};
let resultBody = null;
const res = {
set: () => {}, status: () => res,
json: (data) => { resultBody = data; return res; },
send: (data) => { resultBody = data; return res; }
};
const auth = require('./utils/auth');
auth.authenticateUser = async () => {};
await googleMapsComputeRoute(req, res);
if (resultBody.error) {
console.error(`Error: ${resultBody.error}`);
} else {
console.log(JSON.stringify(resultBody.routes[0], null, 2));
}
}
testToulouse();
+30
View File
@@ -0,0 +1,30 @@
const axios = require('axios');
require('dotenv').config({ path: '.env' });
async function testUlysParams() {
const apiKey = process.env.API_MAPS;
const resToll = await axios.post('https://routes.googleapis.com/directions/v2:computeRoutes', {
travelMode: 'DRIVE', routingPreference: 'TRAFFIC_UNAWARE',
origin: { address: "25 Impasse du Puits du Suc, Saint-Martin-en-Haut, France" },
destination: { address: "Toulouse, France" },
}, { headers: { 'Content-Type': 'application/json', 'X-Goog-Api-Key': apiKey, 'X-Goog-FieldMask': 'routes.polyline.encodedPolyline' } });
const poly = resToll.data.routes[0].polyline.encodedPolyline;
const urls = [
`https://api-ulys.azure-api.net/placemark/v2/legs?precision=5&includeLayersIds=GaresPeage&radius=100`,
`https://api-ulys.azure-api.net/placemark/v2/legs?precision=5&includeLayersIds=GaresPeage&tolerance=100`,
`https://api-ulys.azure-api.net/placemark/v2/legs?precision=5&includeLayersIds=GaresPeage&distance=100`
];
for(let url of urls) {
try {
const res = await axios.post(url, JSON.stringify(poly), { headers: { 'Content-Type': 'application/json' } });
console.log(`URL: ${url}`);
console.log(`Found ${res.data.length || (res.data.features && res.data.features.length) || 0} gates`);
} catch(e) {
console.log(`Error on ${url}`);
}
}
}
testUlysParams();
+41
View File
@@ -0,0 +1,41 @@
const axios = require('axios');
const polylineLib = require('@mapbox/polyline');
require('dotenv').config({ path: '.env' });
async function directTestToulouse() {
const origin = "25 Impasse du Puits du Suc, Saint-Martin-en-Haut, France";
const destination = "Toulouse, France";
const apiKey = process.env.API_MAPS;
const routesUrl = 'https://routes.googleapis.com/directions/v2:computeRoutes';
const fieldMask = 'routes.distanceMeters,routes.duration,routes.polyline.encodedPolyline,routes.travelAdvisory.tollInfo';
const resToll = await axios.post(routesUrl, {
travelMode: 'DRIVE',
routingPreference: 'TRAFFIC_UNAWARE',
origin: { address: origin },
destination: { address: destination },
routeModifiers: { avoidTolls: false }
}, {
headers: {
'Content-Type': 'application/json',
'X-Goog-Api-Key': apiKey,
'X-Goog-FieldMask': fieldMask,
}
});
const poly = resToll.data.routes[0].polyline.encodedPolyline;
const ulysUrl = `https://api-ulys.azure-api.net/placemark/v2/legs?precision=5&includeLayersIds=GaresPeage`;
try {
const res = await axios.post(ulysUrl, JSON.stringify(poly), {
headers: { 'Content-Type': 'application/json' }
});
console.log("Ulys Response:");
console.log(res.data);
} catch(e) {
console.log("Ulys Error:", e.message);
if(e.response && e.response.data) console.log(e.response.data);
}
}
directTestToulouse();