Skip to content
This repository has been archived by the owner on Dec 4, 2018. It is now read-only.

add autocompletion support to OSM Scout nearby search #52

Merged
merged 2 commits into from
Jul 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions guides/osmscout.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
"""

import copy
import functools
import poor
import unicodedata
import urllib.parse

URL_SEARCH = ("http://localhost:8553/v1/guide"
Expand All @@ -42,6 +44,26 @@

cache = {}

def autocomplete_type(query, params=None):
"""Return a list of autocomplete dictionaries matching `query`."""
query = normalize(query)
results = []
for t in get_types():
pos = t["normalized"].find(query)
if pos < 0: continue
results.append({"label": t["original"]})
if len(results) > 100:
break
return results

@functools.lru_cache(1)
def get_types():
"""Get list of types"""
types = []
for t in poor.http.get_json("http://localhost:8553/v1/poi_types"):
types.append( { "original": t, "normalized": normalize(t) } )
return types

def nearby(query, near, radius, params):
"""Return X, Y and a list of dictionaries of places matching `query`."""
query = urllib.parse.quote_plus(query)
Expand Down Expand Up @@ -71,6 +93,10 @@ def nearby(query, near, radius, params):
cache[url] = copy.deepcopy((x, y, results))
return x, y, results

def normalize(t):
"""Normalize the string"""
return unicodedata.normalize("NFKC", t).casefold()

def parse_description(result):
"""Parse description from search result."""
items = []
Expand Down
2 changes: 1 addition & 1 deletion guides/osmscout_name.qml
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ Dialog {
function filterHistory() {
// Filter search history for current search field text.
var query = listView.searchField.text;
var found = Util.findMatches(query, dialog.history, listView.model.count);
var found = Util.findMatches(query, dialog.history, [], listView.model.count);
Util.injectMatches(listView.model, found, "name", "text");
viewPlaceholder.enabled = found.length === 0;
}
Expand Down