- Implemented StopsMap component to display bus stops on a map using MapLibre GL. - Integrated walking directions feature in WalkingDirections component to calculate routes from user location to selected bus stop. - Added geo utility functions for coordinate validation and distance calculation. - Created route grouping and stop filtering logic in routeGroups module. - Developed custom hooks for managing stop routes with React Query. - Added tests for routing and stop functionalities to ensure reliability.
18 lines
1.1 KiB
JavaScript
18 lines
1.1 KiB
JavaScript
import { ApiError } from './errors.mjs';
|
|
import { coordinates } from './coordinates.mjs';
|
|
|
|
export function normalizeWalking(raw) {
|
|
if (['NoRoute', 'NoSegment'].includes(raw?.code)) throw new ApiError(404, 'walking_not_found');
|
|
const route = raw?.routes?.[0];
|
|
if (raw?.code !== 'Ok' || !route || typeof route.distance !== 'number' || !Number.isFinite(route.distance) || route.distance < 0
|
|
|| typeof route.duration !== 'number' || !Number.isFinite(route.duration) || route.duration < 0
|
|
|| route.geometry?.type !== 'LineString' || !Array.isArray(route.geometry.coordinates)
|
|
|| route.geometry.coordinates.length < 2 || route.geometry.coordinates.length > 100000) throw new ApiError(502);
|
|
const points = route.geometry.coordinates.map(point => {
|
|
if (!Array.isArray(point) || point.length !== 2 || typeof point[0] !== 'number' || typeof point[1] !== 'number'
|
|
|| Object.keys(coordinates(point[1], point[0])).length !== 2) throw new ApiError(502);
|
|
return [point[0], point[1]];
|
|
});
|
|
return { distance: route.distance, duration: route.duration, geometry: { type: 'LineString', coordinates: points } };
|
|
}
|