X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/0c65289a80fb376b9d7e53653e16cef6a8fa781e..5184a8aff9686c4035e236be65c3615fe97859a0:/nominatim/api/types.py diff --git a/nominatim/api/types.py b/nominatim/api/types.py index 89b81111..9dc3ff2e 100644 --- a/nominatim/api/types.py +++ b/nominatim/api/types.py @@ -10,6 +10,7 @@ Complex datatypes used by the Nominatim API. from typing import Optional, Union, NamedTuple import dataclasses import enum +from struct import unpack @dataclasses.dataclass class PlaceID: @@ -55,6 +56,33 @@ class Point(NamedTuple): return self.x + def to_geojson(self) -> str: + """ Return the point in GeoJSON format. + """ + return f'{{"type": "Point","coordinates": [{self.x}, {self.y}]}}' + + + @staticmethod + def from_wkb(wkb: bytes) -> 'Point': + """ Create a point from EWKB as returned from the database. + """ + if len(wkb) != 25: + raise ValueError("Point wkb has unexpected length") + if wkb[0] == 0: + gtype, srid, x, y = unpack('>iidd', wkb[1:]) + elif wkb[0] == 1: + gtype, srid, x, y = unpack('