UNPKG

s2-tools

Version:

A collection of geospatial tools primarily designed for WGS84, Web Mercator, and S2.

224 lines 8.96 kB
// https://gtfs.org/documentation/schedule/reference/#agencytxt import { iterItems } from '../../../util'; import { parseGTFSAgencies } from './agency'; import { parseGTFSAreas } from './areas'; import { parseGTFSAttributions } from './attributions'; import { parseGTFSBookingRules } from './bookingRules'; import { parseGTFSCalendars } from './calendar'; import { parseGTFSCalendarDates } from './calendarDates'; import { parseGTFSFareAttributes } from './fareAttributes'; import { parseGTFSFareLegJoinRules } from './fareLegJoinRules'; import { parseGTFSFareLegRules } from './fareLegRules'; import { parseGTFSFareMedias } from './fareMedia'; import { parseGTFSFareProducts } from './fareProducts'; import { parseGTFSFareRules } from './fareRules'; import { parseGTFSFareTransferRules } from './fareTransferRules'; import { parseGTFSFeedInfos } from './feedInfo'; import { parseGTFSFrequencies } from './frequencies'; import { parseGTFSLevels } from './levels'; import { parseGTFSLocationGroups } from './locationGroups'; import { parseGTFSLocationGroupStops } from './locationGroupStops'; import { parseGTFSNetworks } from './networks'; import { parseGTFSPathways } from './pathways'; import { parseGTFSRoutes } from './routes'; import { parseGTFSRouteNetworks } from './routeNetworks'; import { parseGTFSShapes } from './shapes'; import { parseGTFSStops } from './stops'; import { parseGTFSStopAreas } from './stopAreas'; import { parseGTFSStopTimes } from './stopTimes'; import { parseGTFSTimeframes } from './timeframes'; import { parseGTFSTransfers } from './transfers'; import { parseGTFSTranslations } from './translations'; import { parseGTFSTrips } from './trips'; import { BufferJSONReader } from '../../'; export * from './agency'; export * from './areas'; export * from './attributions'; export * from './bookingRules'; export * from './calendar'; export * from './calendarDates'; export * from './fareAttributes'; export * from './fareLegJoinRules'; export * from './fareLegRules'; export * from './fareMedia'; export * from './fareProducts'; export * from './fareRules'; export * from './fareTransferRules'; export * from './feedInfo'; export * from './frequencies'; export * from './levels'; export * from './locationGroups'; export * from './locationGroupStops'; export * from './networks'; export * from './pathways'; export * from './routeNetworks'; export * from './routes'; export * from './shapes'; export * from './stopAreas'; export * from './stops'; export * from './stopTimes'; export * from './timeframes'; export * from './transfers'; export * from './translations'; export * from './trips'; /** * # GTFS Schedule Reader * * ## Description * Schedule class that pulls in all of the GTFS schedule files and parses them into a single object * implements the {@link FeatureIterator} interface. * * ## Usage * ```ts * import { buildGTFSSchedule } from 's2-tools'; * * const schedule = await buildGTFSSchedule(gzipData); * ``` */ export class GTFSScheduleReader { agencies; areas; attributions; bookingRules; calendar; calendarDates; fareAttributes; fareLegJoinRules; fareLegRules; fareMedia; fareProducts; fareRules; fareTransferRules; feedInfo; frequencies; levels; locationGroups; locationGroupStops; networks; pathways; routeNetworks; routes; shapes; stopAreas; stops; stopTimes; timeframes; transfers; translations; trips; geojson; /** @param pieces - all files */ constructor(pieces) { for (const { filename, data } of pieces) { if (filename === 'agency.txt') this.agencies = parseGTFSAgencies(data); else if (filename === 'areas.txt') this.areas = parseGTFSAreas(data); else if (filename === 'attributions.txt') this.attributions = parseGTFSAttributions(data); else if (filename === 'booking_rules.txt') this.bookingRules = parseGTFSBookingRules(data); else if (filename === 'calendar.txt') this.calendar = parseGTFSCalendars(data); else if (filename === 'calendar_dates.txt') this.calendarDates = parseGTFSCalendarDates(data); else if (filename === 'fare_attributes.txt') this.fareAttributes = parseGTFSFareAttributes(data); else if (filename === 'fare_leg_join_rules.txt') this.fareLegJoinRules = parseGTFSFareLegJoinRules(data); else if (filename === 'fare_leg_rules.txt') this.fareLegRules = parseGTFSFareLegRules(data); else if (filename === 'fare_media.txt') this.fareMedia = parseGTFSFareMedias(data); else if (filename === 'fare_products.txt') this.fareProducts = parseGTFSFareProducts(data); else if (filename === 'fare_rules.txt') this.fareRules = parseGTFSFareRules(data); else if (filename === 'fare_transfer_rules.txt') this.fareTransferRules = parseGTFSFareTransferRules(data); else if (filename === 'feed_info.txt') this.feedInfo = parseGTFSFeedInfos(data); else if (filename === 'frequencies.txt') this.frequencies = parseGTFSFrequencies(data); else if (filename === 'levels.txt') this.levels = parseGTFSLevels(data); else if (filename === 'location_groups.txt') this.locationGroups = parseGTFSLocationGroups(data); else if (filename === 'location_group_stops.txt') this.locationGroupStops = parseGTFSLocationGroupStops(data); else if (filename === 'networks.txt') this.networks = parseGTFSNetworks(data); else if (filename === 'pathways.txt') this.pathways = parseGTFSPathways(data); else if (filename === 'route_networks.txt') this.routeNetworks = parseGTFSRouteNetworks(data); else if (filename === 'routes.txt') this.routes = parseGTFSRoutes(data); else if (filename === 'shapes.txt') this.shapes = parseGTFSShapes(data); else if (filename === 'stop_areas.txt') this.stopAreas = parseGTFSStopAreas(data); else if (filename === 'stops.txt') this.stops = parseGTFSStops(data); else if (filename === 'stop_times.txt') this.stopTimes = parseGTFSStopTimes(data); else if (filename === 'timeframes.txt') this.timeframes = parseGTFSTimeframes(data); else if (filename === 'transfers.txt') this.transfers = parseGTFSTransfers(data); else if (filename === 'translations.txt') this.translations = parseGTFSTranslations(data); else if (filename === 'trips.txt') this.trips = parseGTFSTrips(data); else if (filename === 'locations.geojson') { // Defines zones where riders can request either pickup or drop off by on-demand services. // These zones are represented as GeoJSON polygons. this.geojson = new BufferJSONReader(data); } } } /** * TODO: Add proeprties from other files like "color" * TODO: All features should be parsed as VectorGeometry * Yields all of the shapes * @yields an iterator that contains shapes, stops, location data, and routes */ async *[Symbol.asyncIterator]() { if (this.geojson !== undefined) { for await (const feature of this.geojson) yield feature; } if (this.shapes !== undefined) { for (const shape of Object.values(this.shapes)) yield shape; } if (this.stops !== undefined) { for (const stop of Object.values(this.stops)) { const { lon, lat } = stop; if (lon !== undefined && lat !== undefined) { const stopFeature = { type: 'Feature', properties: stop.properties(), geometry: { type: 'Point', coordinates: [lon, lat] }, }; yield stopFeature; } } } } } /** * Builds a GTFSScheduleReader from a gzip folder * @param gzipData - the gzip folder to parse * @returns - a Schedule class */ export async function buildGTFSSchedule(gzipData) { const pieces = []; for (const item of iterItems(new Uint8Array(gzipData))) { const { filename } = item; const chunk = new TextDecoder('utf8').decode(await item.read()); pieces.push({ filename, data: chunk }); } return new GTFSScheduleReader(pieces); } //# sourceMappingURL=index.js.map