Compare commits
1 Commits
postgres
...
456d486d39
| Author | SHA1 | Date | |
|---|---|---|---|
| 456d486d39 |
150
package-lock.json
generated
150
package-lock.json
generated
@@ -10,8 +10,7 @@
|
|||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
"express": "^5.1.0",
|
"express": "^5.1.0"
|
||||||
"pg": "^8.16.3"
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/accepts": {
|
"node_modules/accepts": {
|
||||||
@@ -582,135 +581,6 @@
|
|||||||
"url": "https://opencollective.com/express"
|
"url": "https://opencollective.com/express"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/pg": {
|
|
||||||
"version": "8.16.3",
|
|
||||||
"resolved": "https://registry.npmjs.org/pg/-/pg-8.16.3.tgz",
|
|
||||||
"integrity": "sha512-enxc1h0jA/aq5oSDMvqyW3q89ra6XIIDZgCX9vkMrnz5DFTw/Ny3Li2lFQ+pt3L6MCgm/5o2o8HW9hiJji+xvw==",
|
|
||||||
"license": "MIT",
|
|
||||||
"peer": true,
|
|
||||||
"dependencies": {
|
|
||||||
"pg-connection-string": "^2.9.1",
|
|
||||||
"pg-pool": "^3.10.1",
|
|
||||||
"pg-protocol": "^1.10.3",
|
|
||||||
"pg-types": "2.2.0",
|
|
||||||
"pgpass": "1.0.5"
|
|
||||||
},
|
|
||||||
"engines": {
|
|
||||||
"node": ">= 16.0.0"
|
|
||||||
},
|
|
||||||
"optionalDependencies": {
|
|
||||||
"pg-cloudflare": "^1.2.7"
|
|
||||||
},
|
|
||||||
"peerDependencies": {
|
|
||||||
"pg-native": ">=3.0.1"
|
|
||||||
},
|
|
||||||
"peerDependenciesMeta": {
|
|
||||||
"pg-native": {
|
|
||||||
"optional": true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"node_modules/pg-cloudflare": {
|
|
||||||
"version": "1.2.7",
|
|
||||||
"resolved": "https://registry.npmjs.org/pg-cloudflare/-/pg-cloudflare-1.2.7.tgz",
|
|
||||||
"integrity": "sha512-YgCtzMH0ptvZJslLM1ffsY4EuGaU0cx4XSdXLRFae8bPP4dS5xL1tNB3k2o/N64cHJpwU7dxKli/nZ2lUa5fLg==",
|
|
||||||
"license": "MIT",
|
|
||||||
"optional": true
|
|
||||||
},
|
|
||||||
"node_modules/pg-connection-string": {
|
|
||||||
"version": "2.9.1",
|
|
||||||
"resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-2.9.1.tgz",
|
|
||||||
"integrity": "sha512-nkc6NpDcvPVpZXxrreI/FOtX3XemeLl8E0qFr6F2Lrm/I8WOnaWNhIPK2Z7OHpw7gh5XJThi6j6ppgNoaT1w4w==",
|
|
||||||
"license": "MIT"
|
|
||||||
},
|
|
||||||
"node_modules/pg-int8": {
|
|
||||||
"version": "1.0.1",
|
|
||||||
"resolved": "https://registry.npmjs.org/pg-int8/-/pg-int8-1.0.1.tgz",
|
|
||||||
"integrity": "sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw==",
|
|
||||||
"license": "ISC",
|
|
||||||
"engines": {
|
|
||||||
"node": ">=4.0.0"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"node_modules/pg-pool": {
|
|
||||||
"version": "3.10.1",
|
|
||||||
"resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-3.10.1.tgz",
|
|
||||||
"integrity": "sha512-Tu8jMlcX+9d8+QVzKIvM/uJtp07PKr82IUOYEphaWcoBhIYkoHpLXN3qO59nAI11ripznDsEzEv8nUxBVWajGg==",
|
|
||||||
"license": "MIT",
|
|
||||||
"peerDependencies": {
|
|
||||||
"pg": ">=8.0"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"node_modules/pg-protocol": {
|
|
||||||
"version": "1.10.3",
|
|
||||||
"resolved": "https://registry.npmjs.org/pg-protocol/-/pg-protocol-1.10.3.tgz",
|
|
||||||
"integrity": "sha512-6DIBgBQaTKDJyxnXaLiLR8wBpQQcGWuAESkRBX/t6OwA8YsqP+iVSiond2EDy6Y/dsGk8rh/jtax3js5NeV7JQ==",
|
|
||||||
"license": "MIT"
|
|
||||||
},
|
|
||||||
"node_modules/pg-types": {
|
|
||||||
"version": "2.2.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/pg-types/-/pg-types-2.2.0.tgz",
|
|
||||||
"integrity": "sha512-qTAAlrEsl8s4OiEQY69wDvcMIdQN6wdz5ojQiOy6YRMuynxenON0O5oCpJI6lshc6scgAY8qvJ2On/p+CXY0GA==",
|
|
||||||
"license": "MIT",
|
|
||||||
"dependencies": {
|
|
||||||
"pg-int8": "1.0.1",
|
|
||||||
"postgres-array": "~2.0.0",
|
|
||||||
"postgres-bytea": "~1.0.0",
|
|
||||||
"postgres-date": "~1.0.4",
|
|
||||||
"postgres-interval": "^1.1.0"
|
|
||||||
},
|
|
||||||
"engines": {
|
|
||||||
"node": ">=4"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"node_modules/pgpass": {
|
|
||||||
"version": "1.0.5",
|
|
||||||
"resolved": "https://registry.npmjs.org/pgpass/-/pgpass-1.0.5.tgz",
|
|
||||||
"integrity": "sha512-FdW9r/jQZhSeohs1Z3sI1yxFQNFvMcnmfuj4WBMUTxOrAyLMaTcE1aAMBiTlbMNaXvBCQuVi0R7hd8udDSP7ug==",
|
|
||||||
"license": "MIT",
|
|
||||||
"dependencies": {
|
|
||||||
"split2": "^4.1.0"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"node_modules/postgres-array": {
|
|
||||||
"version": "2.0.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/postgres-array/-/postgres-array-2.0.0.tgz",
|
|
||||||
"integrity": "sha512-VpZrUqU5A69eQyW2c5CA1jtLecCsN2U/bD6VilrFDWq5+5UIEVO7nazS3TEcHf1zuPYO/sqGvUvW62g86RXZuA==",
|
|
||||||
"license": "MIT",
|
|
||||||
"engines": {
|
|
||||||
"node": ">=4"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"node_modules/postgres-bytea": {
|
|
||||||
"version": "1.0.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/postgres-bytea/-/postgres-bytea-1.0.0.tgz",
|
|
||||||
"integrity": "sha512-xy3pmLuQqRBZBXDULy7KbaitYqLcmxigw14Q5sj8QBVLqEwXfeybIKVWiqAXTlcvdvb0+xkOtDbfQMOf4lST1w==",
|
|
||||||
"license": "MIT",
|
|
||||||
"engines": {
|
|
||||||
"node": ">=0.10.0"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"node_modules/postgres-date": {
|
|
||||||
"version": "1.0.7",
|
|
||||||
"resolved": "https://registry.npmjs.org/postgres-date/-/postgres-date-1.0.7.tgz",
|
|
||||||
"integrity": "sha512-suDmjLVQg78nMK2UZ454hAG+OAW+HQPZ6n++TNDUX+L0+uUlLywnoxJKDou51Zm+zTCjrCl0Nq6J9C5hP9vK/Q==",
|
|
||||||
"license": "MIT",
|
|
||||||
"engines": {
|
|
||||||
"node": ">=0.10.0"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"node_modules/postgres-interval": {
|
|
||||||
"version": "1.2.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/postgres-interval/-/postgres-interval-1.2.0.tgz",
|
|
||||||
"integrity": "sha512-9ZhXKM/rw350N1ovuWHbGxnGh/SNJ4cnxHiM0rxE4VN41wsg8P8zWn9hv/buK00RP4WvlOyr/RBDiptyxVbkZQ==",
|
|
||||||
"license": "MIT",
|
|
||||||
"dependencies": {
|
|
||||||
"xtend": "^4.0.0"
|
|
||||||
},
|
|
||||||
"engines": {
|
|
||||||
"node": ">=0.10.0"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"node_modules/proxy-addr": {
|
"node_modules/proxy-addr": {
|
||||||
"version": "2.0.7",
|
"version": "2.0.7",
|
||||||
"resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz",
|
"resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz",
|
||||||
@@ -916,15 +786,6 @@
|
|||||||
"url": "https://github.com/sponsors/ljharb"
|
"url": "https://github.com/sponsors/ljharb"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/split2": {
|
|
||||||
"version": "4.2.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/split2/-/split2-4.2.0.tgz",
|
|
||||||
"integrity": "sha512-UcjcJOWknrNkF6PLX83qcHM6KHgVKNkV62Y8a5uYDVv9ydGQVwAHMKqHdJje1VTWpljG0WYpCDhrCdAOYH4TWg==",
|
|
||||||
"license": "ISC",
|
|
||||||
"engines": {
|
|
||||||
"node": ">= 10.x"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"node_modules/statuses": {
|
"node_modules/statuses": {
|
||||||
"version": "2.0.2",
|
"version": "2.0.2",
|
||||||
"resolved": "https://registry.npmjs.org/statuses/-/statuses-2.0.2.tgz",
|
"resolved": "https://registry.npmjs.org/statuses/-/statuses-2.0.2.tgz",
|
||||||
@@ -980,15 +841,6 @@
|
|||||||
"resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
|
"resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
|
||||||
"integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==",
|
"integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==",
|
||||||
"license": "ISC"
|
"license": "ISC"
|
||||||
},
|
|
||||||
"node_modules/xtend": {
|
|
||||||
"version": "4.0.2",
|
|
||||||
"resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz",
|
|
||||||
"integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==",
|
|
||||||
"license": "MIT",
|
|
||||||
"engines": {
|
|
||||||
"node": ">=0.4"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,6 @@
|
|||||||
"type": "module",
|
"type": "module",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
"express": "^5.1.0",
|
"express": "^5.1.0"
|
||||||
"pg": "^8.16.3"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6020,7 +6020,7 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"703":
|
"701":
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
"shape_id": "238255",
|
"shape_id": "238255",
|
||||||
|
|||||||
@@ -1,142 +0,0 @@
|
|||||||
import pkg from "pg";
|
|
||||||
const { Pool } = pkg;
|
|
||||||
|
|
||||||
const pool = new Pool({
|
|
||||||
user: "nate",
|
|
||||||
host: "localhost",
|
|
||||||
database: "gtfs",
|
|
||||||
password: "",
|
|
||||||
port: 5432,
|
|
||||||
});
|
|
||||||
|
|
||||||
// ---------------------- VEHICLES ----------------------
|
|
||||||
export async function getVehicles({ minLat, maxLat, minLng, maxLng, routeNum } = {}) {
|
|
||||||
let sql = `
|
|
||||||
SELECT vehicle_id, trip_id, route_id, ts, speed,
|
|
||||||
ST_Y(geom::geometry) AS latitude,
|
|
||||||
ST_X(geom::geometry) AS longitude
|
|
||||||
FROM rt_vehicles
|
|
||||||
`;
|
|
||||||
const params = [];
|
|
||||||
const conditions = [];
|
|
||||||
|
|
||||||
if (routeNum) {
|
|
||||||
conditions.push(`route_id = $${params.length + 1}`);
|
|
||||||
params.push(routeNum);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (minLat != null) {
|
|
||||||
conditions.push(`ST_Y(geom::geometry) >= $${params.length + 1}`);
|
|
||||||
params.push(minLat);
|
|
||||||
}
|
|
||||||
if (maxLat != null) {
|
|
||||||
conditions.push(`ST_Y(geom::geometry) <= $${params.length + 1}`);
|
|
||||||
params.push(maxLat);
|
|
||||||
}
|
|
||||||
if (minLng != null) {
|
|
||||||
conditions.push(`ST_X(geom::geometry) >= $${params.length + 1}`);
|
|
||||||
params.push(minLng);
|
|
||||||
}
|
|
||||||
if (maxLng != null) {
|
|
||||||
conditions.push(`ST_X(geom::geometry) <= $${params.length + 1}`);
|
|
||||||
params.push(maxLng);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (conditions.length) {
|
|
||||||
sql += " WHERE " + conditions.join(" AND ");
|
|
||||||
}
|
|
||||||
|
|
||||||
const { rows } = await pool.query(sql, params);
|
|
||||||
return rows.map(v => ({
|
|
||||||
vehicleId: v.vehicle_id,
|
|
||||||
tripId: v.trip_id,
|
|
||||||
routeId: v.route_id,
|
|
||||||
ts: Number(v.ts),
|
|
||||||
speed: v.speed,
|
|
||||||
location: { latitude: v.latitude, longitude: v.longitude }
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function getVehicleById(vehicleId) {
|
|
||||||
const sql = `
|
|
||||||
SELECT vehicle_id, trip_id, route_id, ts, speed,
|
|
||||||
ST_Y(geom::geometry) AS latitude,
|
|
||||||
ST_X(geom::geometry) AS longitude
|
|
||||||
FROM rt_vehicles
|
|
||||||
WHERE vehicle_id = $1
|
|
||||||
LIMIT 1
|
|
||||||
`;
|
|
||||||
const { rows } = await pool.query(sql, [vehicleId]);
|
|
||||||
if (!rows[0]) return null;
|
|
||||||
|
|
||||||
const v = rows[0];
|
|
||||||
return {
|
|
||||||
vehicleId: v.vehicle_id,
|
|
||||||
tripId: v.trip_id,
|
|
||||||
routeId: v.route_id,
|
|
||||||
ts: Number(v.ts),
|
|
||||||
speed: v.speed,
|
|
||||||
location: { latitude: v.latitude, longitude: v.longitude }
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------------------- ROUTES ----------------------
|
|
||||||
export async function getRoutes() {
|
|
||||||
const { rows } = await pool.query(`SELECT route_id, short_name, long_name, name FROM gtfs_routes`);
|
|
||||||
return rows;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function getRouteById(routeId) {
|
|
||||||
const { rows } = await pool.query(
|
|
||||||
`SELECT route_id, short_name, long_name, name FROM gtfs_routes WHERE route_id = $1 LIMIT 1`,
|
|
||||||
[routeId]
|
|
||||||
);
|
|
||||||
return rows[0] || null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------------------- STOPS ----------------------
|
|
||||||
export async function getStops() {
|
|
||||||
const { rows } = await pool.query(`
|
|
||||||
SELECT stop_id, stop_name, stop_lat, stop_lon
|
|
||||||
FROM gtfs_stops
|
|
||||||
`);
|
|
||||||
return rows;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function getStopById(stopId) {
|
|
||||||
const { rows } = await pool.query(`
|
|
||||||
SELECT stop_id, stop_name, stop_lat, stop_lon
|
|
||||||
FROM gtfs_stops
|
|
||||||
WHERE stop_id = $1
|
|
||||||
LIMIT 1
|
|
||||||
`, [stopId]);
|
|
||||||
return rows[0] || null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------------------- STOPS BY ROUTE ----------------------
|
|
||||||
export async function getStopsByRoute(routeId) {
|
|
||||||
const sql = `
|
|
||||||
SELECT s.stop_id, s.stop_name, s.stop_lat, s.stop_lon
|
|
||||||
FROM gtfs_stop_times st
|
|
||||||
JOIN gtfs_trips t ON st.trip_id = t.trip_id
|
|
||||||
JOIN gtfs_stops s ON st.stop_id = s.stop_id
|
|
||||||
WHERE t.route_id = $1
|
|
||||||
GROUP BY s.stop_id, s.stop_name, s.stop_lat, s.stop_lon
|
|
||||||
ORDER BY MIN(st.stop_sequence)
|
|
||||||
`;
|
|
||||||
const { rows } = await pool.query(sql, [routeId]);
|
|
||||||
return rows;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------------------- TRIP SCHEDULE ----------------------
|
|
||||||
export async function getScheduleByTrip(tripId) {
|
|
||||||
const sql = `
|
|
||||||
SELECT st.stop_sequence, s.stop_id, s.stop_name, st.arrival_time, st.departure_time
|
|
||||||
FROM gtfs_stop_times st
|
|
||||||
JOIN gtfs_stops s ON st.stop_id = s.stop_id
|
|
||||||
WHERE st.trip_id = $1
|
|
||||||
ORDER BY st.stop_sequence
|
|
||||||
`;
|
|
||||||
const { rows } = await pool.query(sql, [tripId]);
|
|
||||||
return rows;
|
|
||||||
}
|
|
||||||
@@ -14,11 +14,6 @@ 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");
|
||||||
|
|
||||||
@@ -31,11 +26,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 : toNumber(v.bearing),
|
bearing: v.bearing == null ? undefined : Number(v.bearing),
|
||||||
speed: v.speed == null ? undefined : toNumber(v.speed),
|
speed: v.speed == null ? undefined : Number(v.speed),
|
||||||
location: {
|
location: {
|
||||||
latitude: toNumber(loc.latitude),
|
latitude: loc.latitude == null ? undefined : Number(loc.latitude),
|
||||||
longitude: toNumber(loc.longitude)
|
longitude: loc.longitude == null ? undefined : Number(loc.longitude)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -60,40 +55,22 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
function readRoutepathsRaw() {
|
|
||||||
const raw = readJSON("routePath.json");
|
|
||||||
|
|
||||||
if (!Array.isArray(raw)) return [];
|
|
||||||
return raw;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getRoutePathsMap() {
|
export function getRoutePathsMap() {
|
||||||
const raw = readRoutepathsRaw();
|
const raw = readJSON("routepaths.json");
|
||||||
const map = {};
|
|
||||||
|
|
||||||
for (const item of raw) {
|
if (raw && typeof raw === "object" && !Array.isArray(raw)) return raw;
|
||||||
if (!item || typeof item !== "object" || Array.isArray(item)) continue;
|
return {};
|
||||||
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, key) ? map[key] : null;
|
return Object.prototype.hasOwnProperty.call(map, routeId) ? map[routeId] : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getStationsRaw() {
|
export function getStationsRaw() {
|
||||||
@@ -101,7 +78,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)) {
|
||||||
@@ -116,29 +93,13 @@ 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)) {
|
||||||
const arr = raw[routeId];
|
return Array.isArray(raw[routeId]) ? raw[routeId] : [];
|
||||||
if (!Array.isArray(arr)) return [];
|
|
||||||
return arr.map(transformationStation);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Array.isArray(raw)) {
|
if (Array.isArray(raw)) {
|
||||||
@@ -146,12 +107,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.map(transformationStation));
|
if (Array.isArray(arr)) matchesFromArrayMaps.push(...arr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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))).map(transformationStation);
|
return raw.filter(s => Array.isArray(s.lines) && s.lines.map(String).includes(String(routeId)));
|
||||||
}
|
}
|
||||||
|
|
||||||
return [];
|
return [];
|
||||||
@@ -167,7 +128,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 transformationStation(found);
|
if (found) return found;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -178,14 +139,13 @@ 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));
|
const found = arr.find(s => String(s.stop_id) === String(id) || String(s.stationId) === String(id));
|
||||||
if (found) return transformationStation(found);
|
if (found) return found;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
const found = raw.find(s => String(s.stop_id) === String(id) || String(s.stationId) === String(id));
|
return raw.find(s => String(s.stop_id) === String(id) || String(s.stationId) === String(id)) || null;
|
||||||
return found ? transformationStation(found) : null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
@@ -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,9 +14,10 @@ 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, stations}});
|
res.json({data: {route, routePath, stations}});
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get("/route/:routeId", (req, res) => {
|
router.get("/route/:routeId", (req, res) => {
|
||||||
@@ -24,17 +25,10 @@ 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}});
|
|
||||||
});
|
|
||||||
|
|
||||||
router.get("/routepaths/:routeId", (req, res) => {
|
res.json({data: {route, routePath, stations}});
|
||||||
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) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user