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
19 from nominatim.api.search.db_search_lookups import LookupAll, LookupAny, Restrict
21 def run_search(apiobj, global_penalty, lookup, ranking, count=2,
22 hnrs=[], pcs=[], ccodes=[], quals=[],
23 details=SearchDetails()):
25 penalty = global_penalty
26 postcodes = WeightedStrings(pcs, [0.0] * len(pcs))
27 countries = WeightedStrings(ccodes, [0.0] * len(ccodes))
28 housenumbers = WeightedStrings(hnrs, [0.0] * len(hnrs))
29 qualifiers = WeightedCategories(quals, [0.0] * len(quals))
33 search = PlaceSearch(0.0, MySearchData(), count)
36 async with apiobj.api._async_api.begin() as conn:
37 return await search.lookup(conn, details)
39 results = apiobj.async_to_sync(run())
40 results.sort(key=lambda r: r.accuracy)
45 class TestNameOnlySearches:
47 @pytest.fixture(autouse=True)
48 def fill_database(self, apiobj):
49 apiobj.add_placex(place_id=100, country_code='us',
51 apiobj.add_search_name(100, names=[1,2,10,11], country_code='us',
53 apiobj.add_placex(place_id=101, country_code='mx',
54 centroid=(-10.3, 56.9))
55 apiobj.add_search_name(101, names=[1,2,20,21], country_code='mx',
56 centroid=(-10.3, 56.9))
59 @pytest.mark.parametrize('lookup_type', [LookupAll, Restrict])
60 @pytest.mark.parametrize('rank,res', [([10], [100, 101]),
62 def test_lookup_all_match(self, apiobj, lookup_type, rank, res):
63 lookup = FieldLookup('name_vector', [1,2], lookup_type)
64 ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, rank)])
66 results = run_search(apiobj, 0.1, [lookup], [ranking])
68 assert [r.place_id for r in results] == res
71 @pytest.mark.parametrize('lookup_type', [LookupAll, Restrict])
72 def test_lookup_all_partial_match(self, apiobj, lookup_type):
73 lookup = FieldLookup('name_vector', [1,20], lookup_type)
74 ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, [21])])
76 results = run_search(apiobj, 0.1, [lookup], [ranking])
78 assert len(results) == 1
79 assert results[0].place_id == 101
81 @pytest.mark.parametrize('rank,res', [([10], [100, 101]),
83 def test_lookup_any_match(self, apiobj, rank, res):
84 lookup = FieldLookup('name_vector', [11,21], LookupAny)
85 ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, rank)])
87 results = run_search(apiobj, 0.1, [lookup], [ranking])
89 assert [r.place_id for r in results] == res
92 def test_lookup_any_partial_match(self, apiobj):
93 lookup = FieldLookup('name_vector', [20], LookupAll)
94 ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, [21])])
96 results = run_search(apiobj, 0.1, [lookup], [ranking])
98 assert len(results) == 1
99 assert results[0].place_id == 101
102 @pytest.mark.parametrize('cc,res', [('us', 100), ('mx', 101)])
103 def test_lookup_restrict_country(self, apiobj, cc, res):
104 lookup = FieldLookup('name_vector', [1,2], LookupAll)
105 ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, [10])])
107 results = run_search(apiobj, 0.1, [lookup], [ranking], ccodes=[cc])
109 assert [r.place_id for r in results] == [res]
112 def test_lookup_restrict_placeid(self, apiobj):
113 lookup = FieldLookup('name_vector', [1,2], LookupAll)
114 ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, [10])])
116 results = run_search(apiobj, 0.1, [lookup], [ranking],
117 details=SearchDetails(excluded=[101]))
119 assert [r.place_id for r in results] == [100]
122 @pytest.mark.parametrize('geom', [napi.GeometryFormat.GEOJSON,
123 napi.GeometryFormat.KML,
124 napi.GeometryFormat.SVG,
125 napi.GeometryFormat.TEXT])
126 def test_return_geometries(self, apiobj, geom):
127 lookup = FieldLookup('name_vector', [20], LookupAll)
128 ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, [21])])
130 results = run_search(apiobj, 0.1, [lookup], [ranking],
131 details=SearchDetails(geometry_output=geom))
133 assert geom.name.lower() in results[0].geometry
136 @pytest.mark.parametrize('factor,npoints', [(0.0, 3), (1.0, 2)])
137 def test_return_simplified_geometry(self, apiobj, factor, npoints):
138 apiobj.add_placex(place_id=333, country_code='us',
140 geometry='LINESTRING(8.9 9.0, 9.0 9.0, 9.1 9.0)')
141 apiobj.add_search_name(333, names=[55], country_code='us',
144 lookup = FieldLookup('name_vector', [55], LookupAll)
145 ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, [21])])
147 results = run_search(apiobj, 0.1, [lookup], [ranking],
148 details=SearchDetails(geometry_output=napi.GeometryFormat.GEOJSON,
149 geometry_simplification=factor))
151 assert len(results) == 1
153 geom = json.loads(result.geometry['geojson'])
155 assert result.place_id == 333
156 assert len(geom['coordinates']) == npoints
159 @pytest.mark.parametrize('viewbox', ['5.0,4.0,6.0,5.0', '5.7,4.0,6.0,5.0'])
160 @pytest.mark.parametrize('wcount,rids', [(2, [100, 101]), (20000, [100])])
161 def test_prefer_viewbox(self, apiobj, viewbox, wcount, rids):
162 lookup = FieldLookup('name_vector', [1, 2], LookupAll)
163 ranking = FieldRanking('name_vector', 0.2, [RankedTokens(0.0, [21])])
165 results = run_search(apiobj, 0.1, [lookup], [ranking])
166 assert [r.place_id for r in results] == [101, 100]
168 results = run_search(apiobj, 0.1, [lookup], [ranking], count=wcount,
169 details=SearchDetails.from_kwargs({'viewbox': viewbox}))
170 assert [r.place_id for r in results] == rids
173 @pytest.mark.parametrize('viewbox', ['5.0,4.0,6.0,5.0', '5.55,4.27,5.62,4.31'])
174 def test_force_viewbox(self, apiobj, viewbox):
175 lookup = FieldLookup('name_vector', [1, 2], LookupAll)
177 details=SearchDetails.from_kwargs({'viewbox': viewbox,
178 'bounded_viewbox': True})
180 results = run_search(apiobj, 0.1, [lookup], [], details=details)
181 assert [r.place_id for r in results] == [100]
184 def test_prefer_near(self, apiobj):
185 lookup = FieldLookup('name_vector', [1, 2], LookupAll)
186 ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, [21])])
188 results = run_search(apiobj, 0.1, [lookup], [ranking])
189 assert [r.place_id for r in results] == [101, 100]
191 results = run_search(apiobj, 0.1, [lookup], [ranking],
192 details=SearchDetails.from_kwargs({'near': '5.6,4.3'}))
193 results.sort(key=lambda r: -r.importance)
194 assert [r.place_id for r in results] == [100, 101]
197 @pytest.mark.parametrize('radius', [0.09, 0.11])
198 def test_force_near(self, apiobj, radius):
199 lookup = FieldLookup('name_vector', [1, 2], LookupAll)
201 details=SearchDetails.from_kwargs({'near': '5.6,4.3',
202 'near_radius': radius})
204 results = run_search(apiobj, 0.1, [lookup], [], details=details)
206 assert [r.place_id for r in results] == [100]
209 class TestStreetWithHousenumber:
211 @pytest.fixture(autouse=True)
212 def fill_database(self, apiobj):
213 apiobj.add_placex(place_id=1, class_='place', type='house',
214 parent_place_id=1000,
215 housenumber='20 a', country_code='es')
216 apiobj.add_placex(place_id=2, class_='place', type='house',
217 parent_place_id=1000,
218 housenumber='21;22', country_code='es')
219 apiobj.add_placex(place_id=1000, class_='highway', type='residential',
220 rank_search=26, rank_address=26,
222 apiobj.add_search_name(1000, names=[1,2,10,11],
223 search_rank=26, address_rank=26,
225 apiobj.add_placex(place_id=91, class_='place', type='house',
226 parent_place_id=2000,
227 housenumber='20', country_code='pt')
228 apiobj.add_placex(place_id=92, class_='place', type='house',
229 parent_place_id=2000,
230 housenumber='22', country_code='pt')
231 apiobj.add_placex(place_id=93, class_='place', type='house',
232 parent_place_id=2000,
233 housenumber='24', country_code='pt')
234 apiobj.add_placex(place_id=2000, class_='highway', type='residential',
235 rank_search=26, rank_address=26,
237 apiobj.add_search_name(2000, names=[1,2,20,21],
238 search_rank=26, address_rank=26,
242 @pytest.mark.parametrize('hnr,res', [('20', [91, 1]), ('20 a', [1]),
243 ('21', [2]), ('22', [2, 92]),
244 ('24', [93]), ('25', [])])
245 def test_lookup_by_single_housenumber(self, apiobj, hnr, res):
246 lookup = FieldLookup('name_vector', [1,2], LookupAll)
247 ranking = FieldRanking('name_vector', 0.3, [RankedTokens(0.0, [10])])
249 results = run_search(apiobj, 0.1, [lookup], [ranking], hnrs=[hnr])
251 assert [r.place_id for r in results] == res + [1000, 2000]
254 @pytest.mark.parametrize('cc,res', [('es', [2, 1000]), ('pt', [92, 2000])])
255 def test_lookup_with_country_restriction(self, apiobj, cc, res):
256 lookup = FieldLookup('name_vector', [1,2], LookupAll)
257 ranking = FieldRanking('name_vector', 0.3, [RankedTokens(0.0, [10])])
259 results = run_search(apiobj, 0.1, [lookup], [ranking], hnrs=['22'],
262 assert [r.place_id for r in results] == res
265 def test_lookup_exclude_housenumber_placeid(self, apiobj):
266 lookup = FieldLookup('name_vector', [1,2], LookupAll)
267 ranking = FieldRanking('name_vector', 0.3, [RankedTokens(0.0, [10])])
269 results = run_search(apiobj, 0.1, [lookup], [ranking], hnrs=['22'],
270 details=SearchDetails(excluded=[92]))
272 assert [r.place_id for r in results] == [2, 1000, 2000]
275 def test_lookup_exclude_street_placeid(self, apiobj):
276 lookup = FieldLookup('name_vector', [1,2], LookupAll)
277 ranking = FieldRanking('name_vector', 0.3, [RankedTokens(0.0, [10])])
279 results = run_search(apiobj, 0.1, [lookup], [ranking], hnrs=['22'],
280 details=SearchDetails(excluded=[1000]))
282 assert [r.place_id for r in results] == [2, 92, 2000]
285 def test_lookup_only_house_qualifier(self, apiobj):
286 lookup = FieldLookup('name_vector', [1,2], LookupAll)
287 ranking = FieldRanking('name_vector', 0.3, [RankedTokens(0.0, [10])])
289 results = run_search(apiobj, 0.1, [lookup], [ranking], hnrs=['22'],
290 quals=[('place', 'house')])
292 assert [r.place_id for r in results] == [2, 92]
295 def test_lookup_only_street_qualifier(self, apiobj):
296 lookup = FieldLookup('name_vector', [1,2], LookupAll)
297 ranking = FieldRanking('name_vector', 0.3, [RankedTokens(0.0, [10])])
299 results = run_search(apiobj, 0.1, [lookup], [ranking], hnrs=['22'],
300 quals=[('highway', 'residential')])
302 assert [r.place_id for r in results] == [1000, 2000]
305 @pytest.mark.parametrize('rank,found', [(26, True), (27, False), (30, False)])
306 def test_lookup_min_rank(self, apiobj, rank, found):
307 lookup = FieldLookup('name_vector', [1,2], LookupAll)
308 ranking = FieldRanking('name_vector', 0.3, [RankedTokens(0.0, [10])])
310 results = run_search(apiobj, 0.1, [lookup], [ranking], hnrs=['22'],
311 details=SearchDetails(min_rank=rank))
313 assert [r.place_id for r in results] == ([2, 92, 1000, 2000] if found else [2, 92])
316 @pytest.mark.parametrize('geom', [napi.GeometryFormat.GEOJSON,
317 napi.GeometryFormat.KML,
318 napi.GeometryFormat.SVG,
319 napi.GeometryFormat.TEXT])
320 def test_return_geometries(self, apiobj, geom):
321 lookup = FieldLookup('name_vector', [1, 2], LookupAll)
323 results = run_search(apiobj, 0.1, [lookup], [], hnrs=['20', '21', '22'],
324 details=SearchDetails(geometry_output=geom))
327 assert all(geom.name.lower() in r.geometry for r in results)
330 def test_very_large_housenumber(apiobj):
331 apiobj.add_placex(place_id=93, class_='place', type='house',
332 parent_place_id=2000,
333 housenumber='2467463524544', country_code='pt')
334 apiobj.add_placex(place_id=2000, class_='highway', type='residential',
335 rank_search=26, rank_address=26,
337 apiobj.add_search_name(2000, names=[1,2],
338 search_rank=26, address_rank=26,
341 lookup = FieldLookup('name_vector', [1, 2], LookupAll)
343 results = run_search(apiobj, 0.1, [lookup], [], hnrs=['2467463524544'],
344 details=SearchDetails())
347 assert [r.place_id for r in results] == [93, 2000]
350 @pytest.mark.parametrize('wcount,rids', [(2, [990, 991]), (30000, [990])])
351 def test_name_and_postcode(apiobj, wcount, rids):
352 apiobj.add_placex(place_id=990, class_='highway', type='service',
353 rank_search=27, rank_address=27,
355 centroid=(10.0, 10.0),
356 geometry='LINESTRING(9.995 10, 10.005 10)')
357 apiobj.add_search_name(990, names=[111], centroid=(10.0, 10.0),
358 search_rank=27, address_rank=27)
359 apiobj.add_placex(place_id=991, class_='highway', type='service',
360 rank_search=27, rank_address=27,
362 centroid=(10.1, 10.1),
363 geometry='LINESTRING(9.995 10.1, 10.005 10.1)')
364 apiobj.add_search_name(991, names=[111], centroid=(10.1, 10.1),
365 search_rank=27, address_rank=27)
366 apiobj.add_postcode(place_id=100, country_code='ch', postcode='11225',
367 geometry='POINT(10 10)')
369 lookup = FieldLookup('name_vector', [111], LookupAll)
371 results = run_search(apiobj, 0.1, [lookup], [], pcs=['11225'], count=wcount,
372 details=SearchDetails())
375 assert [r.place_id for r in results] == rids
378 class TestInterpolations:
380 @pytest.fixture(autouse=True)
381 def fill_database(self, apiobj):
382 apiobj.add_placex(place_id=990, class_='highway', type='service',
383 rank_search=27, rank_address=27,
384 centroid=(10.0, 10.0),
385 geometry='LINESTRING(9.995 10, 10.005 10)')
386 apiobj.add_search_name(990, names=[111],
387 search_rank=27, address_rank=27)
388 apiobj.add_placex(place_id=991, class_='place', type='house',
390 rank_search=30, rank_address=30,
392 centroid=(10.0, 10.00002))
393 apiobj.add_osmline(place_id=992,
395 startnumber=21, endnumber=29, step=2,
396 centroid=(10.0, 10.00001),
397 geometry='LINESTRING(9.995 10.00001, 10.005 10.00001)')
400 @pytest.mark.parametrize('hnr,res', [('21', [992]), ('22', []), ('23', [991])])
401 def test_lookup_housenumber(self, apiobj, hnr, res):
402 lookup = FieldLookup('name_vector', [111], LookupAll)
404 results = run_search(apiobj, 0.1, [lookup], [], hnrs=[hnr])
406 assert [r.place_id for r in results] == res + [990]
409 @pytest.mark.parametrize('geom', [napi.GeometryFormat.GEOJSON,
410 napi.GeometryFormat.KML,
411 napi.GeometryFormat.SVG,
412 napi.GeometryFormat.TEXT])
413 def test_osmline_with_geometries(self, apiobj, geom):
414 lookup = FieldLookup('name_vector', [111], LookupAll)
416 results = run_search(apiobj, 0.1, [lookup], [], hnrs=['21'],
417 details=SearchDetails(geometry_output=geom))
419 assert results[0].place_id == 992
420 assert geom.name.lower() in results[0].geometry
426 @pytest.fixture(autouse=True)
427 def fill_database(self, apiobj):
428 apiobj.add_placex(place_id=990, class_='highway', type='service',
429 rank_search=27, rank_address=27,
431 centroid=(10.0, 10.0),
432 geometry='LINESTRING(9.995 10, 10.005 10)')
433 apiobj.add_search_name(990, names=[111], country_code='us',
434 search_rank=27, address_rank=27)
435 apiobj.add_placex(place_id=991, class_='place', type='house',
437 rank_search=30, rank_address=30,
440 centroid=(10.0, 10.00002))
441 apiobj.add_tiger(place_id=992,
443 startnumber=21, endnumber=29, step=2,
444 centroid=(10.0, 10.00001),
445 geometry='LINESTRING(9.995 10.00001, 10.005 10.00001)')
448 @pytest.mark.parametrize('hnr,res', [('21', [992]), ('22', []), ('23', [991])])
449 def test_lookup_housenumber(self, apiobj, hnr, res):
450 lookup = FieldLookup('name_vector', [111], LookupAll)
452 results = run_search(apiobj, 0.1, [lookup], [], hnrs=[hnr])
454 assert [r.place_id for r in results] == res + [990]
457 @pytest.mark.parametrize('geom', [napi.GeometryFormat.GEOJSON,
458 napi.GeometryFormat.KML,
459 napi.GeometryFormat.SVG,
460 napi.GeometryFormat.TEXT])
461 def test_tiger_with_geometries(self, apiobj, geom):
462 lookup = FieldLookup('name_vector', [111], LookupAll)
464 results = run_search(apiobj, 0.1, [lookup], [], hnrs=['21'],
465 details=SearchDetails(geometry_output=geom))
467 assert results[0].place_id == 992
468 assert geom.name.lower() in results[0].geometry
471 class TestLayersRank30:
473 @pytest.fixture(autouse=True)
474 def fill_database(self, apiobj):
475 apiobj.add_placex(place_id=223, class_='place', type='house',
479 apiobj.add_search_name(223, names=[34],
481 address_rank=30, search_rank=30)
482 apiobj.add_placex(place_id=224, class_='amenity', type='toilet',
485 apiobj.add_search_name(224, names=[34],
487 address_rank=30, search_rank=30)
488 apiobj.add_placex(place_id=225, class_='man_made', type='tower',
491 apiobj.add_search_name(225, names=[34],
493 address_rank=0, search_rank=30)
494 apiobj.add_placex(place_id=226, class_='railway', type='station',
497 apiobj.add_search_name(226, names=[34],
499 address_rank=0, search_rank=30)
500 apiobj.add_placex(place_id=227, class_='natural', type='cave',
503 apiobj.add_search_name(227, names=[34],
505 address_rank=0, search_rank=30)
508 @pytest.mark.parametrize('layer,res', [(napi.DataLayer.ADDRESS, [223]),
509 (napi.DataLayer.POI, [224]),
510 (napi.DataLayer.ADDRESS | napi.DataLayer.POI, [223, 224]),
511 (napi.DataLayer.MANMADE, [225]),
512 (napi.DataLayer.RAILWAY, [226]),
513 (napi.DataLayer.NATURAL, [227]),
514 (napi.DataLayer.MANMADE | napi.DataLayer.NATURAL, [225, 227]),
515 (napi.DataLayer.MANMADE | napi.DataLayer.RAILWAY, [225, 226])])
516 def test_layers_rank30(self, apiobj, layer, res):
517 lookup = FieldLookup('name_vector', [34], LookupAny)
519 results = run_search(apiobj, 0.1, [lookup], [],
520 details=SearchDetails(layers=layer))
522 assert [r.place_id for r in results] == res