Skip to content

Commit

Permalink
Merge pull request #22 from ibi-group/move-to-otp-geocoder
Browse files Browse the repository at this point in the history
Support OTP Geocoder
  • Loading branch information
miles-grant-ibigroup authored Mar 17, 2023
2 parents 664855d + de001db commit af3303c
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 23 deletions.
2 changes: 2 additions & 0 deletions env.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ GEOCODE_EARTH_URL: https://api.geocode.earth/v1 # Not needed if geocode.earth is
CSV_ENABLED: true
CUSTOM_PELIAS_URL: http://<insert your Pelias endpoint here>/v1
GEOCODER: HERE # Options: HERE, PELIAS
TRANSIT_GEOCODER: (optional) OTP/PELIAS
TRANSIT_BASE_URL: (conditionally required) OTP instance when TRANSIT_GEOCODER=OTP

SECONDARY_GEOCODER: (optional) HERE/PELIAS
SECONDARY_GEOCODER_API_KEY: (optional) INSERT SECONDARY API KEY HERE
Expand Down
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
} = 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

0 comments on commit af3303c

Please sign in to comment.