|
|
|
|
@@ -14,6 +14,11 @@ function readJSON(name) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function toNumber(v) {
|
|
|
|
|
const n = Number(v);
|
|
|
|
|
return Number.isFinite(n) ? n : undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getVehicles() {
|
|
|
|
|
const raw = readJSON("vehicles.json");
|
|
|
|
|
|
|
|
|
|
@@ -26,11 +31,11 @@ export function getVehicles() {
|
|
|
|
|
routeNum: v.routeNum,
|
|
|
|
|
routeName: v.routeName,
|
|
|
|
|
destination: v.destination,
|
|
|
|
|
bearing: v.bearing == null ? undefined : Number(v.bearing),
|
|
|
|
|
speed: v.speed == null ? undefined : Number(v.speed),
|
|
|
|
|
bearing: v.bearing == null ? undefined : toNumber(v.bearing),
|
|
|
|
|
speed: v.speed == null ? undefined : toNumber(v.speed),
|
|
|
|
|
location: {
|
|
|
|
|
latitude: loc.latitude == null ? undefined : Number(loc.latitude),
|
|
|
|
|
longitude: loc.longitude == null ? undefined : Number(loc.longitude)
|
|
|
|
|
latitude: toNumber(loc.latitude),
|
|
|
|
|
longitude: toNumber(loc.longitude)
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
@@ -55,22 +60,40 @@ export function getRoutes() {
|
|
|
|
|
export function getRouteById(routeId) {
|
|
|
|
|
if (routeId == null) return null;
|
|
|
|
|
const routes = getRoutes();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return routes.find(r => String(r.route_id) === String(routeId)) || null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getRoutePathsMap() {
|
|
|
|
|
const raw = readJSON("routepaths.json");
|
|
|
|
|
function readRoutepathsRaw() {
|
|
|
|
|
const raw = readJSON("routePath.json");
|
|
|
|
|
|
|
|
|
|
if (raw && typeof raw === "object" && !Array.isArray(raw)) return raw;
|
|
|
|
|
return {};
|
|
|
|
|
if (!Array.isArray(raw)) return [];
|
|
|
|
|
return raw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getRoutePathsMap() {
|
|
|
|
|
const raw = readRoutepathsRaw();
|
|
|
|
|
const map = {};
|
|
|
|
|
|
|
|
|
|
for (const item of raw) {
|
|
|
|
|
if (!item || typeof item !== "object" || Array.isArray(item)) continue;
|
|
|
|
|
const keys = Object.keys(item);
|
|
|
|
|
|
|
|
|
|
if (keys.length !== 1) continue;
|
|
|
|
|
const k = String(keys[0]);
|
|
|
|
|
|
|
|
|
|
if (Array.isArray(item[k])) map[k] = item[k];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return map;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getRoutePath(routeId) {
|
|
|
|
|
if (routeId == null) return null;
|
|
|
|
|
const map = getRoutePathsMap();
|
|
|
|
|
const key = String(routeId);
|
|
|
|
|
|
|
|
|
|
return Object.prototype.hasOwnProperty.call(map, routeId) ? map[routeId] : null;
|
|
|
|
|
return Object.prototype.hasOwnProperty.call(map, key) ? map[key] : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getStationsRaw() {
|
|
|
|
|
@@ -78,7 +101,7 @@ export function getStationsRaw() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getStations() {
|
|
|
|
|
const raw = getStationsRaw();
|
|
|
|
|
const raw = getStationsRaw();
|
|
|
|
|
|
|
|
|
|
if (raw == null) return [];
|
|
|
|
|
if (Array.isArray(raw)) {
|
|
|
|
|
@@ -93,13 +116,29 @@ export function getStations() {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function transformationStation(s) {
|
|
|
|
|
return {
|
|
|
|
|
stop_id: s.stop_id,
|
|
|
|
|
stop_code: s.stop_code,
|
|
|
|
|
stop_name: s.stop_name,
|
|
|
|
|
stop_desc: s.stop_desc,
|
|
|
|
|
location: { latitude: toNumber(s.stop_lat), longitude: toNumber(s.stop_lon) },
|
|
|
|
|
stop_url: s.stop_url,
|
|
|
|
|
location_type: s.location_type,
|
|
|
|
|
parent_station: s.parent_station,
|
|
|
|
|
lines: s.lines
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getStopsByRoute(routeId) {
|
|
|
|
|
if (routeId == null) return [];
|
|
|
|
|
const raw = getStationsRaw();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (raw == null) return [];
|
|
|
|
|
if (typeof raw === "object" && !Array.isArray(raw)) {
|
|
|
|
|
return Array.isArray(raw[routeId]) ? raw[routeId] : [];
|
|
|
|
|
const arr = raw[routeId];
|
|
|
|
|
if (!Array.isArray(arr)) return [];
|
|
|
|
|
return arr.map(transformationStation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Array.isArray(raw)) {
|
|
|
|
|
@@ -107,12 +146,12 @@ export function getStopsByRoute(routeId) {
|
|
|
|
|
for (const item of raw) {
|
|
|
|
|
if (typeof item === "object" && !Array.isArray(item) && Object.prototype.hasOwnProperty.call(item, routeId)) {
|
|
|
|
|
const arr = item[routeId];
|
|
|
|
|
if (Array.isArray(arr)) matchesFromArrayMaps.push(...arr);
|
|
|
|
|
if (Array.isArray(arr)) matchesFromArrayMaps.push(...arr.map(transformationStation));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (matchesFromArrayMaps.length) return matchesFromArrayMaps;
|
|
|
|
|
|
|
|
|
|
return raw.filter(s => Array.isArray(s.lines) && s.lines.map(String).includes(String(routeId)));
|
|
|
|
|
return raw.filter(s => Array.isArray(s.lines) && s.lines.map(String).includes(String(routeId))).map(transformationStation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [];
|
|
|
|
|
@@ -128,7 +167,7 @@ export function getStationById(id) {
|
|
|
|
|
const arr = raw[key];
|
|
|
|
|
if (!Array.isArray(arr)) continue;
|
|
|
|
|
const found = arr.find(s => String(s.stop_id) === String(id) || String(s.stationId) === String(id));
|
|
|
|
|
if (found) return found;
|
|
|
|
|
if (found) return transformationStation(found);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
@@ -139,13 +178,14 @@ export function getStationById(id) {
|
|
|
|
|
for (const item of raw) {
|
|
|
|
|
for (const key of Object.keys(item)) {
|
|
|
|
|
const arr = item[key];
|
|
|
|
|
const found = arr.find(s => String(s.stop_id) === String(id) || String(s.stationId) === String(id));
|
|
|
|
|
if (found) return found;
|
|
|
|
|
const found = arr.find(s => String(s.stop_id) === String(id));
|
|
|
|
|
if (found) return transformationStation(found);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return raw.find(s => String(s.stop_id) === String(id) || String(s.stationId) === String(id)) || null;
|
|
|
|
|
const found = raw.find(s => String(s.stop_id) === String(id) || String(s.stationId) === String(id));
|
|
|
|
|
return found ? transformationStation(found) : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|