gtfs-bods
Version:
A CLI tool for processing UK Bus Open Data Service (BODS) GTFS data - import, export, and query transit data with ease
122 lines β’ 5.98 kB
JavaScript
import { GTFSQueries } from './queries.js';
/**
* Example usage of the GTFS query utilities
*/
async function runExamples() {
try {
console.log('π GTFS Query Examples');
console.log('=====================\n');
// Example 1: Generate comprehensive report
console.log('π Example 1: Comprehensive Database Report');
console.log('-------------------------------------------');
await GTFSQueries.generateReport();
// Example 2: Get specific agency data
console.log('\nπ’ Example 2: Agency-specific Data');
console.log('----------------------------------');
const agencies = await GTFSQueries.getAllAgencies();
if (agencies.length > 0) {
const firstAgency = agencies[0];
console.log(`Selected agency: ${firstAgency.agency_name} (${firstAgency.agency_id})`);
const routes = await GTFSQueries.getAllRoutes({ agencyKey: firstAgency.agency_key });
console.log(`Routes for this agency: ${routes.length}`);
if (routes.length > 0) {
console.log('Sample routes:');
routes.slice(0, 3).forEach((route, index) => {
console.log(` ${index + 1}. ${route.route_short_name || route.route_id}: ${route.route_long_name || 'No description'}`);
});
}
}
// Example 3: Route details
console.log('\nπ Example 3: Route and Trip Details');
console.log('------------------------------------');
const allRoutes = await GTFSQueries.getAllRoutes();
if (allRoutes.length > 0) {
const sampleRoute = allRoutes[0];
console.log(`Analyzing route: ${sampleRoute.route_short_name || sampleRoute.route_id}`);
const trips = await GTFSQueries.getTripsForRoute(sampleRoute.route_id, sampleRoute.agency_key);
console.log(`Trips for this route: ${trips.length}`);
if (trips.length > 0) {
const sampleTrip = trips[0];
console.log(`Sample trip: ${sampleTrip.trip_id}`);
const stopTimes = await GTFSQueries.getStopTimesForTrip(sampleTrip.trip_id, sampleTrip.agency_key);
console.log(`Stops on this trip: ${stopTimes.length}`);
if (stopTimes.length > 0) {
console.log('First few stops:');
stopTimes.slice(0, 3).forEach((stopTime, index) => {
console.log(` ${index + 1}. Stop ${stopTime.stop_id} at ${stopTime.arrival_time || 'N/A'}`);
});
}
}
}
// Example 4: Geographic queries
console.log('\nπΊοΈ Example 4: Geographic Queries');
console.log('----------------------------------');
const allStops = await GTFSQueries.getAllStops();
if (allStops.length > 0) {
// Find stops in London area (approximate)
const londonStops = await GTFSQueries.findStopsInArea(51.3, -0.5, // min lat, min lon
51.7, 0.3 // max lat, max lon
);
console.log(`Stops in London area: ${londonStops.length}`);
if (londonStops.length > 0) {
console.log('Sample London stops:');
londonStops.slice(0, 3).forEach((stop, index) => {
console.log(` ${index + 1}. ${stop.stop_name} - ${stop.stop_lat}, ${stop.stop_lon}`);
});
}
}
// Example 5: Calendar information
console.log('\nπ
Example 5: Service Calendar');
console.log('------------------------------');
const calendar = await GTFSQueries.getCalendarInfo();
console.log(`Total calendar entries: ${calendar.length}`);
if (calendar.length > 0) {
console.log('Service periods:');
calendar.slice(0, 5).forEach((cal, index) => {
const days = [];
if (cal.monday)
days.push('Mon');
if (cal.tuesday)
days.push('Tue');
if (cal.wednesday)
days.push('Wed');
if (cal.thursday)
days.push('Thu');
if (cal.friday)
days.push('Fri');
if (cal.saturday)
days.push('Sat');
if (cal.sunday)
days.push('Sun');
console.log(` ${index + 1}. Service ${cal.service_id}: ${cal.start_date} to ${cal.end_date} (${days.join(', ')})`);
});
}
// Example 6: Shapes (route geometry)
console.log('\nπ£οΈ Example 6: Route Shapes');
console.log('----------------------------');
const routesWithShapes = allRoutes.filter(route => route.shape_id);
console.log(`Routes with shape data: ${routesWithShapes.length}`);
if (routesWithShapes.length > 0) {
const routeWithShape = routesWithShapes[0];
console.log(`Route with shape: ${routeWithShape.route_short_name || routeWithShape.route_id}`);
const shapes = await GTFSQueries.getShapesForRoute(routeWithShape.shape_id, routeWithShape.agency_key);
console.log(`Shape points: ${shapes.length}`);
if (shapes.length > 0) {
console.log('Sample shape points:');
shapes.slice(0, 3).forEach((shape, index) => {
console.log(` ${index + 1}. Point ${shape.shape_pt_sequence}: ${shape.shape_pt_lat}, ${shape.shape_pt_lon}`);
});
}
}
console.log('\nβ
All examples completed successfully!');
}
catch (error) {
console.error('β Error running examples:', error);
}
}
// Run examples if this file is executed directly
if (import.meta.url === `file://${process.argv[1]}`) {
runExamples();
}
export { runExamples };
//# sourceMappingURL=examples.js.map