UNPKG

gis-tools-ts

Version:

A collection of geospatial tools primarily designed for WGS84, Web Mercator, and S2.

98 lines 3.62 kB
import { parseCSVAsRecord } from '../index.js'; import { GBFSReaderV1, buildGBFSReaderV1 } from './schemaV1/index.js'; import { GBFSReaderV2, buildGBFSReaderV2 } from './schemaV2/index.js'; import { GBFSReaderV3, buildGBFSReaderV3 } from './schemaV3/index.js'; export * from './schemaV1/index.js'; export * from './schemaV2/index.js'; export * from './schemaV3/index.js'; /** * # General Bikeshare Feed Specification (GBFS) Reader * * ## Description * Given a link to a GBFS feed, build the appropriate reader for the feed. * The versions of GBFS reader classes this data could be (1, 2, or 3). * Implements the {@link FeatureIterator} interface. * * ## Usage * * ```ts * import { buildGBFSReader } from 'gis-tools-ts'; * * const reader = await buildGBFSReader('https://gbfs.urbansharing.com/gbfs/gbfs.json'); * // read the features * for await (const feature of reader) { * // do something with the feature * } * ``` * * ## Links * - https://github.com/MobilityData/gbfs * - https://github.com/MobilityData/gbfs-json-schema/tree/master/v3.0 * - v3 example data: https://backend.citiz.fr/public/provider/9/gbfs/v3.0/gbfs.json * - v2 example data: https://gbfs.helbiz.com/v2.2/durham/gbfs.json * - v1 example data: 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'); } /** * # General Bikeshare Feed Specification (GBFS) Reader * * ## Description * Fetches the list of GBFS systems from the github CSV file * * ## Usage * * ```ts * import { fetchGTFSSystems } from 'gis-tools-ts'; * * const systems = await fetchGTFSSystems(); * console.log(systems); * ``` * * ## Links * - https://github.com/MobilityData/gbfs/blob/master/systems.csv * @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