routePath + Stops Location{}
Some checks failed
Deploy Express API / deploy (push) Has been cancelled

This commit is contained in:
Edward-1100
2025-11-28 16:17:28 -07:00
parent bfe47fb443
commit 06f3129fb9
3 changed files with 72 additions and 26 deletions

View File

@@ -6020,7 +6020,7 @@
] ]
}, },
{ {
"701": "703":
[ [
{ {
"shape_id": "238255", "shape_id": "238255",

View File

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

View File

@@ -5,7 +5,7 @@ const router = express.Router();
router.get("/routes", (req, res) => { router.get("/routes", (req, res) => {
const routes = dal.getRoutes(); const routes = dal.getRoutes();
res.json({meta: {returned: routes.length}, data: routes}); res.json({meta: {returned: routes.length}, data: routes});
}); });
@@ -14,10 +14,9 @@ router.get("/routes/:routeId", (req, res) => {
const route = dal.getRouteById(routeId); const route = dal.getRouteById(routeId);
if (!route) return res.status(404).json({error: "Route Was Not Found"}); if (!route) return res.status(404).json({error: "Route Was Not Found"});
const routePath = dal.getRoutePath(routeId);
const stations = dal.getStopsByRoute(routeId); const stations = dal.getStopsByRoute(routeId);
res.json({data: {route, routePath, stations}}); res.json({data: {route, stations}});
}); });
router.get("/route/:routeId", (req, res) => { router.get("/route/:routeId", (req, res) => {
@@ -25,10 +24,17 @@ router.get("/route/:routeId", (req, res) => {
const route = dal.getRouteById(routeId); const route = dal.getRouteById(routeId);
if (!route) return res.status(404).json({error: "Route Was Not Found"}); if (!route) return res.status(404).json({error: "Route Was Not Found"});
const routePath = dal.getRoutePath(routeId);
const stations = dal.getStopsByRoute(routeId); const stations = dal.getStopsByRoute(routeId);
res.json({data: {route, stations}});
});
res.json({data: {route, routePath, stations}}); router.get("/routepaths/:routeId", (req, res) => {
const routeId = req.params.routeId;
const rp = dal.getRoutePath(routeId);
if (!rp) return res.status(404).json({error: "RoutePath Was Not Found"});
res.json({meta: {routeId: String(routeId), returned: Array.isArray(rp) ? rp.length : 0}, data: rp});
}); });
router.get("/routes/:routeId/stations", (req, res) => { router.get("/routes/:routeId/stations", (req, res) => {