Skip to content

Commit

Permalink
improved GBFS
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr Martian committed Dec 30, 2024
1 parent 35ed7ec commit 34a795f
Show file tree
Hide file tree
Showing 15 changed files with 529 additions and 363 deletions.
Binary file modified bun.lockb
Binary file not shown.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@
},
"dependencies": {
"earclip": "^1.1.1",
"open-vector-tile": "^1.6.0",
"open-vector-tile": "^1.6.1",
"s2-tilejson": "^1.7.0",
"s2json-spec": "^1.6.0",
"s2json-spec": "^1.6.2",
"sharp": "^0.33.5"
}
}
2 changes: 1 addition & 1 deletion src/readers/gbfs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export type GBFSTypess = GBFSV1 | GBFSV2 | GBFSV3;
* - 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
* @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: string, locale = 'en'): Promise<GBFSReader> {
Expand Down
103 changes: 100 additions & 3 deletions src/readers/gbfs/schemaV1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
GBFSVersionsV1,
} from '.';

import type { Feature, FeatureIterator, PointGeometry, Properties } from '../../..';

export * from './freeBikeStatus';
export * from './gbfs';
export * from './gbfsVersions';
Expand All @@ -24,13 +26,60 @@ export * from './systemInformation';
export * from './systemPricingPlans';
export * from './systemRegions';

/** Station Information feature properties */
export interface GBFSStationV1FeaturesV1Properties extends Properties {
station_id: string;
name: string;
short_name?: string;
address?: string;
cross_street?: string;
region_id?: string;
post_code?: string;
rental_methods?: Array<
| 'key'
| 'creditcard'
| 'paypass'
| 'applepay'
| 'androidpay'
| 'transitcard'
| 'accountnumber'
| 'phone'
| 'KEY'
| 'CREDITCARD'
| 'PAYPASS'
| 'APPLEPAY'
| 'ANDROIDPAY'
| 'TRANSITCARD'
| 'ACCOUNTNUMBER'
| 'PHONE'
>;
capacity?: number;
}

/** Station Information Point Feature */
export type GBFSStationPointFeatureV1 = Feature<
undefined,
Properties,
GBFSStationV1FeaturesV1Properties,
PointGeometry<Properties>
>;

/** All potential feature types in a GBFS V1 specification */
export type GBFSFeaturesV1 = GBFSStationPointFeatureV1;

/** All potential feature property types in a GBFS V1 specification */
export type GBFSFeaturePropertiesV1 = GBFSStationV1FeaturesV1Properties;

/**
* GBFS Version 1 Reader
*/
export class GBFSReaderV1 {
export class GBFSReaderV1
implements FeatureIterator<undefined, Properties, GBFSFeaturePropertiesV1>
{
version = 1;
freeBikeStatus?: GBFSFreeBikeStatusV1;
gbfs: GBFSV1;
versions?: GBFSVersionsV1;
gbfsVersions?: GBFSVersionsV1;
stationInformation?: GBFSStationInformationV1;
stationStatus?: GBFSStationStatusV1;
systemAlerts?: GBFSSystemAlertsV1;
Expand All @@ -46,7 +95,7 @@ export class GBFSReaderV1 {
*/
constructor(gbfs: GBFSV1, feeds?: FeedResV1) {
this.gbfs = gbfs;
this.versions = feeds?.gbfs_versions;
this.gbfsVersions = feeds?.gbfs_versions;
this.systemInformation = feeds?.system_information as GBFSSystemInformationV1;
this.stationInformation = feeds?.station_information;
this.stationStatus = feeds?.station_status;
Expand All @@ -57,6 +106,54 @@ export class GBFSReaderV1 {
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](): AsyncGenerator<GBFSFeaturesV1> {
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: GBFSStationV1FeaturesV1Properties = {
station_id,
name,
short_name,
address,
cross_street,
region_id,
post_code,
rental_methods,
capacity,
};
const stationPoint: GBFSStationPointFeatureV1 = {
type: 'Feature',
properties: stationProperties,
geometry: {
type: 'Point',
coordinates: [lon, lat],
},
};
yield stationPoint;
}
}
}
}

/** Parsing all the available feeds */
Expand Down
Loading

0 comments on commit 34a795f

Please sign in to comment.