gis-tools-ts
Version:
A collection of geospatial tools primarily designed for WGS84, Web Mercator, and S2.
234 lines • 9.5 kB
JavaScript
// https://gtfs.org/documentation/schedule/reference/#agencytxt
import { BufferJSONReader, iterZipFolder } from '../../../index.js';
import { parseGTFSAgencies } from './agency.js';
import { parseGTFSAreas } from './areas.js';
import { parseGTFSAttributions } from './attributions.js';
import { parseGTFSBookingRules } from './bookingRules.js';
import { parseGTFSCalendars } from './calendar.js';
import { parseGTFSCalendarDates } from './calendarDates.js';
import { parseGTFSFareAttributes } from './fareAttributes.js';
import { parseGTFSFareLegJoinRules } from './fareLegJoinRules.js';
import { parseGTFSFareLegRules } from './fareLegRules.js';
import { parseGTFSFareMedias } from './fareMedia.js';
import { parseGTFSFareProducts } from './fareProducts.js';
import { parseGTFSFareRules } from './fareRules.js';
import { parseGTFSFareTransferRules } from './fareTransferRules.js';
import { parseGTFSFeedInfos } from './feedInfo.js';
import { parseGTFSFrequencies } from './frequencies.js';
import { parseGTFSLevels } from './levels.js';
import { parseGTFSLocationGroups } from './locationGroups.js';
import { parseGTFSLocationGroupStops } from './locationGroupStops.js';
import { parseGTFSNetworks } from './networks.js';
import { parseGTFSPathways } from './pathways.js';
import { parseGTFSRoutes } from './routes.js';
import { parseGTFSRouteNetworks } from './routeNetworks.js';
import { parseGTFSShapes } from './shapes.js';
import { parseGTFSStops } from './stops.js';
import { parseGTFSStopAreas } from './stopAreas.js';
import { parseGTFSStopTimes } from './stopTimes.js';
import { parseGTFSTimeframes } from './timeframes.js';
import { parseGTFSTransfers } from './transfers.js';
import { parseGTFSTranslations } from './translations.js';
import { parseGTFSTrips } from './trips.js';
export * from './agency.js';
export * from './areas.js';
export * from './attributions.js';
export * from './bookingRules.js';
export * from './calendar.js';
export * from './calendarDates.js';
export * from './fareAttributes.js';
export * from './fareLegJoinRules.js';
export * from './fareLegRules.js';
export * from './fareMedia.js';
export * from './fareProducts.js';
export * from './fareRules.js';
export * from './fareTransferRules.js';
export * from './feedInfo.js';
export * from './frequencies.js';
export * from './levels.js';
export * from './locationGroups.js';
export * from './locationGroupStops.js';
export * from './networks.js';
export * from './pathways.js';
export * from './routeNetworks.js';
export * from './routes.js';
export * from './shapes.js';
export * from './stopAreas.js';
export * from './stops.js';
export * from './stopTimes.js';
export * from './timeframes.js';
export * from './transfers.js';
export * from './translations.js';
export * from './trips.js';
/**
* # 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 'gis-tools-ts';
*
* const schedule = await buildGTFSSchedule(gzipData);
*
* for await (const feature of schedule) {
* console.log(feature);
* }
* ```
*
* ## Links
* - https://mobilitydatabase.org
* - https://developers.google.com/transit/gtfs/examples/overview
* - https://gtfs.org/documentation/schedule/reference/#tripstxt
* - https://mobilitydata.github.io/
* - https://www.transit.land
*/
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 {GTFSFeature} - 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: 'VectorFeature',
properties: stop.properties(),
geometry: { type: 'Point', is3D: false, coordinates: { x: lon, y: 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 iterZipFolder(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