Skip to content

Commit

Permalink
feat: TomTom geocoder
Browse files Browse the repository at this point in the history
feat: toFixed limit confidence number to 4 digits
test: update api calls
  • Loading branch information
spurreiter committed Feb 12, 2022
1 parent 616dc67 commit 0c4bd71
Show file tree
Hide file tree
Showing 31 changed files with 877 additions and 41 deletions.
35 changes: 35 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"args": [
"-u",
"tdd",
"--timeout",
"999999",
"--colors",
"${workspaceFolder}/test"
],
"internalConsoleOptions": "openOnSessionStart",
"name": "Mocha Tests",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"request": "launch",
"skipFiles": [
"<node_internals>/**"
],
"type": "pwa-node"
},
{
"type": "pwa-node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/lib/index.cjs"
}
]
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Features:
| [PeliasGeocoder](https://github.com/pelias/documentation/blob/master/README.md) |||| Local or [Geocode.earth](https://geocode.earth/docs) |
| [PickpointGeocoder](https://pickpoint.io/api-reference) |||| Search Results based on OSM |
| [TeleportGeocoder](https://developers.teleport.org/api/resources/) |||| Searches only by city names, no addresses |
| [TomTomGeocoder](https://developer.tomtom.com/) |||| |
| [YandexGeocoder](https://yandex.com/dev/maps/geocoder/) |||| |

## usage
Expand Down
18 changes: 18 additions & 0 deletions examples/tomtom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import dotenv from 'dotenv'
import { argv } from './argv.js'
import { fetchAdapter, TomTomGeocoder } from '../src/index.js'

dotenv.config()

const { TOMTOM_APIKEY: apiKey, FORWARD, REVERSE, LANGUAGE } = process.env
const { forward, reverse, ...other } = argv({ forward: FORWARD, reverse: REVERSE })

const adapter = fetchAdapter()
const geocoder = new TomTomGeocoder(adapter, { apiKey, language: LANGUAGE, ...other })

const promise = reverse
? geocoder.reverse(reverse)
: geocoder.forward(forward)
promise
.then(res => console.dir(res, { depth: null }))
.catch(console.error)
4 changes: 2 additions & 2 deletions src/geocoder/arcgis.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AbstractGeocoder } from './abstract.js'
import { HttpError, countryCode, countryName } from '../utils/index.js'
import { HttpError, countryCode, countryName, toFixed } from '../utils/index.js'

/** @typedef {import('../adapter').fetchAdapterFn} fetchAdapterFn */

Expand Down Expand Up @@ -179,7 +179,7 @@ export class ArcGisGeocoder extends AbstractGeocoder {
const { xmin, ymin, xmax, ymax } = extent

const extra = {
confidence: score / 100,
confidence: toFixed(score / 100),
type: undef(Type),
placeName: undef(PlaceName),
addrType: undef(Addr_type),
Expand Down
4 changes: 2 additions & 2 deletions src/geocoder/here.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AbstractGeocoder } from './abstract.js'
import { HttpError, countryCode } from '../utils/index.js'
import { HttpError, countryCode, toFixed } from '../utils/index.js'

/** @typedef {import('../adapter').fetchAdapterFn} fetchAdapterFn */

Expand Down Expand Up @@ -130,7 +130,7 @@ export class HereGeocoder extends AbstractGeocoder {
building: address.building,
extra: {
id: id,
confidence: scoring.queryScore || 0
confidence: toFixed(scoring.queryScore || 0)
}
}

Expand Down
1 change: 1 addition & 0 deletions src/geocoder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ export * from './osm.js'
export * from './pelias.js'
export * from './pickpoint.js'
export * from './teleport.js'
export * from './tomtom.js'
export * from './yandex.js'
4 changes: 2 additions & 2 deletions src/geocoder/locationiq.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AbstractGeocoder } from './abstract.js'
import { HttpError, toUpperCase } from '../utils/index.js'
import { HttpError, toFixed, toUpperCase } from '../utils/index.js'

/** @typedef {import('../adapter').fetchAdapterFn} fetchAdapterFn */

Expand Down Expand Up @@ -147,7 +147,7 @@ export class LocationIqGeocoder extends AbstractGeocoder {
streetNumber: address.house_number,
extra: {
id,
confidence: importance || 0,
confidence: toFixed(importance || 0),
type,
addrType,
bbox: toBbox(boundingbox)
Expand Down
4 changes: 2 additions & 2 deletions src/geocoder/opendatafrance.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AbstractGeocoder } from './abstract.js'
import { HttpError } from '../utils/index.js'
import { HttpError, toFixed } from '../utils/index.js'

/** @typedef {import('../adapter').fetchAdapterFn} fetchAdapterFn */

Expand Down Expand Up @@ -119,7 +119,7 @@ export class OpendataFranceGeocoder extends AbstractGeocoder {
citycode: properties.citycode,
extra: {
id: properties.id,
confidence: properties.score || 0
confidence: toFixed(properties.score || 0)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/geocoder/osm.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AbstractGeocoder } from './abstract.js'
import { HttpError, toUpperCase } from '../utils/index.js'
import { HttpError, toFixed, toUpperCase } from '../utils/index.js'

/** @typedef {import('../adapter').fetchAdapterFn} fetchAdapterFn */

Expand Down Expand Up @@ -161,7 +161,7 @@ export class OsmGeocoder extends AbstractGeocoder {
neighbourhood: address.neighbourhood,
extra: {
id: result.osm_id,
confidence: result.importance || 0,
confidence: toFixed(result.importance || 0),
bbox: toBbox(result.boundingbox)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/geocoder/pelias.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AbstractGeocoder } from './abstract.js'
import { HttpError, countryCode } from '../utils/index.js'
import { HttpError, countryCode, toFixed } from '../utils/index.js'

/** @typedef {import('../adapter').fetchAdapterFn} fetchAdapterFn */

Expand Down Expand Up @@ -104,7 +104,7 @@ export class PeliasGeocoder extends AbstractGeocoder {

_formatResult (result = {}) {
const { geometry = {}, properties = {} } = result
const confidence = properties.confidence < 1 ? properties.confidence - 0.1 : 1
const confidence = properties.confidence < 1 ? toFixed(properties.confidence - 0.1) : 1

const formatted = {
formattedAddress: properties.label,
Expand Down
4 changes: 2 additions & 2 deletions src/geocoder/teleport.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import { AbstractGeocoder } from './abstract.js'
import { HttpError } from '../utils/index.js'
import { HttpError, toFixed } from '../utils/index.js'

/** @typedef {import('../adapter').fetchAdapterFn} fetchAdapterFn */

Expand Down Expand Up @@ -137,7 +137,7 @@ export class TeleportGeocoder extends AbstractGeocoder {
}
if (result.distance_km) {
extra.distanceKm = result.distance_km
extra.confidence = Math.max(0, 25 - result.distance_km) / 25 * 10
extra.confidence = toFixed(Math.max(0, 25 - result.distance_km) / 25 * 10)
}
if (result.matching_full_name) {
extra.matchingFullName = result.matching_full_name
Expand Down
Loading

0 comments on commit 0c4bd71

Please sign in to comment.