Skip to content

Commit 60c9cda

Browse files
Merge pull request #338 from ibi-group/fix-here-bugs
fix(geocoder): HERE API bugs
2 parents 4f6d9ac + fec3608 commit 60c9cda

File tree

5 files changed

+34
-16
lines changed

5 files changed

+34
-16
lines changed

Diff for: packages/geocoder/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"dependencies": {
1313
"@conveyal/geocoder-arcgis-geojson": "^0.0.3",
1414
"@conveyal/lonlat": "^1.4.1",
15-
"isomorphic-mapzen-search": "^1.6.0",
15+
"isomorphic-mapzen-search": "^1.6.1",
1616
"lodash.memoize": "^4.1.2"
1717
},
1818
"gitHead": "644c72e0d08f8daf93b44eaf0deec88a1e16f414",

Diff for: packages/geocoder/src/apis/here/index.ts

+24-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { stringify } from "querystring";
55
// eslint-disable-next-line prettier/prettier
66
import type { LonLatOutput } from "@conveyal/lonlat"
77
import type { AutocompleteQuery, ReverseQuery, SearchQuery } from "../../geocoders/types"
8-
import type { HereResponse } from "./types";
8+
import type { Boundary, HereResponse, Item } from "./types";
99

1010
const AUTOCOMPLETE_URL =
1111
"https://autosuggest.search.hereapi.com/v1/autosuggest";
@@ -41,6 +41,12 @@ function run({ options, query, url }: HereFetchArgs): Promise<HereResponse> {
4141
return fetch(`${url}?${stringify(query)}`, options).then(res => res.json());
4242
}
4343

44+
const checkItemInBoundary = ({ rect }: Boundary) => ({ position }: Item) => {
45+
const { maxLat, maxLon, minLat, minLon } = rect
46+
const { lat, lng } = position
47+
return lng <= maxLon && lng >= minLon && lat <= maxLat && lat >= minLat
48+
}
49+
4450
/**
4551
* Search for an address using
4652
* Here's {@link https://developer.here.com/documentation/geocoding-search-api/api-reference-swagger.html|Autocomplete}
@@ -55,7 +61,7 @@ function run({ options, query, url }: HereFetchArgs): Promise<HereResponse> {
5561
* @param {string} $0.text query text
5662
* @return {Promise} A Promise that'll get resolved with the autocomplete result
5763
*/
58-
function autocomplete({
64+
async function autocomplete({
5965
apiKey,
6066
boundary,
6167
focusPoint,
@@ -66,6 +72,21 @@ function autocomplete({
6672
// build query
6773
const query: HereQuery = { apiKey, limit: size, q: text, show: "details" };
6874

75+
if (focusPoint) {
76+
const { lat, lon }: LonLatOutput = normalize(focusPoint);
77+
query.at = `${lat},${lon}`;
78+
const res = await run({
79+
options,
80+
query,
81+
url: AUTOCOMPLETE_URL
82+
});
83+
if (boundary?.rect) {
84+
// HERE does not support a boundary when you use a focus point
85+
// This workaround filters the results internally to the boundary
86+
res.items = res.items.filter(checkItemInBoundary(boundary))
87+
}
88+
return res
89+
}
6990
if (boundary) {
7091
const { country, rect } = boundary;
7192
if (country) query.in = `countryCode:${country}`;
@@ -77,12 +98,8 @@ function autocomplete({
7798
rect.maxLat
7899
].join(",")}`;
79100
}
80-
} else if (focusPoint) {
81-
// Only add focusPoint if the boundary is not specified.
82-
// HERE only supports one or the other, not both.
83-
const { lat, lon }: LonLatOutput = normalize(focusPoint);
84-
query.at = `${lat},${lon}`;
85101
}
102+
86103
return run({
87104
options,
88105
query,

Diff for: packages/geocoder/src/apis/here/types.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ export type Rect = {
1515
};
1616

1717
export type Boundary = {
18-
country: string;
19-
rect: Rect;
18+
country?: string;
19+
rect?: Rect;
2020
};
2121

2222
export interface Item {

Diff for: packages/geocoder/src/geocoders/here.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,9 @@ export default class HereGeocoder extends Geocoder {
7373
const { items } = response;
7474
return {
7575
features: items
76-
// Here can return continued query suggestions, which we do not support.
77-
?.filter(item => item.resultType !== "chainQuery")
76+
// Here has various types of responses, some of which are not locations.
77+
// We only want the actual places, so throw out any without a position.
78+
?.filter(item => !!item.position)
7879
.map(convertHereToGeojson),
7980
type: "FeatureCollection"
8081
};

Diff for: yarn.lock

+4-4
Original file line numberDiff line numberDiff line change
@@ -12152,10 +12152,10 @@ isomorphic-fetch@^3.0.0:
1215212152
node-fetch "^2.6.1"
1215312153
whatwg-fetch "^3.4.1"
1215412154

12155-
isomorphic-mapzen-search@^1.6.0:
12156-
version "1.6.0"
12157-
resolved "https://registry.yarnpkg.com/isomorphic-mapzen-search/-/isomorphic-mapzen-search-1.6.0.tgz#eb368756d6132016a32c76435d6f34a38da5279f"
12158-
integrity sha512-90Zp8jxWuMCk582S7d6b85lHO2Lj4Nybv8o6ShqQX2SRP7SFh1fzHx67eilNUGNjBeamKYQJHmijZ1/wSzFe3A==
12155+
isomorphic-mapzen-search@^1.6.1:
12156+
version "1.6.1"
12157+
resolved "https://registry.yarnpkg.com/isomorphic-mapzen-search/-/isomorphic-mapzen-search-1.6.1.tgz#5310e8b845ba70ae4fdd2604c3964264939abe53"
12158+
integrity sha512-MWVko9xX8nhJ2zWx7Zx+9pGQkWd+iy5YIRhT372Rp11/anZ8u5WmOAAxsVA2t2UFmtYEkvNImXCKoFlNg63J+w==
1215912159
dependencies:
1216012160
"@conveyal/lonlat" "^1.4.1"
1216112161
isomorphic-fetch "^3.0.0"

0 commit comments

Comments
 (0)