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

add CHECK_NAME_DUPLICATES feature #33

Merged
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 2 additions & 0 deletions __tests__/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,15 @@ describe('response merging', () => {
customResponse: CUSTOM_RESPONSE,
primaryResponse: GEOCODE_EARTH_RESPONSE
},
true,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we get a test that tests this with false?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried but couldn't do it in any way that made sense. This test tests for something completely different. We could add an entire new test case but this is a really weird edge case so I'm tempted to add a TODO here instead?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be fine with a TODO.

{ lat: 47.880281, lon: -122.238459 }
)
const mergedFocusedOnSteinerStreet = mergeResponses(
{
customResponse: CUSTOM_RESPONSE,
primaryResponse: GEOCODE_EARTH_RESPONSE
},
true,
{ lat: 37.793899, lon: -122.43634 }
)
expect(mergedFocusedOnBusStop).not.toEqual(mergedFocusedOnSteinerStreet)
Expand Down
1 change: 1 addition & 0 deletions env.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ GEOCODERS: <Stringified JSON Array of OTP-UI `GeocoderConfig`s>
BACKUP_GEOCODERS: <Stringified JSON Array of OTP-UI `GeocoderConfig`'s. Same length and order as 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
14 changes: 12 additions & 2 deletions handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -128,7 +133,12 @@ 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 false
miles-grant-ibigroup marked this conversation as resolved.
Show resolved Hide resolved
CHECK_NAME_DUPLICATES !== 'false'
// convertQSPToGeocoderArgs(event.queryStringParameters)?.focusPoint

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove commented code.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch thanks

)
},
// TODO: clean this reducer up. See https://github.com/ibi-group/pelias-stitch/pull/28#discussion_r1547582739
{ features: [], type: 'FeatureCollection' }
Expand Down
1 change: 1 addition & 0 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 12 additions & 5 deletions utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { URLSearchParams } from 'url'

import bugsnag from '@bugsnag/js'

Check warning on line 3 in utils.ts

View workflow job for this annotation

GitHub Actions / test-lambda-function

'bugsnag' is defined but never used
import { fromCoordinates } from '@conveyal/lonlat'
import { getDistance } from 'geolib'
import fetch from 'node-fetch'

Check warning on line 6 in utils.ts

View workflow job for this annotation

GitHub Actions / test-lambda-function

'fetch' is defined but never used
import type { LonLatOutput } from '@conveyal/lonlat'
import type { Feature, FeatureCollection, Position } from 'geojson'
import { AnyGeocoderQuery } from '@opentripplanner/geocoder/lib/geocoders/types'
Expand Down Expand Up @@ -131,15 +131,17 @@
*/
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
)
Expand Down Expand Up @@ -199,14 +201,19 @@
customResponse: FeatureCollection
primaryResponse: FeatureCollection
},
checkNameDuplicates = true,
focusPoint?: LonLatOutput
): FeatureCollection => {
// Openstreetmap can sometimes include bus stop info with less
// correct information than the GTFS feed.
// 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
Expand Down Expand Up @@ -258,7 +265,7 @@
requestMethod: string,
args: AnyGeocoderQuery
): Promise<FeatureCollection> => {
const { focusPoint, text } = args

Check warning on line 268 in utils.ts

View workflow job for this annotation

GitHub Actions / test-lambda-function

'focusPoint' is assigned a value but never used
if (!text) return { features: [], type: 'FeatureCollection' }
const onlineResponse = await geocoder[requestMethod](args)

Expand Down
Loading