s2-tools
Version:
A collection of geospatial tools primarily designed for WGS84, Web Mercator, and S2.
100 lines • 3.52 kB
JavaScript
export * from './freeBikeStatus';
export * from './gbfs';
export * from './gbfsVersions';
export * from './stationInformation';
export * from './stationStatus';
export * from './systemAlerts';
export * from './systemCalendar';
export * from './systemHours';
export * from './systemInformation';
export * from './systemPricingPlans';
export * from './systemRegions';
/**
* GBFS Version 1 Reader
*/
export class GBFSReaderV1 {
version = 1;
freeBikeStatus;
gbfs;
gbfsVersions;
stationInformation;
stationStatus;
systemAlerts;
systemCalendar;
systemHours;
systemInformation;
systemPricingPlans;
systemRegions;
/**
* @param gbfs - the GBFS schema
* @param feeds - the feeds for the GBFS
*/
constructor(gbfs, feeds) {
this.gbfs = gbfs;
this.gbfsVersions = feeds?.gbfs_versions;
this.systemInformation = feeds?.system_information;
this.stationInformation = feeds?.station_information;
this.stationStatus = feeds?.station_status;
this.freeBikeStatus = feeds?.free_bike_status;
this.systemHours = feeds?.system_hours;
this.systemAlerts = feeds?.system_alerts;
this.systemCalendar = feeds?.system_calendar;
this.systemPricingPlans = feeds?.system_pricing_plans;
this.systemRegions = feeds?.system_regions;
}
/**
* Yields all of the shapes
* @yields an iterator that contains shapes, stops, location data, and routes
*/
async *[Symbol.asyncIterator]() {
const { stationInformation } = this;
if (stationInformation !== undefined) {
const { data: { stations }, } = stationInformation;
for (const station of stations) {
const { lat, lon, station_id, name, short_name, address, cross_street, region_id, post_code, rental_methods, capacity, } = station;
const stationProperties = {
station_id,
name,
short_name,
address,
cross_street,
region_id,
post_code,
rental_methods,
capacity,
};
const stationPoint = {
type: 'Feature',
properties: stationProperties,
geometry: {
type: 'Point',
coordinates: [lon, lat],
},
};
yield stationPoint;
}
}
}
}
/**
* @param gbfs - the GBFS schema to parse
* @param locale - the locale to use if provided, otherwise default to en
* @param path - if provided, will use this path instead of the url (for testing)
* @returns - the GBFS reader
*/
export async function buildGBFSReaderV1(gbfs, locale = 'en', path) {
const { data } = gbfs;
const firstLocale = Object.keys(data)[0];
const { feeds } = data[locale] ?? data[firstLocale];
const feedData = {};
await Promise.allSettled(feeds.map(async (feed) => {
if (feed.name === 'gbfs')
return;
const url = path !== undefined ? `${path}/${feed.name}.json` : feed.url;
const json = await fetch(url).then(async (res) => await res.json());
// @ts-expect-error - We really don't care, we know it categorizes correctly
feedData[feed.name] = json;
}));
return new GBFSReaderV1(gbfs, feedData);
}
//# sourceMappingURL=index.js.map