diff --git a/__tests__/util.test.ts b/__tests__/util.test.ts index 9ccb68d..fff0d25 100644 --- a/__tests__/util.test.ts +++ b/__tests__/util.test.ts @@ -112,6 +112,7 @@ describe('response merging', () => { customResponse: CUSTOM_RESPONSE, primaryResponse: GEOCODE_EARTH_RESPONSE }, + true, { lat: 47.880281, lon: -122.238459 } ) const mergedFocusedOnSteinerStreet = mergeResponses( @@ -119,6 +120,7 @@ describe('response merging', () => { customResponse: CUSTOM_RESPONSE, primaryResponse: GEOCODE_EARTH_RESPONSE }, + true, { lat: 37.793899, lon: -122.43634 } ) expect(mergedFocusedOnBusStop).not.toEqual(mergedFocusedOnSteinerStreet) diff --git a/env.example.yml b/env.example.yml index 39e5b48..064e0c8 100644 --- a/env.example.yml +++ b/env.example.yml @@ -5,3 +5,4 @@ GEOCODERS: BACKUP_GEOCODERS: COORDINATE_COMPARISON_PRECISION_DIGITS: defaults to 4 (~10m). What precision to use when comparing if two locations are the same +CHECK_NAME_DUPLICATES: defaults to true. If disabled, name-based duplicate checking will be disabled. Useful if your GTFS has common words in its stop names \ No newline at end of file diff --git a/handler.ts b/handler.ts index 100d1b3..02571f7 100644 --- a/handler.ts +++ b/handler.ts @@ -24,7 +24,12 @@ import { // This plugin must be imported via cjs to ensure its existence (typescript recommendation) const BugsnagPluginAwsLambda = require('@bugsnag/plugin-aws-lambda') -const { BACKUP_GEOCODERS, BUGSNAG_NOTIFIER_KEY, GEOCODERS } = process.env +const { + BACKUP_GEOCODERS, + BUGSNAG_NOTIFIER_KEY, + CHECK_NAME_DUPLICATES, + GEOCODERS +} = process.env const POIS = require('./pois.json') if (!GEOCODERS) { @@ -128,7 +133,13 @@ export const makeGeocoderRequests = async ( >( (prev, cur, idx) => { if (idx === 0) return cur - return mergeResponses({ customResponse: cur, primaryResponse: prev }) + return mergeResponses( + { customResponse: cur, primaryResponse: prev }, + // Default to true + CHECK_NAME_DUPLICATES !== 'false' + // TODO: use focus point here to pre-sort results? It's possible to grab + // the focus point by calling convertQSPToGeocoderArgs on event.queryStringParameters + ) }, // TODO: clean this reducer up. See https://github.com/ibi-group/pelias-stitch/pull/28#discussion_r1547582739 { features: [], type: 'FeatureCollection' } diff --git a/serverless.yml b/serverless.yml index 08350b4..da11c26 100644 --- a/serverless.yml +++ b/serverless.yml @@ -13,6 +13,7 @@ provider: BACKUP_GEOCODERS: ${self:custom.secrets.BACKUP_GEOCODERS} BUGSNAG_NOTIFIER_KEY: ${self:custom.secrets.BUGSNAG_NOTIFIER_KEY} COORDINATE_COMPARISON_PRECISION_DIGITS: ${self:custom.secrets.COORDINATE_COMPARISON_PRECISION_DIGITS, 4} + CHECK_NAME_DUPLICATES: ${self:custom.secrets.CHECK_NAME_DUPLICATES, true} package: patterns: - pois.json diff --git a/utils.ts b/utils.ts index d0692bf..f6db7d3 100644 --- a/utils.ts +++ b/utils.ts @@ -131,15 +131,17 @@ export const arePointsRoughlyEqual = ( */ const filterOutDuplicateStops = ( feature: Feature, - customFeatures: Feature[] + customFeatures: Feature[], + checkNameDuplicates: boolean ): boolean => { // If the names are the same, or if the feature is too far away, we can't consider the feature if ( customFeatures.find( (otherFeature: Feature) => - (feature?.properties?.name || '') - .toLowerCase() - .includes((otherFeature?.properties?.name || '').toLowerCase()) || + (checkNameDuplicates && + (feature?.properties?.name || '') + .toLowerCase() + .includes((otherFeature?.properties?.name || '').toLowerCase())) || // Any feature this far away is likely not worth being considered feature?.properties?.distance > 7500 ) @@ -199,6 +201,7 @@ export const mergeResponses = ( customResponse: FeatureCollection primaryResponse: FeatureCollection }, + checkNameDuplicates = true, focusPoint?: LonLatOutput ): FeatureCollection => { // Openstreetmap can sometimes include bus stop info with less @@ -206,7 +209,11 @@ export const mergeResponses = ( // Remove anything from the geocode.earth response that's within 10 meters of a custom result responses.primaryResponse.features = responses?.primaryResponse?.features?.filter((feature: Feature) => - filterOutDuplicateStops(feature, responses.customResponse.features) + filterOutDuplicateStops( + feature, + responses.customResponse.features, + checkNameDuplicates + ) ) || [] // If a focus point is specified, sort custom features by distance to the focus point