Skip to content

Commit

Permalink
Merge pull request #338 from ibi-group/fix-here-bugs
Browse files Browse the repository at this point in the history
fix(geocoder): HERE API bugs
  • Loading branch information
daniel-heppner-ibigroup authored Feb 8, 2022
2 parents 4f6d9ac + fec3608 commit 60c9cda
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/geocoder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"dependencies": {
"@conveyal/geocoder-arcgis-geojson": "^0.0.3",
"@conveyal/lonlat": "^1.4.1",
"isomorphic-mapzen-search": "^1.6.0",
"isomorphic-mapzen-search": "^1.6.1",
"lodash.memoize": "^4.1.2"
},
"gitHead": "644c72e0d08f8daf93b44eaf0deec88a1e16f414",
Expand Down
31 changes: 24 additions & 7 deletions packages/geocoder/src/apis/here/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { stringify } from "querystring";
// eslint-disable-next-line prettier/prettier
import type { LonLatOutput } from "@conveyal/lonlat"
import type { AutocompleteQuery, ReverseQuery, SearchQuery } from "../../geocoders/types"
import type { HereResponse } from "./types";
import type { Boundary, HereResponse, Item } from "./types";

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

const checkItemInBoundary = ({ rect }: Boundary) => ({ position }: Item) => {
const { maxLat, maxLon, minLat, minLon } = rect
const { lat, lng } = position
return lng <= maxLon && lng >= minLon && lat <= maxLat && lat >= minLat
}

/**
* Search for an address using
* Here's {@link https://developer.here.com/documentation/geocoding-search-api/api-reference-swagger.html|Autocomplete}
Expand All @@ -55,7 +61,7 @@ function run({ options, query, url }: HereFetchArgs): Promise<HereResponse> {
* @param {string} $0.text query text
* @return {Promise} A Promise that'll get resolved with the autocomplete result
*/
function autocomplete({
async function autocomplete({
apiKey,
boundary,
focusPoint,
Expand All @@ -66,6 +72,21 @@ function autocomplete({
// build query
const query: HereQuery = { apiKey, limit: size, q: text, show: "details" };

if (focusPoint) {
const { lat, lon }: LonLatOutput = normalize(focusPoint);
query.at = `${lat},${lon}`;
const res = await run({
options,
query,
url: AUTOCOMPLETE_URL
});
if (boundary?.rect) {
// HERE does not support a boundary when you use a focus point
// This workaround filters the results internally to the boundary
res.items = res.items.filter(checkItemInBoundary(boundary))
}
return res
}
if (boundary) {
const { country, rect } = boundary;
if (country) query.in = `countryCode:${country}`;
Expand All @@ -77,12 +98,8 @@ function autocomplete({
rect.maxLat
].join(",")}`;
}
} else if (focusPoint) {
// Only add focusPoint if the boundary is not specified.
// HERE only supports one or the other, not both.
const { lat, lon }: LonLatOutput = normalize(focusPoint);
query.at = `${lat},${lon}`;
}

return run({
options,
query,
Expand Down
4 changes: 2 additions & 2 deletions packages/geocoder/src/apis/here/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export type Rect = {
};

export type Boundary = {
country: string;
rect: Rect;
country?: string;
rect?: Rect;
};

export interface Item {
Expand Down
5 changes: 3 additions & 2 deletions packages/geocoder/src/geocoders/here.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ export default class HereGeocoder extends Geocoder {
const { items } = response;
return {
features: items
// Here can return continued query suggestions, which we do not support.
?.filter(item => item.resultType !== "chainQuery")
// Here has various types of responses, some of which are not locations.
// We only want the actual places, so throw out any without a position.
?.filter(item => !!item.position)
.map(convertHereToGeojson),
type: "FeatureCollection"
};
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12152,10 +12152,10 @@ isomorphic-fetch@^3.0.0:
node-fetch "^2.6.1"
whatwg-fetch "^3.4.1"

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

0 comments on commit 60c9cda

Please sign in to comment.