-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #535 from ibi-group/add-otp-geocoder
Add OTP Geocoder
- Loading branch information
Showing
6 changed files
with
179 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
|
||
// eslint-disable-next-line prettier/prettier | ||
import type { AutocompleteQuery } from "../../geocoders/types" | ||
|
||
type FetchArgs = { | ||
url: string | ||
query: string | ||
} | ||
|
||
type OTPGeocoderResponse = { | ||
results: { | ||
lat: number, | ||
lng: number, | ||
description: string, | ||
id: string | ||
}[] | ||
} | undefined | ||
|
||
|
||
function run({ query, url }: FetchArgs): Promise<OTPGeocoderResponse> { | ||
// TODO: Support corners/osm nodes? | ||
return fetch(`${url}/geocode?corners=false&query=${query}`) | ||
.then(res => res.text()) | ||
.then(res => JSON.parse(`{"results": ${res}}`)); | ||
} | ||
|
||
/** | ||
* Search for an address using | ||
* OTP Geocoder | ||
* | ||
* @param {Object} $0 | ||
* @param {string} $0.url The OTP instance, ending with /default/ | ||
* @param {string} $0.text query | ||
* @return {Promise} A Promise that'll get resolved with the autocomplete result | ||
*/ | ||
async function autocomplete({ | ||
url, | ||
text | ||
}: AutocompleteQuery): Promise<OTPGeocoderResponse> { | ||
return run({ | ||
query: text, | ||
url | ||
}) | ||
} | ||
|
||
function search(): Promise<unknown> { console.warn("Not implemented"); return null } | ||
function reverse(): Promise<unknown> { console.warn("Not implemented"); return null } | ||
|
||
|
||
export { autocomplete, reverse, search }; | ||
export type { OTPGeocoderResponse } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Prettier does not support typescript annotation | ||
// eslint-disable-next-line prettier/prettier | ||
import type { AutocompleteQuery, MultiGeocoderResponse } from "./types"; | ||
|
||
import Geocoder from "./abstract-geocoder"; | ||
import { OTPGeocoderResponse } from "../apis/otp"; | ||
|
||
/** | ||
* Allows fetching results from OTP instance with the geocoder endpoint enabled | ||
*/ | ||
export default class OTPGeocoder extends Geocoder { | ||
getAutocompleteQuery(query: AutocompleteQuery): AutocompleteQuery { | ||
const { | ||
baseUrl, | ||
} = this.geocoderConfig; | ||
return { | ||
url: baseUrl, | ||
...query | ||
}; | ||
} | ||
|
||
|
||
rewriteAutocompleteResponse(response: OTPGeocoderResponse): MultiGeocoderResponse { | ||
return { | ||
features: response?.results?.map(stop => ({ | ||
geometry: { type: "Point", coordinates: [stop.lng, stop.lat] }, | ||
id: stop.id, | ||
// TODO: if non-stops are supported, these need to be detected here and | ||
// this layer property updated accordingly | ||
properties: { layer: "stops", source: "otp", name: stop.description, label: stop.description }, | ||
type: "Feature" | ||
})), | ||
type: "FeatureCollection" | ||
}; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters