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 Complex datatypes used by the Nominatim API.
10 from typing import Optional, Union, NamedTuple
13 from struct import unpack
15 @dataclasses.dataclass
17 """ Reference an object by Nominatim's internal ID.
22 @dataclasses.dataclass
24 """ Reference by the OSM ID and potentially the basic category.
28 osm_class: Optional[str] = None
30 def __post_init__(self) -> None:
31 if self.osm_type not in ('N', 'W', 'R'):
32 raise ValueError(f"Illegal OSM type '{self.osm_type}'. Must be one of N, W, R.")
35 PlaceRef = Union[PlaceID, OsmID]
38 class Point(NamedTuple):
39 """ A geographic point in WGS84 projection.
46 def lat(self) -> float:
47 """ Return the latitude of the point.
53 def lon(self) -> float:
54 """ Return the longitude of the point.
59 def to_geojson(self) -> str:
60 """ Return the point in GeoJSON format.
62 return f'{{"type": "Point","coordinates": [{self.x}, {self.y}]}}'
66 def from_wkb(wkb: bytes) -> 'Point':
67 """ Create a point from EWKB as returned from the database.
70 raise ValueError("Point wkb has unexpected length")
72 gtype, srid, x, y = unpack('>iidd', wkb[1:])
74 gtype, srid, x, y = unpack('<iidd', wkb[1:])
76 raise ValueError("WKB has unknown endian value.")
78 if gtype != 0x20000001:
79 raise ValueError("WKB must be a point geometry.")
81 raise ValueError("Only WGS84 WKB supported.")
86 class GeometryFormat(enum.Flag):
87 """ Geometry output formats supported by Nominatim.
96 @dataclasses.dataclass
98 """ Collection of parameters that define the amount of details
99 returned with a search result.
101 geometry_output: GeometryFormat = GeometryFormat.NONE
102 """ Add the full geometry of the place to the result. Multiple
103 formats may be selected. Note that geometries can become quite large.
105 address_details: bool = False
106 """ Get detailed information on the places that make up the address
109 linked_places: bool = False
110 """ Get detailed information on the places that link to the result.
112 parented_places: bool = False
113 """ Get detailed information on all places that this place is a parent
114 for, i.e. all places for which it provides the address details.
115 Only POI places can have parents.
117 keywords: bool = False
118 """ Add information about the search terms used for this place.