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

Configurable stop filtering radius #30

Merged
merged 6 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion env.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ LAMBDA_EXEC_SG: Insert AWS Security Group ID Here (it must be in the same VPC as
LAMBDA_EXEC_SUBNET: Insert AWS Subnet ID Here (it must be in the same VPC as the security group)
BUGSNAG_NOTIFIER_KEY: INSERT BUGSNAG NOTIFIER KEY HERE
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>
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"@bugsnag/js": "^7.11.0",
"@bugsnag/plugin-aws-lambda": "^7.11.0",
"@conveyal/lonlat": "^1.4.1",
"@opentripplanner/geocoder": "^2.2.0",
"@opentripplanner/geocoder": "^2.2.1",
"geolib": "^3.3.1",
"node-fetch": "^2.6.1",
"serverless-api-gateway-caching": "^1.8.1",
Expand Down
1 change: 1 addition & 0 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ provider:
GEOCODERS: ${self:custom.secrets.GEOCODERS}
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}
package:
patterns:
- pois.json
Expand Down
22 changes: 16 additions & 6 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 All @@ -30,6 +30,8 @@
// Consts
const PREFERRED_LAYERS = ['venue', 'address', 'street', 'intersection']

const { COORDINATE_COMPARISON_PRECISION_DIGITS } = process.env

/**
* This method removes all characters Pelias doesn't support.
* Unfortunately, these characters not only don't match if they're found in the
Expand Down Expand Up @@ -97,17 +99,22 @@

/**
* Compares two GeoJSON positions and returns if they are equal within 10m accuracy
* @param a One GeoJSON Position object
* @param b One GeoJSON Position Object
* @param a One GeoJSON Position object
* @param b One GeoJSON Position Object
* @param precision How many digits after the decimal point to use when comparing
* @returns True if the positions describe the same place, false if they are different
*/
export const arePointsRoughlyEqual = (a: Position, b: Position): boolean => {
export const arePointsRoughlyEqual = (
a: Position,
b: Position,
precision = 4
): boolean => {
// 4 decimal places is approximately 10 meters, which is acceptable error
const aRounded = a?.map((point: number): number =>
parseFloat(point?.toFixed(4))
parseFloat(point?.toFixed(precision))
)
const bRounded = b?.map((point: number): number =>
parseFloat(point?.toFixed(4))
parseFloat(point?.toFixed(precision))
)

return (
Expand Down Expand Up @@ -174,7 +181,10 @@
// duplicate
return arePointsRoughlyEqual(
feature.geometry.coordinates,
otherFeature.geometry.coordinates
otherFeature.geometry.coordinates,
COORDINATE_COMPARISON_PRECISION_DIGITS
? parseInt(COORDINATE_COMPARISON_PRECISION_DIGITS)
: undefined
)
})
}
Expand Down Expand Up @@ -248,7 +258,7 @@
requestMethod: string,
args: AnyGeocoderQuery
): Promise<FeatureCollection> => {
const { focusPoint, text } = args

Check warning on line 261 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
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2521,10 +2521,10 @@
dependencies:
"@octokit/openapi-types" "^12.11.0"

"@opentripplanner/geocoder@^2.2.0":
version "2.2.0"
resolved "https://registry.yarnpkg.com/@opentripplanner/geocoder/-/geocoder-2.2.0.tgz#071f91664e898b06705c781fddd4f75dfcb3cc3f"
integrity sha512-V41WOCIpvwLRHEchBg3vRzGOOxoI+SdxczLGgLGPJ/Q/XiqaTqUgsK8WUv+hAUAGYNxI7jZbx7zdC2pXCH0m4w==
"@opentripplanner/geocoder@^2.2.1":
version "2.2.1"
resolved "https://registry.yarnpkg.com/@opentripplanner/geocoder/-/geocoder-2.2.1.tgz#c833c7a965291daf04e712adcf25a8d4d9c5c065"
integrity sha512-JkiydCToqivnz8gN57nx14MmaUoLco/K08xIrafYO6akvZHnwVZGi9enIigfxmAH+y1hu3uCviHgu3ATUmO/NQ==
dependencies:
"@conveyal/geocoder-arcgis-geojson" "^0.0.3"
"@conveyal/lonlat" "^1.4.1"
Expand Down
Loading