s2-tools
Version:
A collection of geospatial tools primarily designed for WGS84, Web Mercator, and S2.
126 lines • 4.95 kB
JavaScript
export * from './freeBikeStatus';
export * from './gbfs';
export * from './gbfsVersions';
export * from './geofencingZones';
export * from './stationInformation';
export * from './stationStatus';
export * from './systemAlerts';
export * from './systemCalendar';
export * from './systemHours';
export * from './systemInformation';
export * from './systemPricingPlans';
export * from './systemRegions';
export * from './vehicleTypes';
/**
* GBFS Version 1 Reader
*/
export class GBFSReaderV2 {
version = 2;
freeBikeStatus;
gbfs;
gbfsVersions;
geofencingZones;
stationInformation;
stationStatus;
systemAlerts;
systemCalendar;
systemHours;
systemInformation;
systemPricingPlans;
systemRegions;
vehicleTypes;
/**
* @param gbfs - the GBFS schema
* @param feeds - the feeds for the GBFS
*/
constructor(gbfs, feeds) {
this.gbfs = gbfs;
this.gbfsVersions = feeds?.gbfs_versions;
this.geofencingZones = feeds?.geofencing_zones;
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;
this.vehicleTypes = feeds?.vehicle_types;
}
/**
* Yields all of the shapes
* @yields an iterator that contains shapes, stops, location data, and routes
*/
async *[Symbol.asyncIterator]() {
const { geofencingZones, stationInformation } = this;
if (geofencingZones !== undefined) {
const { data: { geofencing_zones }, } = geofencingZones;
for (const feature of geofencing_zones.features)
yield feature;
}
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, rental_uris, } = station;
const stationProperties = {
station_id,
name,
short_name,
address,
cross_street,
region_id,
post_code,
rental_methods,
is_virtual_station: 'is_virtual_station' in station && station.is_virtual_station,
parking_type: 'parking_type' in station ? station.parking_type : undefined,
parking_hoop: 'parking_hoop' in station && station.parking_hoop,
contact_phone: 'contact_phone' in station ? station.contact_phone : undefined,
capacity,
is_valet_station: 'is_valet_station' in station && station.is_valet_station,
is_charging_station: 'is_charging_station' in station && station.is_charging_station,
rental_uris,
};
const stationPoint = {
type: 'Feature',
properties: stationProperties,
geometry: {
type: 'Point',
coordinates: [lon, lat],
},
};
yield stationPoint;
if ('station_area' in station && station.station_area !== undefined) {
const stationArea = {
type: 'Feature',
properties: stationProperties,
geometry: station.station_area,
};
yield stationArea;
}
}
}
}
}
/**
* @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 buildGBFSReaderV2(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 categorize naturally
feedData[feed.name] = json;
}));
return new GBFSReaderV2(gbfs, feedData);
}
//# sourceMappingURL=index.js.map