Files
gods-eye-view/src/layers/bikeshare/model.js
T
Sameh Khamis eb12303f46 refactor: move GBFS station parsing into a portable source module
The bike-share layer parsed station documents inside its model factory.
Move the parsers unchanged to src/sources/gbfsStations.js and have the
model import them, so code outside the layer can read GBFS stations
without constructing the layer.
2026-10-01 12:18:48 -07:00

52 lines
1.5 KiB
JavaScript

import {
extractStationsArray,
normalizeGbfsBool,
parseStationInformation,
parseStationStatus,
toNonNegativeInteger,
} from '../../sources/gbfsStations.js';
export function createModel({ state: layerState, services, parts, source }) {
/**
* Build a globally unique render key for a station.
* @param {string} cityId - City identifier.
* @param {string} stationId - Station identifier within the city.
* @returns {string} Composite key in "cityId:stationId" format.
*/
function stationKey(cityId, stationId) {
return `${cityId}:${stationId}`;
}
/**
* Compute the great-circle distance between two points using the Haversine formula.
* @param {number} aLat - Latitude of point A in degrees.
* @param {number} aLon - Longitude of point A in degrees.
* @param {number} bLat - Latitude of point B in degrees.
* @param {number} bLon - Longitude of point B in degrees.
* @returns {number} Distance in kilometers.
*/
function haversineKm(aLat, aLon, bLat, bLon) {
const toRad = (value) => (value * Math.PI) / 180;
const dLat = toRad(bLat - aLat);
const dLon = toRad(bLon - aLon);
const p1 = toRad(aLat);
const p2 = toRad(bLat);
const a =
Math.sin(dLat / 2) ** 2 +
Math.cos(p1) * Math.cos(p2) * Math.sin(dLon / 2) ** 2;
return 6371 * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
}
return {
normalizeGbfsBool,
toNonNegativeInteger,
stationKey,
haversineKm,
extractStationsArray,
parseStationInformation,
parseStationStatus,
};
}