forked from MapComplete/MapComplete
Search: use 'searchbar' where applicable, refactoring
This commit is contained in:
parent
bcd53405c8
commit
9b8c300e77
28 changed files with 403 additions and 582 deletions
|
|
@ -1,12 +1,12 @@
|
|||
import GeocodingProvider, { SearchResult, GeocodingOptions } from "./GeocodingProvider"
|
||||
import GeocodingProvider, { SearchResult, GeocodingOptions, GeocodeResult } from "./GeocodingProvider"
|
||||
import { Utils } from "../../Utils"
|
||||
import { Store, Stores } from "../UIEventSource"
|
||||
|
||||
export default class CombinedSearcher implements GeocodingProvider {
|
||||
private _providers: ReadonlyArray<GeocodingProvider>
|
||||
private _providersWithSuggest: ReadonlyArray<GeocodingProvider>
|
||||
export default class CombinedSearcher implements GeocodingProvider <GeocodeResult> {
|
||||
private _providers: ReadonlyArray<GeocodingProvider<GeocodeResult>>
|
||||
private _providersWithSuggest: ReadonlyArray<GeocodingProvider<GeocodeResult>>
|
||||
|
||||
constructor(...providers: ReadonlyArray<GeocodingProvider>) {
|
||||
constructor(...providers: ReadonlyArray<GeocodingProvider<GeocodeResult>>) {
|
||||
this._providers = Utils.NoNull(providers)
|
||||
this._providersWithSuggest = this._providers.filter(pr => pr.suggest !== undefined)
|
||||
}
|
||||
|
|
@ -17,10 +17,13 @@ export default class CombinedSearcher implements GeocodingProvider {
|
|||
* @param geocoded
|
||||
* @private
|
||||
*/
|
||||
private merge(geocoded: SearchResult[][]): SearchResult[] {
|
||||
const results: SearchResult[] = []
|
||||
public static merge(geocoded: GeocodeResult[][]): GeocodeResult[] {
|
||||
const results: GeocodeResult[] = []
|
||||
const seenIds = new Set<string>()
|
||||
for (const geocodedElement of geocoded) {
|
||||
if(geocodedElement === undefined){
|
||||
continue
|
||||
}
|
||||
for (const entry of geocodedElement) {
|
||||
|
||||
|
||||
|
|
@ -40,13 +43,13 @@ export default class CombinedSearcher implements GeocodingProvider {
|
|||
|
||||
async search(query: string, options?: GeocodingOptions): Promise<SearchResult[]> {
|
||||
const results = (await Promise.all(this._providers.map(pr => pr.search(query, options))))
|
||||
return this.merge(results)
|
||||
return CombinedSearcher.merge(results)
|
||||
}
|
||||
|
||||
suggest(query: string, options?: GeocodingOptions): Store<SearchResult[]> {
|
||||
return Stores.concat(
|
||||
this._providersWithSuggest.map(pr => pr.suggest(query, options)))
|
||||
.map(gcrss => this.merge(gcrss))
|
||||
.map(gcrss => CombinedSearcher.merge(gcrss))
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
import GeocodingProvider, { SearchResult } from "./GeocodingProvider"
|
||||
import GeocodingProvider, { GeocodeResult } from "./GeocodingProvider"
|
||||
import { Utils } from "../../Utils"
|
||||
import { ImmutableStore, Store } from "../UIEventSource"
|
||||
|
||||
/**
|
||||
* A simple search-class which interprets possible locations
|
||||
*/
|
||||
export default class CoordinateSearch implements GeocodingProvider {
|
||||
export default class CoordinateSearch implements GeocodingProvider<GeocodeResult> {
|
||||
private static readonly latLonRegexes: ReadonlyArray<RegExp> = [
|
||||
/^(-?[0-9]+\.[0-9]+)[ ,;/\\]+(-?[0-9]+\.[0-9]+)/,
|
||||
/lat[:=]? *['"]?(-?[0-9]+\.[0-9]+)['"]?[ ,;&]+lon[:=]? *['"]?(-?[0-9]+\.[0-9]+)['"]?/,
|
||||
|
|
@ -59,9 +59,9 @@ export default class CoordinateSearch implements GeocodingProvider {
|
|||
* results.length // => 1
|
||||
* results[0] // => {lat: -57.5802905, lon: -12.7202538, display_name: "lon: -12.7202538, lat: -57.5802905", "category": "coordinate", "source": "coordinate:latlon"}
|
||||
*/
|
||||
private directSearch(query: string): SearchResult[] {
|
||||
private directSearch(query: string): GeocodeResult[] {
|
||||
|
||||
const matches = Utils.NoNull(CoordinateSearch.latLonRegexes.map(r => query.match(r))).map(m => <SearchResult>{
|
||||
const matches = Utils.NoNull(CoordinateSearch.latLonRegexes.map(r => query.match(r))).map(m => <GeocodeResult>{
|
||||
lat: Number(m[1]),
|
||||
lon: Number(m[2]),
|
||||
display_name: "lon: " + m[2] + ", lat: " + m[1],
|
||||
|
|
@ -71,7 +71,7 @@ export default class CoordinateSearch implements GeocodingProvider {
|
|||
|
||||
|
||||
const matchesLonLat = Utils.NoNull(CoordinateSearch.lonLatRegexes.map(r => query.match(r)))
|
||||
.map(m => <SearchResult>{
|
||||
.map(m => <GeocodeResult>{
|
||||
lat: Number(m[2]),
|
||||
lon: Number(m[1]),
|
||||
display_name: "lon: " + m[1] + ", lat: " + m[2],
|
||||
|
|
@ -81,11 +81,11 @@ export default class CoordinateSearch implements GeocodingProvider {
|
|||
return matches.concat(matchesLonLat)
|
||||
}
|
||||
|
||||
suggest(query: string): Store<SearchResult[]> {
|
||||
suggest(query: string): Store<GeocodeResult[]> {
|
||||
return new ImmutableStore(this.directSearch(query))
|
||||
}
|
||||
|
||||
async search (query: string): Promise<SearchResult[]> {
|
||||
async search (query: string): Promise<GeocodeResult[]> {
|
||||
return this.directSearch(query)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,13 +3,15 @@ import GeocodingProvider, { FilterPayload, FilterResult, GeocodingOptions, Searc
|
|||
import { SpecialVisualizationState } from "../../UI/SpecialVisualization"
|
||||
import { Utils } from "../../Utils"
|
||||
import Locale from "../../UI/i18n/Locale"
|
||||
import Constants from "../../Models/Constants"
|
||||
|
||||
export default class FilterSearch implements GeocodingProvider {
|
||||
private readonly _state: SpecialVisualizationState
|
||||
private readonly suggestions
|
||||
|
||||
constructor(state: SpecialVisualizationState) {
|
||||
this._state = state
|
||||
|
||||
this.suggestions = this.getSuggestions()
|
||||
}
|
||||
|
||||
async search(query: string): Promise<SearchResult[]> {
|
||||
|
|
@ -34,7 +36,6 @@ export default class FilterSearch implements GeocodingProvider {
|
|||
}
|
||||
return query
|
||||
}).filter(q => q.length > 0)
|
||||
console.log("Queries:",queries)
|
||||
const possibleFilters: FilterPayload[] = []
|
||||
for (const layer of this._state.layout.layers) {
|
||||
if (!Array.isArray(layer.filters)) {
|
||||
|
|
@ -55,9 +56,9 @@ export default class FilterSearch implements GeocodingProvider {
|
|||
terms = terms.map(t => Utils.simplifyStringForSearch(t))
|
||||
terms.push(option.emoji)
|
||||
Utils.NoNullInplace(terms)
|
||||
const distances = queries.flatMap(query => terms.map(entry => {
|
||||
const distances = queries.flatMap(query => terms.map(entry => {
|
||||
const d = Utils.levenshteinDistance(query, entry.slice(0, query.length))
|
||||
console.log(query,"? +",terms, "=",d)
|
||||
console.log(query, "? +", terms, "=", d)
|
||||
const dRelative = d / query.length
|
||||
return dRelative
|
||||
}))
|
||||
|
|
@ -78,4 +79,37 @@ export default class FilterSearch implements GeocodingProvider {
|
|||
}
|
||||
|
||||
|
||||
getSuggestions(): FilterPayload[] {
|
||||
if (this.suggestions) {
|
||||
// return this.suggestions
|
||||
}
|
||||
const result: FilterPayload[] = []
|
||||
for (const [id, filteredLayer] of this._state.layerState.filteredLayers) {
|
||||
if (!Array.isArray(filteredLayer.layerDef.filters)) {
|
||||
continue
|
||||
}
|
||||
if (Constants.priviliged_layers.indexOf(id) >= 0) {
|
||||
continue
|
||||
}
|
||||
for (const filter of filteredLayer.layerDef.filters) {
|
||||
const singleFilterResults: FilterPayload[] = []
|
||||
for (let i = 0; i < Math.min(filter.options.length, 5); i++) {
|
||||
const option = filter.options[i]
|
||||
if (option.osmTags === undefined) {
|
||||
continue
|
||||
}
|
||||
singleFilterResults.push({
|
||||
option,
|
||||
filter,
|
||||
index: i,
|
||||
layer: filteredLayer.layerDef
|
||||
})
|
||||
}
|
||||
Utils.shuffle(singleFilterResults)
|
||||
result.push(...singleFilterResults.slice(0,3))
|
||||
}
|
||||
}
|
||||
Utils.shuffle(result)
|
||||
return result.slice(0,6)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -56,16 +56,16 @@ export interface GeocodingOptions {
|
|||
}
|
||||
|
||||
|
||||
export default interface GeocodingProvider {
|
||||
export default interface GeocodingProvider<T extends SearchResult = SearchResult> {
|
||||
|
||||
|
||||
search(query: string, options?: GeocodingOptions): Promise<SearchResult[]>
|
||||
search(query: string, options?: GeocodingOptions): Promise<T[]>
|
||||
|
||||
/**
|
||||
* @param query
|
||||
* @param options
|
||||
*/
|
||||
suggest?(query: string, options?: GeocodingOptions): Store<SearchResult[]>
|
||||
suggest?(query: string, options?: GeocodingOptions): Store<T[]>
|
||||
}
|
||||
|
||||
export type ReverseGeocodingResult = Feature<Geometry, {
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
import { Store, UIEventSource } from "../UIEventSource"
|
||||
import GeocodingProvider, { SearchResult, GeocodingOptions } from "./GeocodingProvider"
|
||||
import GeocodingProvider, { GeocodingOptions, GeocodeResult } from "./GeocodingProvider"
|
||||
import { OsmId } from "../../Models/OsmFeature"
|
||||
import { SpecialVisualizationState } from "../../UI/SpecialVisualization"
|
||||
|
||||
export default class OpenStreetMapIdSearch implements GeocodingProvider {
|
||||
private static readonly regex = /((https?:\/\/)?(www.)?(osm|openstreetmap).org\/)?(n|node|w|way|r|relation)[\/ ]?([0-9]+)/
|
||||
export default class OpenStreetMapIdSearch implements GeocodingProvider<GeocodeResult> {
|
||||
private static readonly regex = /((https?:\/\/)?(www.)?(osm|openstreetmap).org\/)?(n|node|w|way|r|relation)[/ ]?([0-9]+)/
|
||||
|
||||
private static readonly types: Readonly<Record<string, "node" | "way" | "relation">> = {
|
||||
"n":"node",
|
||||
|
|
@ -45,7 +45,7 @@ export default class OpenStreetMapIdSearch implements GeocodingProvider {
|
|||
return undefined
|
||||
}
|
||||
|
||||
async search(query: string, options?: GeocodingOptions): Promise<SearchResult[]> {
|
||||
async search(query: string, options?: GeocodingOptions): Promise<GeocodeResult[]> {
|
||||
const id = OpenStreetMapIdSearch.extractId(query)
|
||||
if (!id) {
|
||||
return []
|
||||
|
|
@ -74,7 +74,7 @@ export default class OpenStreetMapIdSearch implements GeocodingProvider {
|
|||
}]
|
||||
}
|
||||
|
||||
suggest?(query: string, options?: GeocodingOptions): Store<SearchResult[]> {
|
||||
suggest?(query: string, options?: GeocodingOptions): Store<GeocodeResult[]> {
|
||||
return UIEventSource.FromPromise(this.search(query, options))
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue