1 # SPDX-License-Identifier: GPL-3.0-or-later
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2025 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Tests for lookup API call.
14 import nominatim_api as napi
17 def test_lookup_empty_list(apiobj, frontend):
18 api = frontend(apiobj, options={'details'})
19 assert api.lookup([]) == []
22 def test_lookup_non_existing(apiobj, frontend):
23 api = frontend(apiobj, options={'details'})
24 assert api.lookup((napi.PlaceID(332), napi.OsmID('W', 4),
25 napi.OsmID('W', 4, 'highway'))) == []
28 @pytest.mark.parametrize('idobj', (napi.PlaceID(332), napi.OsmID('W', 4),
29 napi.OsmID('W', 4, 'highway')))
30 def test_lookup_single_placex(apiobj, frontend, idobj):
31 apiobj.add_placex(place_id=332, osm_type='W', osm_id=4,
32 class_='highway', type='residential',
33 name={'name': 'Road'}, address={'city': 'Barrow'},
34 extratags={'surface': 'paved'},
35 parent_place_id=34, linked_place_id=55,
36 admin_level=15, country_code='gb',
38 postcode='34425', wikipedia='en:Faa',
39 rank_search=27, rank_address=26,
42 geometry='LINESTRING(23 34, 23.1 34, 23.1 34.1, 23 34)')
44 api = frontend(apiobj, options={'details'})
45 result = api.lookup([idobj])
47 assert len(result) == 1
51 assert result.source_table.name == 'PLACEX'
52 assert result.category == ('highway', 'residential')
53 assert result.centroid == (pytest.approx(23.0), pytest.approx(34.0))
55 assert result.place_id == 332
56 assert result.osm_object == ('W', 4)
58 assert result.names == {'name': 'Road'}
59 assert result.address == {'city': 'Barrow'}
60 assert result.extratags == {'surface': 'paved'}
62 assert result.housenumber == '4'
63 assert result.postcode == '34425'
64 assert result.wikipedia == 'en:Faa'
66 assert result.rank_search == 27
67 assert result.rank_address == 26
68 assert result.importance == pytest.approx(0.01)
70 assert result.country_code == 'gb'
72 assert result.address_rows is None
73 assert result.linked_rows is None
74 assert result.parented_rows is None
75 assert result.name_keywords is None
76 assert result.address_keywords is None
78 assert result.geometry == {}
81 def test_lookup_multiple_places(apiobj, frontend):
82 apiobj.add_placex(place_id=332, osm_type='W', osm_id=4,
83 class_='highway', type='residential',
84 name={'name': 'Road'}, address={'city': 'Barrow'},
85 extratags={'surface': 'paved'},
86 parent_place_id=34, linked_place_id=55,
87 admin_level=15, country_code='gb',
89 postcode='34425', wikipedia='en:Faa',
90 rank_search=27, rank_address=26,
93 geometry='LINESTRING(23 34, 23.1 34, 23.1 34.1, 23 34)')
94 apiobj.add_osmline(place_id=4924, osm_id=9928,
96 startnumber=1, endnumber=4, step=1,
97 country_code='gb', postcode='34425',
98 address={'city': 'Big'},
99 geometry='LINESTRING(23 34, 23 35)')
101 api = frontend(apiobj, options={'details'})
102 result = api.lookup((napi.OsmID('W', 1),
104 napi.OsmID('W', 9928)))
106 assert len(result) == 2
108 assert set(r.place_id for r in result) == {332, 4924}
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',
120 postcode='34425', wikipedia='en:Faa',
121 rank_search=27, rank_address=26,
124 geometry='POLYGON((23 34, 23.1 34, 23.1 34.1, 23 34))')
126 api = frontend(apiobj, options={'details'})
127 result = api.lookup([napi.OsmID('W', 4)], geometry_output=gtype)
129 assert len(result) == 1
130 assert result[0].place_id == 332
132 if gtype == napi.GeometryFormat.NONE:
133 assert list(result[0].geometry.keys()) == []
135 assert list(result[0].geometry.keys()) == [gtype.name.lower()]
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',
146 postcode='34425', wikipedia='en:Faa',
147 rank_search=27, rank_address=26,
150 geometry='POLYGON((23 34, 22.999 34, 23.1 34, 23.1 34.1, 23 34))')
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)
157 assert len(result) == 1
158 assert result[0].place_id == 332
160 geom = json.loads(result[0].geometry['geojson'])
162 assert geom['type'] == 'Polygon'
163 assert geom['coordinates'] == [[[23, 34], [23.1, 34], [23.1, 34.1], [23, 34]]]