diff --git a/packages/geocoder/src/apis/otp/index.ts b/packages/geocoder/src/apis/otp/index.ts index 3403566b9..58f48b92c 100644 --- a/packages/geocoder/src/apis/otp/index.ts +++ b/packages/geocoder/src/apis/otp/index.ts @@ -16,6 +16,8 @@ type OTPGeocoderResponse = { code?: string | undefined, name: string, id: string, + agencies?: { id: string, name: string }[] + feedPublisher?: { name: string } modes: string[] }[] } | undefined diff --git a/packages/geocoder/src/geocoders/otp.ts b/packages/geocoder/src/geocoders/otp.ts index 5e1a7f72a..90ac23d05 100644 --- a/packages/geocoder/src/geocoders/otp.ts +++ b/packages/geocoder/src/geocoders/otp.ts @@ -5,6 +5,24 @@ import type { AutocompleteQuery, MultiGeocoderResponse } from "./types"; import Geocoder from "./abstract-geocoder"; import { OTPGeocoderResponse } from "../apis/otp"; + +const generateLabel = stop => { + let brackets = "" + if (stop?.agencies?.[0]?.name) { + brackets += stop.agencies[0].name + } else if (stop?.feedPublisher?.name) { + brackets += stop.feedPublisher.name + } + if (stop?.code) { + if (brackets !== "") brackets += " " + brackets += stop.code + } + + if (brackets === "") return stop.name + + return `${stop.name} (${brackets})` +} + /** * Allows fetching results from OTP instance with the geocoder endpoint enabled */ @@ -21,17 +39,21 @@ export default class OTPGeocoder extends Geocoder { rewriteAutocompleteResponse(response: OTPGeocoderResponse): MultiGeocoderResponse { - const generateLabel = stop => stop.code ? `${stop.name} (${stop.code})` : stop.name - return { - features: response?.results?.map(stop => ({ - geometry: { type: "Point", coordinates: [stop.coordinate.lon, stop.coordinate.lat] }, - id: stop.id, - // TODO: if non-stops are supported, these need to be detected here and - // this layer property updated accordingly - properties: { layer: "stops", source: "otp", modes: stop.modes, name: stop.name, label: generateLabel(stop) }, - type: "Feature" - })), + features: response?.results?.map(stop => ({ + geometry: { type: "Point", coordinates: [stop.coordinate.lon, stop.coordinate.lat] }, + id: stop.id, + // TODO: if non-stops are supported, these need to be detected here and + // this layer property updated accordingly + properties: { + layer: "stops", + source: "otp", + modes: stop.modes, + name: stop.name, + label: generateLabel(stop) + }, + type: "Feature" + })), type: "FeatureCollection" }; }