1 # SPDX-License-Identifier: GPL-3.0-or-later
 
   3 # This file is part of Nominatim. (https://nominatim.org)
 
   5 # Copyright (C) 2024 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
 
  16 def test_lookup_empty_list(apiobj, frontend):
 
  17     api = frontend(apiobj, options={'details'})
 
  18     assert api.lookup([]) == []
 
  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'))) == []
 
  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',
 
  37                      postcode='34425', wikipedia='en:Faa',
 
  38                      rank_search=27, rank_address=26,
 
  41                      geometry='LINESTRING(23 34, 23.1 34, 23.1 34.1, 23 34)')
 
  43     api = frontend(apiobj, options={'details'})
 
  44     result = api.lookup([idobj])
 
  46     assert len(result) == 1
 
  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))
 
  54     assert result.place_id == 332
 
  55     assert result.osm_object == ('W', 4)
 
  57     assert result.names == {'name': 'Road'}
 
  58     assert result.address == {'city': 'Barrow'}
 
  59     assert result.extratags == {'surface': 'paved'}
 
  61     assert result.housenumber == '4'
 
  62     assert result.postcode == '34425'
 
  63     assert result.wikipedia == 'en:Faa'
 
  65     assert result.rank_search == 27
 
  66     assert result.rank_address == 26
 
  67     assert result.importance == pytest.approx(0.01)
 
  69     assert result.country_code == 'gb'
 
  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
 
  77     assert result.geometry == {}
 
  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',
 
  88                      postcode='34425', wikipedia='en:Faa',
 
  89                      rank_search=27, rank_address=26,
 
  92                      geometry='LINESTRING(23 34, 23.1 34, 23.1 34.1, 23 34)')
 
  93     apiobj.add_osmline(place_id=4924, osm_id=9928,
 
  95                        startnumber=1, endnumber=4, step=1,
 
  96                        country_code='gb', postcode='34425',
 
  97                        address={'city': 'Big'},
 
  98                        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]]]