|
| 1 | +from cmd import Cmd |
| 2 | +import objectbox |
| 3 | +import time |
| 4 | +from .model import * |
| 5 | +import csv |
| 6 | +import os |
| 7 | + |
| 8 | +def list_cities(cities): |
| 9 | + print("{:3s} {:25s} {:>9s} {:>9s}".format("ID", "Name", "Latitude", "Longitude")) |
| 10 | + for city in cities: |
| 11 | + print("{:3d} {:25s} {:>9.2f} {:>9.2f}".format( |
| 12 | + city.id, city.name, city.location[0], city.location[1])) |
| 13 | + |
| 14 | +def list_cities_with_scores(city_score_tuples): |
| 15 | + print("{:3s} {:25s} {:>9s} {:>9s} {:>5s}".format("ID", "Name", "Latitude", "Longitude", "Score")) |
| 16 | + for (city,score) in city_score_tuples: |
| 17 | + print("{:3d} {:25s} {:>9.2f} {:>9.2f} {:>5.2f}".format( |
| 18 | + city.id, city.name, city.location[0], city.location[1], score)) |
| 19 | + |
| 20 | +class VectorSearchCitiesCmd(Cmd): |
| 21 | + prompt = "> " |
| 22 | + def __init__(self, *args): |
| 23 | + Cmd.__init__(self, *args) |
| 24 | + dbdir = "cities-db" |
| 25 | + new_db = not os.path.exists(dbdir) |
| 26 | + self._ob = objectbox.Builder().model(get_objectbox_model()).directory(dbdir).build() |
| 27 | + self._box = objectbox.Box(self._ob, City) |
| 28 | + self._name_prop: Property = City.get_property("name") |
| 29 | + self._location_prop: Property = City.get_property("location") |
| 30 | + if new_db: |
| 31 | + with open(os.path.join(os.path.dirname(__file__), 'cities.csv')) as f: |
| 32 | + r = csv.reader(f) |
| 33 | + cities = [] |
| 34 | + for row in r: |
| 35 | + city = City() |
| 36 | + city.name = row[0] |
| 37 | + city.location = [ row[1], row[2] ] |
| 38 | + cities.append(city) |
| 39 | + self._box.put(*cities) |
| 40 | + |
| 41 | + def do_ls(self, name: str = ""): |
| 42 | + """list all cities or starting with <prefix>\nusage: ls [<prefix>]""" |
| 43 | + qb = self._box.query() |
| 44 | + qb.starts_with_string(self._name_prop, name) |
| 45 | + query = qb.build() |
| 46 | + list_cities(query.find()) |
| 47 | + |
| 48 | + def do_city_neighbors(self, args: str): |
| 49 | + """find <num> (default: 5) next neighbors to city <name>\nusage: city_neighbors <name> [,<num>]""" |
| 50 | + try: |
| 51 | + args = args.split(',') |
| 52 | + if len(args) > 2: |
| 53 | + raise ValueError() |
| 54 | + city = args[0] |
| 55 | + if len(city) == 0: |
| 56 | + raise ValueError() |
| 57 | + num = 5 |
| 58 | + if len(args) == 2: |
| 59 | + num = int(args[1]) |
| 60 | + qb = self._box.query() |
| 61 | + qb.equals_string(self._name_prop, city) |
| 62 | + query = qb.build() |
| 63 | + cities = query.find() |
| 64 | + if len(cities) == 1: |
| 65 | + location = cities[0].location |
| 66 | + qb = self._box.query() |
| 67 | + qb.nearest_neighbors_f32(self._location_prop, location, num+1) # +1 for the city |
| 68 | + qb.not_equals_string(self._name_prop, city) |
| 69 | + neighbors = qb.build().find_with_scores() |
| 70 | + list_cities_with_scores(neighbors) |
| 71 | + else: |
| 72 | + print(f"no city found named '{city}'") |
| 73 | + except ValueError: |
| 74 | + print("usage: city_neighbors <name>[,<num: default 5>]") |
| 75 | + |
| 76 | + def do_neighbors(self, args): |
| 77 | + """find <num> neighbors next to geo-coord <lat> <long>.\nusage: neighbors <num>,<latitude>,<longitude>""" |
| 78 | + try: |
| 79 | + args = args.split(',') |
| 80 | + if len(args) != 3: |
| 81 | + raise ValueError() |
| 82 | + num = int(args[0]) |
| 83 | + geocoord = [ float(args[1]), float(args[2]) ] |
| 84 | + qb = self._box.query() |
| 85 | + qb.nearest_neighbors_f32(self._location_prop, geocoord, num) |
| 86 | + neighbors = qb.build().find_with_scores() |
| 87 | + list_cities_with_scores(neighbors) |
| 88 | + except ValueError: |
| 89 | + print("usage: neighbors <num>,<latitude>,<longitude>") |
| 90 | + |
| 91 | + def do_add(self, args: str): |
| 92 | + """add new location\nusage: add <name>,<lat>,<long>""" |
| 93 | + try: |
| 94 | + args = args.split(',') |
| 95 | + if len(args) != 3: |
| 96 | + raise ValueError() |
| 97 | + name = str(args[0]) |
| 98 | + lat = float(args[1]) |
| 99 | + long = float(args[2]) |
| 100 | + city = City() |
| 101 | + city.name = name |
| 102 | + city.location = [lat,long] |
| 103 | + self._box.put(city) |
| 104 | + except ValueError: |
| 105 | + print("usage: add <name>,<latitude>,<longitude>") |
| 106 | + |
| 107 | + def do_exit(self, _): |
| 108 | + """close the program""" |
| 109 | + raise SystemExit() |
| 110 | + |
| 111 | + |
| 112 | +if __name__ == '__main__': |
| 113 | + app = VectorSearchCitiesCmd() |
| 114 | + app.cmdloop('Welcome to the ObjectBox vectorsearch-cities example. Type help or ? for a list of commands.') |
0 commit comments