]> git.openstreetmap.org Git - nominatim.git/blob - test/python/api/search/test_search_places.py
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / test / python / api / search / test_search_places.py
1 # SPDX-License-Identifier: GPL-3.0-or-later
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2023 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Tests for running the generic place searcher.
9 """
10 import json
11
12 import pytest
13
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
20 def run_search(apiobj, global_penalty, lookup, ranking, count=2,
21                hnrs=[], pcs=[], ccodes=[], quals=[],
22                details=SearchDetails()):
23     class MySearchData:
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))
29         lookups = lookup
30         rankings = ranking
31
32     search = PlaceSearch(0.0, MySearchData(), count)
33
34     async def run():
35         async with apiobj.api._async_api.begin() as conn:
36             return await search.lookup(conn, details)
37
38     results = apiobj.async_to_sync(run())
39     results.sort(key=lambda r: r.accuracy)
40
41     return results
42
43
44 class TestNameOnlySearches:
45
46     @pytest.fixture(autouse=True)
47     def fill_database(self, apiobj):
48         apiobj.add_placex(place_id=100, country_code='us',
49                           centroid=(5.6, 4.3))
50         apiobj.add_search_name(100, names=[1,2,10,11], country_code='us',
51                                centroid=(5.6, 4.3))
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))
56
57
58     @pytest.mark.parametrize('lookup_type', ['lookup_all', 'restrict'])
59     @pytest.mark.parametrize('rank,res', [([10], [100, 101]),
60                                           ([20], [101, 100])])
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)])
64
65         results = run_search(apiobj, 0.1, [lookup], [ranking])
66
67         assert [r.place_id for r in results] == res
68
69
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])])
74
75         results = run_search(apiobj, 0.1, [lookup], [ranking])
76
77         assert len(results) == 1
78         assert results[0].place_id == 101
79
80     @pytest.mark.parametrize('rank,res', [([10], [100, 101]),
81                                           ([20], [101, 100])])
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)])
85
86         results = run_search(apiobj, 0.1, [lookup], [ranking])
87
88         assert [r.place_id for r in results] == res
89
90
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])])
94
95         results = run_search(apiobj, 0.1, [lookup], [ranking])
96
97         assert len(results) == 1
98         assert results[0].place_id == 101
99
100
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])])
105
106         results = run_search(apiobj, 0.1, [lookup], [ranking], ccodes=[cc])
107
108         assert [r.place_id for r in results] == [res]
109
110
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])])
114
115         results = run_search(apiobj, 0.1, [lookup], [ranking],
116                              details=SearchDetails(excluded=[101]))
117
118         assert [r.place_id for r in results] == [100]
119
120
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])])
128
129         results = run_search(apiobj, 0.1, [lookup], [ranking],
130                              details=SearchDetails(geometry_output=geom))
131
132         assert geom.name.lower() in results[0].geometry
133
134
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',
138                           centroid=(9.0, 9.0),
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',
141                                centroid=(5.6, 4.3))
142
143         lookup = FieldLookup('name_vector', [55], 'lookup_all')
144         ranking = FieldRanking('name_vector', 0.9, [RankedTokens(0.0, [21])])
145
146         results = run_search(apiobj, 0.1, [lookup], [ranking],
147                              details=SearchDetails(geometry_output=napi.GeometryFormat.GEOJSON,
148                                                    geometry_simplification=factor))
149
150         assert len(results) == 1
151         result = results[0]
152         geom = json.loads(result.geometry['geojson'])
153
154         assert result.place_id == 333
155         assert len(geom['coordinates']) == npoints
156
157
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])])
163
164         results = run_search(apiobj, 0.1, [lookup], [ranking])
165         assert [r.place_id for r in results] == [101, 100]
166
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
170
171
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')
175
176         details=SearchDetails.from_kwargs({'viewbox': viewbox,
177                                            'bounded_viewbox': True})
178
179         results = run_search(apiobj, 0.1, [lookup], [], details=details)
180         assert [r.place_id for r in results] == [100]
181
182
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])])
186
187         results = run_search(apiobj, 0.1, [lookup], [ranking])
188         assert [r.place_id for r in results] == [101, 100]
189
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]
194
195
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')
199
200         details=SearchDetails.from_kwargs({'near': '5.6,4.3',
201                                            'near_radius': radius})
202
203         results = run_search(apiobj, 0.1, [lookup], [], details=details)
204
205         assert [r.place_id for r in results] == [100]
206
207
208 class TestStreetWithHousenumber:
209
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,
220                           country_code='es')
221         apiobj.add_search_name(1000, names=[1,2,10,11],
222                                search_rank=26, address_rank=26,
223                                country_code='es')
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,
235                           country_code='pt')
236         apiobj.add_search_name(2000, names=[1,2,20,21],
237                                search_rank=26, address_rank=26,
238                                country_code='pt')
239
240
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])])
247
248         results = run_search(apiobj, 0.1, [lookup], [ranking], hnrs=[hnr])
249
250         assert [r.place_id for r in results] == res + [1000, 2000]
251
252
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])])
257
258         results = run_search(apiobj, 0.1, [lookup], [ranking], hnrs=['22'],
259                              ccodes=[cc])
260
261         assert [r.place_id for r in results] == res
262
263
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])])
267
268         results = run_search(apiobj, 0.1, [lookup], [ranking], hnrs=['22'],
269                              details=SearchDetails(excluded=[92]))
270
271         assert [r.place_id for r in results] == [2, 1000, 2000]
272
273
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])])
277
278         results = run_search(apiobj, 0.1, [lookup], [ranking], hnrs=['22'],
279                              details=SearchDetails(excluded=[1000]))
280
281         assert [r.place_id for r in results] == [2, 92, 2000]
282
283
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')
290
291         results = run_search(apiobj, 0.1, [lookup], [], hnrs=['20', '21', '22'],
292                              details=SearchDetails(geometry_output=geom))
293
294         assert results
295         assert all(geom.name.lower() in r.geometry for r in results)
296
297
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,
304                       country_code='pt')
305     apiobj.add_search_name(2000, names=[1,2],
306                            search_rank=26, address_rank=26,
307                            country_code='pt')
308
309     lookup = FieldLookup('name_vector', [1, 2], 'lookup_all')
310
311     results = run_search(apiobj, 0.1, [lookup], [], hnrs=['2467463524544'],
312                          details=SearchDetails())
313
314     assert results
315     assert [r.place_id for r in results] == [93, 2000]
316
317
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,
322                       postcode='11225',
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,
329                       postcode='11221',
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)')
336
337     lookup = FieldLookup('name_vector', [111], 'lookup_all')
338
339     results = run_search(apiobj, 0.1, [lookup], [], pcs=['11225'], count=wcount,
340                          details=SearchDetails())
341
342     assert results
343     assert [r.place_id for r in results] == rids
344
345
346 class TestInterpolations:
347
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',
357                           parent_place_id=990,
358                           rank_search=30, rank_address=30,
359                           housenumber='23',
360                           centroid=(10.0, 10.00002))
361         apiobj.add_osmline(place_id=992,
362                            parent_place_id=990,
363                            startnumber=21, endnumber=29, step=2,
364                            centroid=(10.0, 10.00001),
365                            geometry='LINESTRING(9.995 10.00001, 10.005 10.00001)')
366
367
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')
371
372         results = run_search(apiobj, 0.1, [lookup], [], hnrs=[hnr])
373
374         assert [r.place_id for r in results] == res + [990]
375
376
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')
383
384         results = run_search(apiobj, 0.1, [lookup], [], hnrs=['21'],
385                              details=SearchDetails(geometry_output=geom))
386
387         assert results[0].place_id == 992
388         assert geom.name.lower() in results[0].geometry
389
390
391
392 class TestTiger:
393
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,
398                           country_code='us',
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',
404                           parent_place_id=990,
405                           rank_search=30, rank_address=30,
406                           housenumber='23',
407                           country_code='us',
408                           centroid=(10.0, 10.00002))
409         apiobj.add_tiger(place_id=992,
410                          parent_place_id=990,
411                          startnumber=21, endnumber=29, step=2,
412                          centroid=(10.0, 10.00001),
413                          geometry='LINESTRING(9.995 10.00001, 10.005 10.00001)')
414
415
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')
419
420         results = run_search(apiobj, 0.1, [lookup], [], hnrs=[hnr])
421
422         assert [r.place_id for r in results] == res + [990]
423
424
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')
431
432         results = run_search(apiobj, 0.1, [lookup], [], hnrs=['21'],
433                              details=SearchDetails(geometry_output=geom))
434
435         assert results[0].place_id == 992
436         assert geom.name.lower() in results[0].geometry
437
438
439 class TestLayersRank30:
440
441     @pytest.fixture(autouse=True)
442     def fill_database(self, apiobj):
443         apiobj.add_placex(place_id=223, class_='place', type='house',
444                           housenumber='1',
445                           rank_address=30,
446                           rank_search=30)
447         apiobj.add_search_name(223, names=[34],
448                                importance=0.0009,
449                                address_rank=30, search_rank=30)
450         apiobj.add_placex(place_id=224, class_='amenity', type='toilet',
451                           rank_address=30,
452                           rank_search=30)
453         apiobj.add_search_name(224, names=[34],
454                                importance=0.0008,
455                                address_rank=30, search_rank=30)
456         apiobj.add_placex(place_id=225, class_='man_made', type='tower',
457                           rank_address=0,
458                           rank_search=30)
459         apiobj.add_search_name(225, names=[34],
460                                importance=0.0007,
461                                address_rank=0, search_rank=30)
462         apiobj.add_placex(place_id=226, class_='railway', type='station',
463                           rank_address=0,
464                           rank_search=30)
465         apiobj.add_search_name(226, names=[34],
466                                importance=0.0006,
467                                address_rank=0, search_rank=30)
468         apiobj.add_placex(place_id=227, class_='natural', type='cave',
469                           rank_address=0,
470                           rank_search=30)
471         apiobj.add_search_name(227, names=[34],
472                                importance=0.0005,
473                                address_rank=0, search_rank=30)
474
475
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')
486
487         results = run_search(apiobj, 0.1, [lookup], [],
488                              details=SearchDetails(layers=layer))
489
490         assert [r.place_id for r in results] == res