67 lines
2.5 KiB
JavaScript
67 lines
2.5 KiB
JavaScript
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();
|