1 # SPDX-License-Identifier: GPL-3.0-or-later
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2023 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Tests for running the generic place searcher.
14 import nominatim.api as napi
15 from nominatim.api.types import SearchDetails
16 from nominatim.api.search.db_searches import PlaceSearch
17 from nominatim.api.search.db_search_fields import WeightedStrings, WeightedCategories,\
18 FieldLookup, FieldRanking, RankedTokens
20 def run_search(apiobj, global_penalty, lookup, ranking, count=2,
21 hnrs=[], pcs=[], ccodes=[], quals=[],
22 details=SearchDetails()):
24 penalty = global_penalty
25 postcodes = WeightedStrings(pcs, [0.0] * len(pcs))
26 countries = WeightedStrings(ccodes, [0.0] * len(ccodes))
27 housenumbers = WeightedStrings(hnrs, [0.0] * len(hnrs))
28 qualifiers = WeightedCategories(quals, [0.0] * len(quals))
32 search = PlaceSearch(0.0, MySearchData(), count)
35 async with apiobj.api._async_api.begin() as conn:
36 return await search.lookup(conn, details)
38 results = apiobj.async_to_sync(run())
39 results.sort(key=lambda r: r.accuracy)
44 class TestNameOnlySearches:
46 @pytest.fixture(autouse=True)
47 def fill_database(self, apiobj):
48 apiobj.add_placex(place_id=100, country_code='us',
50 apiobj.add_search_name(100, names=[1,2,10,11], country_code='us',
52 apiobj.add_placex(place_id=101, country_code='mx',
53 centroid=(-10.3, 56.9))
54 apiobj.add_search_name(101, names=[1,2,20,21], country_code='mx',
55 centroid=(-10.3, 56.9))
58 @pytest.mark.parametrize('lookup_type', ['lookup_all', 'restrict'])
59 @pytest.mark.parametrize('rank,res', [([10], [100, 101]),
61 def test_lookup_all_match(self, apiobj, lookup_type, rank, res):
62 lookup = FieldLookup('name_vector', [1,2], lookup_type)
63 ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, rank)])
65 results = run_search(apiobj, 0.1, [lookup], [ranking])
67 assert [r.place_id for r in results] == res
70 @pytest.mark.parametrize('lookup_type', ['lookup_all', 'restrict'])
71 def test_lookup_all_partial_match(self, apiobj, lookup_type):
72 lookup = FieldLookup('name_vector', [1,20], lookup_type)
73 ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, [21])])
75 results = run_search(apiobj, 0.1, [lookup], [ranking])
77 assert len(results) == 1
78 assert results[0].place_id == 101
80 @pytest.mark.parametrize('rank,res', [([10], [100, 101]),
82 def test_lookup_any_match(self, apiobj, rank, res):
83 lookup = FieldLookup('name_vector', [11,21], 'lookup_any')
84 ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, rank)])
86 results = run_search(apiobj, 0.1, [lookup], [ranking])
88 assert [r.place_id for r in results] == res
91 def test_lookup_any_partial_match(self, apiobj):
92 lookup = FieldLookup('name_vector', [20], 'lookup_all')
93 ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, [21])])
95 results = run_search(apiobj, 0.1, [lookup], [ranking])
97 assert len(results) == 1
98 assert results[0].place_id == 101
101 @pytest.mark.parametrize('cc,res', [('us', 100), ('mx', 101)])
102 def test_lookup_restrict_country(self, apiobj, cc, res):
103 lookup = FieldLookup('name_vector', [1,2], 'lookup_all')
104 ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, [10])])
106 results = run_search(apiobj, 0.1, [lookup], [ranking], ccodes=[cc])
108 assert [r.place_id for r in results] == [res]
111 def test_lookup_restrict_placeid(self, apiobj):
112 lookup = FieldLookup('name_vector', [1,2], 'lookup_all')
113 ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, [10])])
115 results = run_search(apiobj, 0.1, [lookup], [ranking],
116 details=SearchDetails(excluded=[101]))
118 assert [r.place_id for r in results] == [100]
121 @pytest.mark.parametrize('geom', [napi.GeometryFormat.GEOJSON,
122 napi.GeometryFormat.KML,
123 napi.GeometryFormat.SVG,
124 napi.GeometryFormat.TEXT])
125 def test_return_geometries(self, apiobj, geom):
126 lookup = FieldLookup('name_vector', [20], 'lookup_all')
127 ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, [21])])
129 results = run_search(apiobj, 0.1, [lookup], [ranking],
130 details=SearchDetails(geometry_output=geom))
132 assert geom.name.lower() in results[0].geometry
135 @pytest.mark.parametrize('factor,npoints', [(0.0, 3), (1.0, 2)])
136 def test_return_simplified_geometry(self, apiobj, factor, npoints):
137 apiobj.add_placex(place_id=333, country_code='us',
139 geometry='LINESTRING(8.9 9.0, 9.0 9.0, 9.1 9.0)')
140 apiobj.add_search_name(333, names=[55], country_code='us',
143 lookup = FieldLookup('name_vector', [55], 'lookup_all')
144 ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, [21])])
146 results = run_search(apiobj, 0.1, [lookup], [ranking],
147 details=SearchDetails(geometry_output=napi.GeometryFormat.GEOJSON,
148 geometry_simplification=factor))
150 assert len(results) == 1
152 geom = json.loads(result.geometry['geojson'])
154 assert result.place_id == 333
155 assert len(geom['coordinates']) == npoints
158 @pytest.mark.parametrize('viewbox', ['5.0,4.0,6.0,5.0', '5.7,4.0,6.0,5.0'])
159 @pytest.mark.parametrize('wcount,rids', [(2, [100, 101]), (20000, [100])])
160 def test_prefer_viewbox(self, apiobj, viewbox, wcount, rids):
161 lookup = FieldLookup('name_vector', [1, 2], 'lookup_all')
162 ranking = FieldRanking('name_vector', 0.2, [RankedTokens(0.0, [21])])
164 results = run_search(apiobj, 0.1, [lookup], [ranking])
165 assert [r.place_id for r in results] == [101, 100]
167 results = run_search(apiobj, 0.1, [lookup], [ranking], count=wcount,
168 details=SearchDetails.from_kwargs({'viewbox': viewbox}))
169 assert [r.place_id for r in results] == rids
172 @pytest.mark.parametrize('viewbox', ['5.0,4.0,6.0,5.0', '5.55,4.27,5.62,4.31'])
173 def test_force_viewbox(self, apiobj, viewbox):
174 lookup = FieldLookup('name_vector', [1, 2], 'lookup_all')
176 details=SearchDetails.from_kwargs({'viewbox': viewbox,
177 'bounded_viewbox': True})
179 results = run_search(apiobj, 0.1, [lookup], [], details=details)
180 assert [r.place_id for r in results] == [100]
183 def test_prefer_near(self, apiobj):
184 lookup = FieldLookup('name_vector', [1, 2], 'lookup_all')
185 ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, [21])])
187 results = run_search(apiobj, 0.1, [lookup], [ranking])
188 assert [r.place_id for r in results] == [101, 100]
190 results = run_search(apiobj, 0.1, [lookup], [ranking],
191 details=SearchDetails.from_kwargs({'near': '5.6,4.3'}))
192 results.sort(key=lambda r: -r.importance)
193 assert [r.place_id for r in results] == [100, 101]
196 @pytest.mark.parametrize('radius', [0.09, 0.11])
197 def test_force_near(self, apiobj, radius):
198 lookup = FieldLookup('name_vector', [1, 2], 'lookup_all')
200 details=SearchDetails.from_kwargs({'near': '5.6,4.3',
201 'near_radius': radius})
203 results = run_search(apiobj, 0.1, [lookup], [], details=details)
205 assert [r.place_id for r in results] == [100]
208 class TestStreetWithHousenumber:
210 @pytest.fixture(autouse=True)
211 def fill_database(self, apiobj):
212 apiobj.add_placex(place_id=1, class_='place', type='house',
213 parent_place_id=1000,
214 housenumber='20 a', country_code='es')
215 apiobj.add_placex(place_id=2, class_='place', type='house',
216 parent_place_id=1000,
217 housenumber='21;22', country_code='es')
218 apiobj.add_placex(place_id=1000, class_='highway', type='residential',
219 rank_search=26, rank_address=26,
221 apiobj.add_search_name(1000, names=[1,2,10,11],
222 search_rank=26, address_rank=26,
224 apiobj.add_placex(place_id=91, class_='place', type='house',
225 parent_place_id=2000,
226 housenumber='20', country_code='pt')
227 apiobj.add_placex(place_id=92, class_='place', type='house',
228 parent_place_id=2000,
229 housenumber='22', country_code='pt')
230 apiobj.add_placex(place_id=93, class_='place', type='house',
231 parent_place_id=2000,
232 housenumber='24', country_code='pt')
233 apiobj.add_placex(place_id=2000, class_='highway', type='residential',
234 rank_search=26, rank_address=26,
236 apiobj.add_search_name(2000, names=[1,2,20,21],
237 search_rank=26, address_rank=26,
241 @pytest.mark.parametrize('hnr,res', [('20', [91, 1]), ('20 a', [1]),
242 ('21', [2]), ('22', [2, 92]),
243 ('24', [93]), ('25', [])])
244 def test_lookup_by_single_housenumber(self, apiobj, hnr, res):
245 lookup = FieldLookup('name_vector', [1,2], 'lookup_all')
246 ranking = FieldRanking('name_vector', 0.3, [RankedTokens(0.0, [10])])
248 results = run_search(apiobj, 0.1, [lookup], [ranking], hnrs=[hnr])
250 assert [r.place_id for r in results] == res + [1000, 2000]
253 @pytest.mark.parametrize('cc,res', [('es', [2, 1000]), ('pt', [92, 2000])])
254 def test_lookup_with_country_restriction(self, apiobj, cc, res):
255 lookup = FieldLookup('name_vector', [1,2], 'lookup_all')
256 ranking = FieldRanking('name_vector', 0.3, [RankedTokens(0.0, [10])])
258 results = run_search(apiobj, 0.1, [lookup], [ranking], hnrs=['22'],
261 assert [r.place_id for r in results] == res
264 def test_lookup_exclude_housenumber_placeid(self, apiobj):
265 lookup = FieldLookup('name_vector', [1,2], 'lookup_all')
266 ranking = FieldRanking('name_vector', 0.3, [RankedTokens(0.0, [10])])
268 results = run_search(apiobj, 0.1, [lookup], [ranking], hnrs=['22'],
269 details=SearchDetails(excluded=[92]))
271 assert [r.place_id for r in results] == [2, 1000, 2000]
274 def test_lookup_exclude_street_placeid(self, apiobj):
275 lookup = FieldLookup('name_vector', [1,2], 'lookup_all')
276 ranking = FieldRanking('name_vector', 0.3, [RankedTokens(0.0, [10])])
278 results = run_search(apiobj, 0.1, [lookup], [ranking], hnrs=['22'],
279 details=SearchDetails(excluded=[1000]))
281 assert [r.place_id for r in results] == [2, 92, 2000]
284 def test_lookup_only_house_qualifier(self, apiobj):
285 lookup = FieldLookup('name_vector', [1,2], 'lookup_all')
286 ranking = FieldRanking('name_vector', 0.3, [RankedTokens(0.0, [10])])
288 results = run_search(apiobj, 0.1, [lookup], [ranking], hnrs=['22'],
289 quals=[('place', 'house')])
291 assert [r.place_id for r in results] == [2, 92]
294 def test_lookup_only_street_qualifier(self, apiobj):
295 lookup = FieldLookup('name_vector', [1,2], 'lookup_all')
296 ranking = FieldRanking('name_vector', 0.3, [RankedTokens(0.0, [10])])
298 results = run_search(apiobj, 0.1, [lookup], [ranking], hnrs=['22'],
299 quals=[('highway', 'residential')])
301 assert [r.place_id for r in results] == [1000, 2000]
304 @pytest.mark.parametrize('rank,found', [(26, True), (27, False), (30, False)])
305 def test_lookup_min_rank(self, apiobj, rank, found):
306 lookup = FieldLookup('name_vector', [1,2], 'lookup_all')
307 ranking = FieldRanking('name_vector', 0.3, [RankedTokens(0.0, [10])])
309 results = run_search(apiobj, 0.1, [lookup], [ranking], hnrs=['22'],
310 details=SearchDetails(min_rank=rank))
312 assert [r.place_id for r in results] == ([2, 92, 1000, 2000] if found else [2, 92])
315 @pytest.mark.parametrize('geom', [napi.GeometryFormat.GEOJSON,
316 napi.GeometryFormat.KML,
317 napi.GeometryFormat.SVG,
318 napi.GeometryFormat.TEXT])
319 def test_return_geometries(self, apiobj, geom):
320 lookup = FieldLookup('name_vector', [1, 2], 'lookup_all')
322 results = run_search(apiobj, 0.1, [lookup], [], hnrs=['20', '21', '22'],
323 details=SearchDetails(geometry_output=geom))
326 assert all(geom.name.lower() in r.geometry for r in results)
329 def test_very_large_housenumber(apiobj):
330 apiobj.add_placex(place_id=93, class_='place', type='house',
331 parent_place_id=2000,
332 housenumber='2467463524544', country_code='pt')
333 apiobj.add_placex(place_id=2000, class_='highway', type='residential',
334 rank_search=26, rank_address=26,
336 apiobj.add_search_name(2000, names=[1,2],
337 search_rank=26, address_rank=26,
340 lookup = FieldLookup('name_vector', [1, 2], 'lookup_all')
342 results = run_search(apiobj, 0.1, [lookup], [], hnrs=['2467463524544'],
343 details=SearchDetails())
346 assert [r.place_id for r in results] == [93, 2000]
349 @pytest.mark.parametrize('wcount,rids', [(2, [990, 991]), (30000, [990])])
350 def test_name_and_postcode(apiobj, wcount, rids):
351 apiobj.add_placex(place_id=990, class_='highway', type='service',
352 rank_search=27, rank_address=27,
354 centroid=(10.0, 10.0),
355 geometry='LINESTRING(9.995 10, 10.005 10)')
356 apiobj.add_search_name(990, names=[111], centroid=(10.0, 10.0),
357 search_rank=27, address_rank=27)
358 apiobj.add_placex(place_id=991, class_='highway', type='service',
359 rank_search=27, rank_address=27,
361 centroid=(10.1, 10.1),
362 geometry='LINESTRING(9.995 10.1, 10.005 10.1)')
363 apiobj.add_search_name(991, names=[111], centroid=(10.1, 10.1),
364 search_rank=27, address_rank=27)
365 apiobj.add_postcode(place_id=100, country_code='ch', postcode='11225',
366 geometry='POINT(10 10)')
368 lookup = FieldLookup('name_vector', [111], 'lookup_all')
370 results = run_search(apiobj, 0.1, [lookup], [], pcs=['11225'], count=wcount,
371 details=SearchDetails())
374 assert [r.place_id for r in results] == rids
377 class TestInterpolations:
379 @pytest.fixture(autouse=True)
380 def fill_database(self, apiobj):
381 apiobj.add_placex(place_id=990, class_='highway', type='service',
382 rank_search=27, rank_address=27,
383 centroid=(10.0, 10.0),
384 geometry='LINESTRING(9.995 10, 10.005 10)')
385 apiobj.add_search_name(990, names=[111],
386 search_rank=27, address_rank=27)
387 apiobj.add_placex(place_id=991, class_='place', type='house',
389 rank_search=30, rank_address=30,
391 centroid=(10.0, 10.00002))
392 apiobj.add_osmline(place_id=992,
394 startnumber=21, endnumber=29, step=2,
395 centroid=(10.0, 10.00001),
396 geometry='LINESTRING(9.995 10.00001, 10.005 10.00001)')
399 @pytest.mark.parametrize('hnr,res', [('21', [992]), ('22', []), ('23', [991])])
400 def test_lookup_housenumber(self, apiobj, hnr, res):
401 lookup = FieldLookup('name_vector', [111], 'lookup_all')
403 results = run_search(apiobj, 0.1, [lookup], [], hnrs=[hnr])
405 assert [r.place_id for r in results] == res + [990]
408 @pytest.mark.parametrize('geom', [napi.GeometryFormat.GEOJSON,
409 napi.GeometryFormat.KML,
410 napi.GeometryFormat.SVG,
411 napi.GeometryFormat.TEXT])
412 def test_osmline_with_geometries(self, apiobj, geom):
413 lookup = FieldLookup('name_vector', [111], 'lookup_all')
415 results = run_search(apiobj, 0.1, [lookup], [], hnrs=['21'],
416 details=SearchDetails(geometry_output=geom))
418 assert results[0].place_id == 992
419 assert geom.name.lower() in results[0].geometry
425 @pytest.fixture(autouse=True)
426 def fill_database(self, apiobj):
427 apiobj.add_placex(place_id=990, class_='highway', type='service',
428 rank_search=27, rank_address=27,
430 centroid=(10.0, 10.0),
431 geometry='LINESTRING(9.995 10, 10.005 10)')
432 apiobj.add_search_name(990, names=[111], country_code='us',
433 search_rank=27, address_rank=27)
434 apiobj.add_placex(place_id=991, class_='place', type='house',
436 rank_search=30, rank_address=30,
439 centroid=(10.0, 10.00002))
440 apiobj.add_tiger(place_id=992,
442 startnumber=21, endnumber=29, step=2,
443 centroid=(10.0, 10.00001),
444 geometry='LINESTRING(9.995 10.00001, 10.005 10.00001)')
447 @pytest.mark.parametrize('hnr,res', [('21', [992]), ('22', []), ('23', [991])])
448 def test_lookup_housenumber(self, apiobj, hnr, res):
449 lookup = FieldLookup('name_vector', [111], 'lookup_all')
451 results = run_search(apiobj, 0.1, [lookup], [], hnrs=[hnr])
453 assert [r.place_id for r in results] == res + [990]
456 @pytest.mark.parametrize('geom', [napi.GeometryFormat.GEOJSON,
457 napi.GeometryFormat.KML,
458 napi.GeometryFormat.SVG,
459 napi.GeometryFormat.TEXT])
460 def test_tiger_with_geometries(self, apiobj, geom):
461 lookup = FieldLookup('name_vector', [111], 'lookup_all')
463 results = run_search(apiobj, 0.1, [lookup], [], hnrs=['21'],
464 details=SearchDetails(geometry_output=geom))
466 assert results[0].place_id == 992
467 assert geom.name.lower() in results[0].geometry
470 class TestLayersRank30:
472 @pytest.fixture(autouse=True)
473 def fill_database(self, apiobj):
474 apiobj.add_placex(place_id=223, class_='place', type='house',
478 apiobj.add_search_name(223, names=[34],
480 address_rank=30, search_rank=30)
481 apiobj.add_placex(place_id=224, class_='amenity', type='toilet',
484 apiobj.add_search_name(224, names=[34],
486 address_rank=30, search_rank=30)
487 apiobj.add_placex(place_id=225, class_='man_made', type='tower',
490 apiobj.add_search_name(225, names=[34],
492 address_rank=0, search_rank=30)
493 apiobj.add_placex(place_id=226, class_='railway', type='station',
496 apiobj.add_search_name(226, names=[34],
498 address_rank=0, search_rank=30)
499 apiobj.add_placex(place_id=227, class_='natural', type='cave',
502 apiobj.add_search_name(227, names=[34],
504 address_rank=0, search_rank=30)
507 @pytest.mark.parametrize('layer,res', [(napi.DataLayer.ADDRESS, [223]),
508 (napi.DataLayer.POI, [224]),
509 (napi.DataLayer.ADDRESS | napi.DataLayer.POI, [223, 224]),
510 (napi.DataLayer.MANMADE, [225]),
511 (napi.DataLayer.RAILWAY, [226]),
512 (napi.DataLayer.NATURAL, [227]),
513 (napi.DataLayer.MANMADE | napi.DataLayer.NATURAL, [225, 227]),
514 (napi.DataLayer.MANMADE | napi.DataLayer.RAILWAY, [225, 226])])
515 def test_layers_rank30(self, apiobj, layer, res):
516 lookup = FieldLookup('name_vector', [34], 'lookup_any')
518 results = run_search(apiobj, 0.1, [lookup], [],
519 details=SearchDetails(layers=layer))
521 assert [r.place_id for r in results] == res