UNPKG

gtfs-bods

Version:

A CLI tool for processing UK Bus Open Data Service (BODS) GTFS data - import, export, and query transit data with ease

169 lines • 5.72 kB
import { getAgencies, getRoutes, getStops, getTrips, getStoptimes, getShapes, getCalendars } from 'gtfs'; export class GTFSQueries { /** * Get all agencies in the database */ static async getAllAgencies() { try { return await getAgencies(); } catch (error) { console.error('Error fetching agencies:', error); return []; } } /** * Get all routes, optionally filtered by agency */ static async getAllRoutes(options = {}) { try { const query = {}; if (options.agencyKey) { query.agency_key = options.agencyKey; } return await getRoutes(query); } catch (error) { console.error('Error fetching routes:', error); return []; } } /** * Get all stops, optionally filtered by agency */ static async getAllStops(options = {}) { try { const query = {}; if (options.agencyKey) { query.agency_key = options.agencyKey; } return await getStops(query); } catch (error) { console.error('Error fetching stops:', error); return []; } } /** * Get trips for a specific route */ static async getTripsForRoute(routeId, agencyKey) { try { const query = { route_id: routeId }; if (agencyKey) { query.agency_key = agencyKey; } return await getTrips(query); } catch (error) { console.error('Error fetching trips:', error); return []; } } /** * Get stop times for a specific trip */ static async getStopTimesForTrip(tripId, agencyKey) { try { const query = { trip_id: tripId }; if (agencyKey) { query.agency_key = agencyKey; } return await getStoptimes(query); } catch (error) { console.error('Error fetching stop times:', error); return []; } } /** * Get shapes (route geometry) for visualization */ static async getShapesForRoute(shapeId, agencyKey) { try { const query = { shape_id: shapeId }; if (agencyKey) { query.agency_key = agencyKey; } return await getShapes(query); } catch (error) { console.error('Error fetching shapes:', error); return []; } } /** * Find stops within a bounding box (alternative to radius search) */ static async findStopsInArea(minLat, minLon, maxLat, maxLon, agencyKey) { try { const allStops = await this.getAllStops({ agencyKey }); return allStops.filter(stop => { const lat = parseFloat(stop.stop_lat); const lon = parseFloat(stop.stop_lon); return lat >= minLat && lat <= maxLat && lon >= minLon && lon <= maxLon; }); } catch (error) { console.error('Error finding stops in area:', error); return []; } } /** * Get calendar information */ static async getCalendarInfo(agencyKey) { try { const query = {}; if (agencyKey) { query.agency_key = agencyKey; } return await getCalendars(query); } catch (error) { console.error('Error fetching calendar:', error); return []; } } /** * Generate a comprehensive report of the GTFS data */ static async generateReport(agencyKey) { console.log('\nšŸ“Š GTFS Database Report'); console.log('========================'); const agencies = await this.getAllAgencies(); const routes = agencyKey ? await this.getAllRoutes({ agencyKey }) : await this.getAllRoutes(); const stops = agencyKey ? await this.getAllStops({ agencyKey }) : await this.getAllStops(); const calendar = await this.getCalendarInfo(agencyKey); console.log(`\nšŸ¢ Agencies: ${agencies.length}`); agencies.forEach((agency, index) => { console.log(` ${index + 1}. ${agency.agency_name} (${agency.agency_id})`); if (agency.agency_url) console.log(` URL: ${agency.agency_url}`); if (agency.agency_timezone) console.log(` Timezone: ${agency.agency_timezone}`); }); console.log(`\n🚌 Routes: ${routes.length}`); if (routes.length > 0) { console.log(' Top 10 routes:'); routes.slice(0, 10).forEach((route, index) => { console.log(` ${index + 1}. ${route.route_short_name || route.route_id}: ${route.route_long_name || 'No description'}`); }); } console.log(`\nšŸš Stops: ${stops.length}`); if (stops.length > 0) { console.log(' Sample stops:'); stops.slice(0, 5).forEach((stop, index) => { console.log(` ${index + 1}. ${stop.stop_name} - ${stop.stop_lat}, ${stop.stop_lon}`); }); } console.log(`\nšŸ“… Calendar entries: ${calendar.length}`); if (calendar.length > 0) { console.log(' Sample calendar periods:'); calendar.slice(0, 3).forEach((cal, index) => { console.log(` ${index + 1}. Service ${cal.service_id}: ${cal.start_date} to ${cal.end_date}`); }); } } } //# sourceMappingURL=queries.js.map