s2-tools
Version:
A collection of geospatial tools primarily designed for WGS84, Web Mercator, and S2.
155 lines • 5.95 kB
JavaScript
export * from './gbfs';
export * from './gbfsVersions';
export * from './geofencingZones';
export * from './manifest';
export * from './stationInformation';
export * from './stationStatus';
export * from './systemAlerts';
export * from './systemInformation';
export * from './systemPricingPlans';
export * from './systemRegions';
export * from './vehicleStatus';
export * from './vehicleTypes';
/**
* GBFS Version 3 Reader
*/
export class GBFSReaderV3 {
version = 3;
gbfs;
gbfsVersions;
systemInformation;
stationInformation;
stationStatus;
vehicleStatus;
systemAlerts;
systemRegions;
systemPricingPlans;
geofencingZones;
manifest;
/**
* @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.vehicleStatus = feeds?.vehicle_status;
this.systemAlerts = feeds?.system_alerts;
this.systemRegions = feeds?.system_regions;
this.systemPricingPlans = feeds?.system_pricing_plans;
this.geofencingZones = feeds?.geofencing_zones;
this.manifest = feeds?.manifest;
}
/**
* Yields all of the shapes
* @yields an iterator that contains shapes, stops, location data, and routes
*/
async *[Symbol.asyncIterator]() {
const { geofencingZones, stationInformation, vehicleStatus, manifest } = 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_area, station_id, name, short_name, address, cross_street, region_id, post_code, station_opening_hours, rental_methods, is_virtual_station, parking_type, parking_hoop, contact_phone, capacity, is_valet_station, is_charging_station, rental_uris, } = station;
const stationProperties = {
station_id,
name,
short_name,
address,
cross_street,
region_id,
post_code,
station_opening_hours,
rental_methods,
is_virtual_station,
parking_type,
parking_hoop,
contact_phone,
capacity,
is_valet_station,
is_charging_station,
rental_uris,
};
const stationPoint = {
type: 'Feature',
properties: stationProperties,
geometry: {
type: 'Point',
coordinates: [lon, lat],
},
};
yield stationPoint;
if (station_area !== undefined) {
const stationArea = {
type: 'Feature',
properties: stationProperties,
geometry: station_area,
};
yield stationArea;
}
}
}
if (vehicleStatus !== undefined) {
const { data: { vehicles }, } = vehicleStatus;
for (const vehicle of vehicles) {
const { lat, lon } = vehicle;
if (lat === undefined || lon === undefined)
continue;
const vehiclePoint = {
type: 'Feature',
properties: { ...vehicle },
geometry: {
type: 'Point',
coordinates: [lon, lat],
},
};
yield vehiclePoint;
}
}
if (manifest !== undefined) {
const datasets = manifest.data.datasets;
for (const dataset of datasets) {
if ('area' in dataset && dataset.area !== undefined) {
const { system_id, versions, area, country_code } = dataset;
const areaFeature = {
type: 'Feature',
properties: { system_id, versions, country_code },
geometry: area,
};
yield areaFeature;
}
}
}
}
}
/**
* @param gbfs - the GBFS schema to parse
* @param path - if provided, will use this path instead of the url (for testing)
* @returns - the GBFS reader
*/
export async function buildGBFSReaderV3(gbfs, path) {
const { data: { feeds }, } = gbfs;
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;
}));
// If there is a manifest.json, lets get it
if (feedData.system_information?.data.manifest_url !== undefined) {
const manifestURL = path !== undefined ? `${path}/manifest.json` : feedData.system_information.data.manifest_url;
feedData.manifest = await fetch(manifestURL).then(async (res) => (await res.json()));
}
return new GBFSReaderV3(gbfs, feedData);
}
//# sourceMappingURL=index.js.map