Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support OTP Geocoder #22

Merged
merged 7 commits into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 37 additions & 13 deletions handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@ const BugsnagPluginAwsLambda = require('@bugsnag/plugin-aws-lambda')
const {
BUGSNAG_NOTIFIER_KEY,
CSV_ENABLED,
CUSTOM_PELIAS_URL,
GEOCODE_EARTH_URL,
GEOCODER,
GEOCODER_API_KEY,
REDIS_HOST,
REDIS_KEY,
SECONDARY_GEOCODE_EARTH_URL,
SECONDARY_GEOCODER,
SECONDARY_GEOCODER_API_KEY
SECONDARY_GEOCODER_API_KEY,
TRANSIT_BASE_URL,
TRANSIT_GEOCODER
miles-grant-ibigroup marked this conversation as resolved.
Show resolved Hide resolved
} = process.env

const redis = REDIS_HOST
Expand All @@ -50,13 +51,19 @@ if (redis) redis.on('error', (err) => console.log('Redis Client Error', err))

// Ensure env variables have been set
if (
typeof CUSTOM_PELIAS_URL !== 'string' ||
typeof TRANSIT_BASE_URL !== 'string' ||
typeof GEOCODER_API_KEY !== 'string' ||
typeof BUGSNAG_NOTIFIER_KEY !== 'string' ||
typeof GEOCODER !== 'string'
) {
throw new Error(
'Error: configuration variables not found! Ensure env.yml has been decrypted'
'Error: required configuration variables not found! Ensure env.yml has been decrypted.'
)
}

if (CSV_ENABLED === 'true' && TRANSIT_GEOCODER === 'OTP') {
throw new Error(
'Error: Invalid configuration. OTP Geocoder does not support CSV_ENABLED.'
)
}

Expand Down Expand Up @@ -84,6 +91,17 @@ const getPrimaryGeocoder = () => {
})
}

const getTransitGeocoder = () => {
if (TRANSIT_GEOCODER === 'OTP') {
return getGeocoder({
baseUrl: TRANSIT_BASE_URL,
type: TRANSIT_GEOCODER
})
}

return null
}

const getSecondaryGeocoder = () => {
if (!SECONDARY_GEOCODER || !SECONDARY_GEOCODER_API_KEY) {
console.warn('Not using secondary Geocoder')
Expand Down Expand Up @@ -143,15 +161,21 @@ export const makeGeocoderRequests = async (
// @ts-expect-error Redis Typescript types are not friendly
redis
),
// Should the custom Pelias instance need to be replaced with something different
// this is where it should be replaced
fetchPelias(
CUSTOM_PELIAS_URL,
apiMethod,
`${new URLSearchParams(peliasQSP).toString()}&sources=transit${
CSV_ENABLED && CSV_ENABLED === 'true' ? ',pelias' : ''
}`
)
// Custom request is either through geocoder package or "old" pelias method
getTransitGeocoder()
? cachedGeocoderRequest(
getTransitGeocoder(),
'autocomplete',
convertQSPToGeocoderArgs(event.queryStringParameters),
null
)
: fetchPelias(
TRANSIT_BASE_URL,
apiMethod,
`${new URLSearchParams(peliasQSP).toString()}&sources=transit${
CSV_ENABLED && CSV_ENABLED === 'true' ? ',pelias' : ''
}`
)
])

// If the primary response doesn't contain responses or the responses are not satisfactory,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@bugsnag/js": "^7.11.0",
"@bugsnag/plugin-aws-lambda": "^7.11.0",
"@conveyal/lonlat": "^1.4.1",
"@opentripplanner/geocoder": "^1.3.2",
"@opentripplanner/geocoder": "^1.4.0",
"geolib": "^3.3.1",
"node-fetch": "^2.6.1",
"redis": "^4.1.0",
Expand Down
4 changes: 2 additions & 2 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ provider:
- ${self:custom.secrets.LAMBDA_EXEC_SUBNET}
environment:
GEOCODER: ${self:custom.secrets.GEOCODER}
TRANSIT_GEOCODER: ${self:custom.secrets.TRANSIT_GEOCODER, null}
TRANSIT_BASE_URL: ${self:custom.secrets.TRANSIT_BASE_URL}
# Pelias instance of Geocode.Earth, with street and landmarks
GEOCODE_EARTH_URL: ${self:custom.secrets.GEOCODE_EARTH_URL, null}
GEOCODER_API_KEY: ${self:custom.secrets.GEOCODER_API_KEY, null}
# Pelias instance that is self-hosted, with GTFS stops and CSV POIs
CUSTOM_PELIAS_URL: ${self:custom.secrets.CUSTOM_PELIAS_URL, null}
# Used to logging to Bugsnag
BUGSNAG_NOTIFIER_KEY: ${self:custom.secrets.BUGSNAG_NOTIFIER_KEY}
REDIS_HOST: ${self:custom.secrets.REDIS_HOST, null}
Expand Down
7 changes: 4 additions & 3 deletions utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,11 @@ export const convertQSPToGeocoderArgs = (
* @returns Pelias response decoded from JSON
*/
export const fetchPelias = async (
baseUrl: string,
service: string,
query: string
baseUrl?: string,
service?: string,
query?: string
): Promise<FeatureCollection> => {
if (!baseUrl) return { features: [], type: 'FeatureCollection' }
try {
const response = await fetch(`${baseUrl}/${service}?${query}`, {})
return await response.json()
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1852,10 +1852,10 @@
dependencies:
"@octokit/openapi-types" "^9.4.0"

"@opentripplanner/geocoder@^1.3.2":
version "1.3.2"
resolved "https://registry.yarnpkg.com/@opentripplanner/geocoder/-/geocoder-1.3.2.tgz#cd67132cb00d6089457d33eb1d4526ef47c0914a"
integrity sha512-gTkJlAy0lURZxjG/+bm1qx+M2oLdjSuQEOjLaWhHsA/K7kQWRGhIFhe2vKJF1qZJE+sVe2WCHJDmGKO3I/Nvsw==
"@opentripplanner/geocoder@^1.4.0":
version "1.4.0"
resolved "https://registry.yarnpkg.com/@opentripplanner/geocoder/-/geocoder-1.4.0.tgz#728afec6585cf3748a1931493b3a3566421f445d"
integrity sha512-FECrLmzYvQmgQDE1Ad19pPdo/HRvz7b0POyD9lXNg/uKTyVXhXxHdYd6sWot6ZiGdiqZTUamNKG3RaWaJvdqoA==
dependencies:
"@conveyal/geocoder-arcgis-geojson" "^0.0.3"
"@conveyal/lonlat" "^1.4.1"
Expand Down