s2-tools
Version:
A collection of geospatial tools primarily designed for WGS84, Web Mercator, and S2.
64 lines • 2.6 kB
JavaScript
import { parseCSVAsRecord } from '..';
import { buildGBFSReaderV1 } from './schemaV1';
import { buildGBFSReaderV2 } from './schemaV2';
import { buildGBFSReaderV3 } from './schemaV3';
export * from './schemaV1';
export * from './schemaV2';
export * from './schemaV3';
// TODO: All features should be parsed as VectorGeometry
/**
* Given a link to a GBFS feed, build the appropriate reader for the feed.
* Examples:
* - v3: https://backend.citiz.fr/public/provider/9/gbfs/v3.0/gbfs.json
* - v2: https://gbfs.helbiz.com/v2.2/durham/gbfs.json
* - v1: https://gbfs.urbansharing.com/gbfs/gbfs.json
* @param url - The link to the GBFS feed
* @param locale - The locale to use if provided, otherwise default to "en" (e.g., "en", "en-US").
* @returns - a GBFSReader of the appropriate version
*/
export async function buildGBFSReader(url, locale = 'en') {
const data = await fetch(url).then(async (res) => (await res.json()));
const path = url.includes('localhost') ? url.split('/').slice(0, -1).join('/') : undefined;
const versionMajor = 'version' in data ? data.version[0] : '1';
if (versionMajor === '1') {
return await buildGBFSReaderV1(data, locale, path);
}
else if (versionMajor === '2') {
return await buildGBFSReaderV2(data, locale, path);
}
else if (versionMajor === '3') {
return await buildGBFSReaderV3(data, path);
}
else
throw Error('Unsupported GBFS version');
}
/**
* Fetches the systems from the github CSV file
* @param url - The URL of the CSV file. The default is the one used by GBFS. This variable exists for testing
* @returns - an array of systems
*/
export async function fetchGTFSSystems(url = 'https://raw.githubusercontent.com/MobilityData/gbfs/refs/heads/master/systems.csv') {
const res = [];
const data = await fetch(url).then(async (res) => await res.text());
const parsed = parseCSVAsRecord(data);
for (const system of parsed) {
const { Name: name, Location: location, URL: url } = system;
const countryCode = system['Country Code'];
const systemId = system['System ID'];
const autoDiscoveryUrl = system['Auto-Discovery URL'];
const supportedVersions = (system['Supported Versions'] ?? '').split(' ; ');
const authInfo = system['Authentication Info'];
res.push({
name,
location,
url,
countryCode,
systemId,
autoDiscoveryUrl,
supportedVersions,
authInfo,
});
}
return res;
}
//# sourceMappingURL=index.js.map