2024-08-15 01:51:33 +02:00
|
|
|
import GeocodingProvider, { GeoCodeResult, GeocodingOptions } from "./GeocodingProvider"
|
2024-08-22 02:54:46 +02:00
|
|
|
import { Utils } from "../../Utils"
|
2024-08-15 01:51:33 +02:00
|
|
|
|
|
|
|
export default class CombinedSearcher implements GeocodingProvider {
|
|
|
|
private _providers: ReadonlyArray<GeocodingProvider>
|
|
|
|
private _providersWithSuggest: ReadonlyArray<GeocodingProvider>
|
|
|
|
|
|
|
|
constructor(...providers: ReadonlyArray<GeocodingProvider>) {
|
2024-08-22 02:54:46 +02:00
|
|
|
this._providers = Utils.NoNull(providers)
|
|
|
|
this._providersWithSuggest = this._providers.filter(pr => pr.suggest !== undefined)
|
2024-08-15 01:51:33 +02:00
|
|
|
}
|
|
|
|
|
2024-08-21 14:06:42 +02:00
|
|
|
/**
|
|
|
|
* Merges the geocode-results from various sources.
|
|
|
|
* If the same osm-id is mentioned multiple times, only the first result will be kept
|
|
|
|
* @param geocoded
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
private merge(geocoded: GeoCodeResult[][]): GeoCodeResult[]{
|
|
|
|
const results : GeoCodeResult[] = []
|
|
|
|
const seenIds = new Set<string>()
|
|
|
|
for (const geocodedElement of geocoded) {
|
|
|
|
for (const entry of geocodedElement) {
|
|
|
|
const id = entry.osm_type+ entry.osm_id
|
|
|
|
if(seenIds.has(id)){
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
seenIds.add(id)
|
|
|
|
results.push(entry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
2024-08-15 01:51:33 +02:00
|
|
|
async search(query: string, options?: GeocodingOptions): Promise<GeoCodeResult[]> {
|
|
|
|
const results = await Promise.all(this._providers.map(pr => pr.search(query, options)))
|
2024-08-21 14:06:42 +02:00
|
|
|
return this.merge(results)
|
2024-08-15 01:51:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async suggest(query: string, options?: GeocodingOptions): Promise<GeoCodeResult[]> {
|
|
|
|
const results = await Promise.all(this._providersWithSuggest.map(pr => pr.suggest(query, options)))
|
2024-08-21 14:06:42 +02:00
|
|
|
return this.merge(results)
|
2024-08-15 01:51:33 +02:00
|
|
|
}
|
|
|
|
}
|