]> git.openstreetmap.org Git - nominatim.git/blob - test/python/api/test_api_lookup.py
use custom result formatters in CLI commands
[nominatim.git] / test / python / api / test_api_lookup.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) 2024 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Tests for lookup API call.
9 """
10 import json
11
12 import pytest
13
14 import nominatim_api as napi
15
16 def test_lookup_empty_list(apiobj, frontend):
17     api = frontend(apiobj, options={'details'})
18     assert api.lookup([]) == []
19
20
21 def test_lookup_non_existing(apiobj, frontend):
22     api = frontend(apiobj, options={'details'})
23     assert api.lookup((napi.PlaceID(332), napi.OsmID('W', 4),
24                        napi.OsmID('W', 4, 'highway'))) == []
25
26
27 @pytest.mark.parametrize('idobj', (napi.PlaceID(332), napi.OsmID('W', 4),
28                                    napi.OsmID('W', 4, 'highway')))
29 def test_lookup_single_placex(apiobj, frontend, idobj):
30     apiobj.add_placex(place_id=332, osm_type='W', osm_id=4,
31                      class_='highway', type='residential',
32                      name={'name': 'Road'}, address={'city': 'Barrow'},
33                      extratags={'surface': 'paved'},
34                      parent_place_id=34, linked_place_id=55,
35                      admin_level=15, country_code='gb',
36                      housenumber='4',
37                      postcode='34425', wikipedia='en:Faa',
38                      rank_search=27, rank_address=26,
39                      importance=0.01,
40                      centroid=(23, 34),
41                      geometry='LINESTRING(23 34, 23.1 34, 23.1 34.1, 23 34)')
42
43     api = frontend(apiobj, options={'details'})
44     result = api.lookup([idobj])
45
46     assert len(result) == 1
47
48     result = result[0]
49
50     assert result.source_table.name == 'PLACEX'
51     assert result.category == ('highway', 'residential')
52     assert result.centroid == (pytest.approx(23.0), pytest.approx(34.0))
53
54     assert result.place_id == 332
55     assert result.osm_object == ('W', 4)
56
57     assert result.names == {'name': 'Road'}
58     assert result.address == {'city': 'Barrow'}
59     assert result.extratags == {'surface': 'paved'}
60
61     assert result.housenumber == '4'
62     assert result.postcode == '34425'
63     assert result.wikipedia == 'en:Faa'
64
65     assert result.rank_search == 27
66     assert result.rank_address == 26
67     assert result.importance == pytest.approx(0.01)
68
69     assert result.country_code == 'gb'
70
71     assert result.address_rows is None
72     assert result.linked_rows is None
73     assert result.parented_rows is None
74     assert result.name_keywords is None
75     assert result.address_keywords is None
76
77     assert result.geometry == {}
78
79
80 def test_lookup_multiple_places(apiobj, frontend):
81     apiobj.add_placex(place_id=332, osm_type='W', osm_id=4,
82                      class_='highway', type='residential',
83                      name={'name': 'Road'}, address={'city': 'Barrow'},
84                      extratags={'surface': 'paved'},
85                      parent_place_id=34, linked_place_id=55,
86                      admin_level=15, country_code='gb',
87                      housenumber='4',
88                      postcode='34425', wikipedia='en:Faa',
89                      rank_search=27, rank_address=26,
90                      importance=0.01,
91                      centroid=(23, 34),
92                      geometry='LINESTRING(23 34, 23.1 34, 23.1 34.1, 23 34)')
93     apiobj.add_osmline(place_id=4924, osm_id=9928,
94                        parent_place_id=12,
95                        startnumber=1, endnumber=4, step=1,
96                        country_code='gb', postcode='34425',
97                        address={'city': 'Big'},
98                        geometry='LINESTRING(23 34, 23 35)')
99
100
101     api = frontend(apiobj, options={'details'})
102     result = api.lookup((napi.OsmID('W', 1),
103                          napi.OsmID('W', 4),
104                          napi.OsmID('W', 9928)))
105
106     assert len(result) == 2
107
108     assert set(r.place_id for r in result) == {332, 4924}
109
110
111 @pytest.mark.parametrize('gtype', list(napi.GeometryFormat))
112 def test_simple_place_with_geometry(apiobj, frontend, gtype):
113     apiobj.add_placex(place_id=332, osm_type='W', osm_id=4,
114                      class_='highway', type='residential',
115                      name={'name': 'Road'}, address={'city': 'Barrow'},
116                      extratags={'surface': 'paved'},
117                      parent_place_id=34, linked_place_id=55,
118                      admin_level=15, country_code='gb',
119                      housenumber='4',
120                      postcode='34425', wikipedia='en:Faa',
121                      rank_search=27, rank_address=26,
122                      importance=0.01,
123                      centroid=(23, 34),
124                      geometry='POLYGON((23 34, 23.1 34, 23.1 34.1, 23 34))')
125
126     api = frontend(apiobj, options={'details'})
127     result = api.lookup([napi.OsmID('W', 4)], geometry_output=gtype)
128
129     assert len(result) == 1
130     assert result[0].place_id == 332
131
132     if gtype == napi.GeometryFormat.NONE:
133         assert list(result[0].geometry.keys()) == []
134     else:
135         assert list(result[0].geometry.keys()) == [gtype.name.lower()]
136
137
138 def test_simple_place_with_geometry_simplified(apiobj, frontend):
139     apiobj.add_placex(place_id=332, osm_type='W', osm_id=4,
140                      class_='highway', type='residential',
141                      name={'name': 'Road'}, address={'city': 'Barrow'},
142                      extratags={'surface': 'paved'},
143                      parent_place_id=34, linked_place_id=55,
144                      admin_level=15, country_code='gb',
145                      housenumber='4',
146                      postcode='34425', wikipedia='en:Faa',
147                      rank_search=27, rank_address=26,
148                      importance=0.01,
149                      centroid=(23, 34),
150                      geometry='POLYGON((23 34, 22.999 34, 23.1 34, 23.1 34.1, 23 34))')
151
152     api = frontend(apiobj, options={'details'})
153     result = api.lookup([napi.OsmID('W', 4)],
154                         geometry_output=napi.GeometryFormat.GEOJSON,
155                         geometry_simplification=0.1)
156
157     assert len(result) == 1
158     assert result[0].place_id == 332
159
160     geom = json.loads(result[0].geometry['geojson'])
161
162     assert geom['type']  == 'Polygon'
163     assert geom['coordinates'] == [[[23, 34], [23.1, 34], [23.1, 34.1], [23, 34]]]