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.9, [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 @pytest.mark.parametrize('geom', [napi.GeometryFormat.GEOJSON,
285 napi.GeometryFormat.KML,
286 napi.GeometryFormat.SVG,
287 napi.GeometryFormat.TEXT])
288 def test_return_geometries(self, apiobj, geom):
289 lookup = FieldLookup('name_vector', [1, 2], 'lookup_all')
291 results = run_search(apiobj, 0.1, [lookup], [], hnrs=['20', '21', '22'],
292 details=SearchDetails(geometry_output=geom))
295 assert all(geom.name.lower() in r.geometry for r in results)
298 def test_very_large_housenumber(apiobj):
299 apiobj.add_placex(place_id=93, class_='place', type='house',
300 parent_place_id=2000,
301 housenumber='2467463524544', country_code='pt')
302 apiobj.add_placex(place_id=2000, class_='highway', type='residential',
303 rank_search=26, rank_address=26,
305 apiobj.add_search_name(2000, names=[1,2],
306 search_rank=26, address_rank=26,
309 lookup = FieldLookup('name_vector', [1, 2], 'lookup_all')
311 results = run_search(apiobj, 0.1, [lookup], [], hnrs=['2467463524544'],
312 details=SearchDetails())
315 assert [r.place_id for r in results] == [93, 2000]
318 @pytest.mark.parametrize('wcount,rids', [(2, [990, 991]), (30000, [990])])
319 def test_name_and_postcode(apiobj, wcount, rids):
320 apiobj.add_placex(place_id=990, class_='highway', type='service',
321 rank_search=27, rank_address=27,
323 centroid=(10.0, 10.0),
324 geometry='LINESTRING(9.995 10, 10.005 10)')
325 apiobj.add_search_name(990, names=[111], centroid=(10.0, 10.0),
326 search_rank=27, address_rank=27)
327 apiobj.add_placex(place_id=991, class_='highway', type='service',
328 rank_search=27, rank_address=27,
330 centroid=(10.1, 10.1),
331 geometry='LINESTRING(9.995 10.1, 10.005 10.1)')
332 apiobj.add_search_name(991, names=[111], centroid=(10.1, 10.1),
333 search_rank=27, address_rank=27)
334 apiobj.add_postcode(place_id=100, country_code='ch', postcode='11225',
335 geometry='POINT(10 10)')
337 lookup = FieldLookup('name_vector', [111], 'lookup_all')
339 results = run_search(apiobj, 0.1, [lookup], [], pcs=['11225'], count=wcount,
340 details=SearchDetails())
343 assert [r.place_id for r in results] == rids
346 class TestInterpolations:
348 @pytest.fixture(autouse=True)
349 def fill_database(self, apiobj):
350 apiobj.add_placex(place_id=990, class_='highway', type='service',
351 rank_search=27, rank_address=27,
352 centroid=(10.0, 10.0),
353 geometry='LINESTRING(9.995 10, 10.005 10)')
354 apiobj.add_search_name(990, names=[111],
355 search_rank=27, address_rank=27)
356 apiobj.add_placex(place_id=991, class_='place', type='house',
358 rank_search=30, rank_address=30,
360 centroid=(10.0, 10.00002))
361 apiobj.add_osmline(place_id=992,
363 startnumber=21, endnumber=29, step=2,
364 centroid=(10.0, 10.00001),
365 geometry='LINESTRING(9.995 10.00001, 10.005 10.00001)')
368 @pytest.mark.parametrize('hnr,res', [('21', [992]), ('22', []), ('23', [991])])
369 def test_lookup_housenumber(self, apiobj, hnr, res):
370 lookup = FieldLookup('name_vector', [111], 'lookup_all')
372 results = run_search(apiobj, 0.1, [lookup], [], hnrs=[hnr])
374 assert [r.place_id for r in results] == res + [990]
377 @pytest.mark.parametrize('geom', [napi.GeometryFormat.GEOJSON,
378 napi.GeometryFormat.KML,
379 napi.GeometryFormat.SVG,
380 napi.GeometryFormat.TEXT])
381 def test_osmline_with_geometries(self, apiobj, geom):
382 lookup = FieldLookup('name_vector', [111], 'lookup_all')
384 results = run_search(apiobj, 0.1, [lookup], [], hnrs=['21'],
385 details=SearchDetails(geometry_output=geom))
387 assert results[0].place_id == 992
388 assert geom.name.lower() in results[0].geometry
394 @pytest.fixture(autouse=True)
395 def fill_database(self, apiobj):
396 apiobj.add_placex(place_id=990, class_='highway', type='service',
397 rank_search=27, rank_address=27,
399 centroid=(10.0, 10.0),
400 geometry='LINESTRING(9.995 10, 10.005 10)')
401 apiobj.add_search_name(990, names=[111], country_code='us',
402 search_rank=27, address_rank=27)
403 apiobj.add_placex(place_id=991, class_='place', type='house',
405 rank_search=30, rank_address=30,
408 centroid=(10.0, 10.00002))
409 apiobj.add_tiger(place_id=992,
411 startnumber=21, endnumber=29, step=2,
412 centroid=(10.0, 10.00001),
413 geometry='LINESTRING(9.995 10.00001, 10.005 10.00001)')
416 @pytest.mark.parametrize('hnr,res', [('21', [992]), ('22', []), ('23', [991])])
417 def test_lookup_housenumber(self, apiobj, hnr, res):
418 lookup = FieldLookup('name_vector', [111], 'lookup_all')
420 results = run_search(apiobj, 0.1, [lookup], [], hnrs=[hnr])
422 assert [r.place_id for r in results] == res + [990]
425 @pytest.mark.parametrize('geom', [napi.GeometryFormat.GEOJSON,
426 napi.GeometryFormat.KML,
427 napi.GeometryFormat.SVG,
428 napi.GeometryFormat.TEXT])
429 def test_tiger_with_geometries(self, apiobj, geom):
430 lookup = FieldLookup('name_vector', [111], 'lookup_all')
432 results = run_search(apiobj, 0.1, [lookup], [], hnrs=['21'],
433 details=SearchDetails(geometry_output=geom))
435 assert results[0].place_id == 992
436 assert geom.name.lower() in results[0].geometry
439 class TestLayersRank30:
441 @pytest.fixture(autouse=True)
442 def fill_database(self, apiobj):
443 apiobj.add_placex(place_id=223, class_='place', type='house',
447 apiobj.add_search_name(223, names=[34],
449 address_rank=30, search_rank=30)
450 apiobj.add_placex(place_id=224, class_='amenity', type='toilet',
453 apiobj.add_search_name(224, names=[34],
455 address_rank=30, search_rank=30)
456 apiobj.add_placex(place_id=225, class_='man_made', type='tower',
459 apiobj.add_search_name(225, names=[34],
461 address_rank=0, search_rank=30)
462 apiobj.add_placex(place_id=226, class_='railway', type='station',
465 apiobj.add_search_name(226, names=[34],
467 address_rank=0, search_rank=30)
468 apiobj.add_placex(place_id=227, class_='natural', type='cave',
471 apiobj.add_search_name(227, names=[34],
473 address_rank=0, search_rank=30)
476 @pytest.mark.parametrize('layer,res', [(napi.DataLayer.ADDRESS, [223]),
477 (napi.DataLayer.POI, [224]),
478 (napi.DataLayer.ADDRESS | napi.DataLayer.POI, [223, 224]),
479 (napi.DataLayer.MANMADE, [225]),
480 (napi.DataLayer.RAILWAY, [226]),
481 (napi.DataLayer.NATURAL, [227]),
482 (napi.DataLayer.MANMADE | napi.DataLayer.NATURAL, [225, 227]),
483 (napi.DataLayer.MANMADE | napi.DataLayer.RAILWAY, [225, 226])])
484 def test_layers_rank30(self, apiobj, layer, res):
485 lookup = FieldLookup('name_vector', [34], 'lookup_any')
487 results = run_search(apiobj, 0.1, [lookup], [],
488 details=SearchDetails(layers=layer))
490 assert [r.place_id for r in results] == res