diff --git a/pymeos/boxes/stbox.py b/pymeos/boxes/stbox.py index 1a4dd2bc..334d5dd4 100644 --- a/pymeos/boxes/stbox.py +++ b/pymeos/boxes/stbox.py @@ -288,8 +288,94 @@ def from_expanding_bounding_box( raise TypeError(f"Operation not supported with type {value.__class__}") return STBox(_inner=stbox_expand_space(box, expansion)) + @staticmethod + def from_text(text: str) -> STBox: + """ + Returns a :class:`STBox` from its text, the inverse of :meth:`as_text`. + + Args: + text: The text representation. + + Returns: + A new :class:`STBox` instance. + + MEOS Functions: + stbox_in + """ + return STBox(string=text) + + @staticmethod + def from_ewkb(ewkb: bytes) -> STBox: + """ + Returns a :class:`STBox` from its EWKB, the inverse of :meth:`as_ewkb`. + + Args: + ewkb: The EWKB representation. + + Returns: + A new :class:`STBox` instance. + + MEOS Functions: + stbox_from_wkb + """ + return STBox(_inner=stbox_from_wkb(ewkb)) + + @staticmethod + def from_hexewkb(hexewkb: str) -> STBox: + """ + Returns a :class:`STBox` from its HEXEWKB, the inverse of :meth:`as_hexewkb`. + + Args: + hexewkb: The HEXEWKB representation. + + Returns: + A new :class:`STBox` instance. + + MEOS Functions: + stbox_from_hexwkb + """ + return STBox(_inner=stbox_from_hexwkb(hexewkb)) + + def as_ewkb(self, endian: str = "") -> bytes: + """ + Returns the EWKB representation of ``self``, carrying its SRID. + + Args: + endian: The byte order, ``"NDR"``, ``"XDR"`` or ``""`` for the + machine's own. + + Returns: + A :class:`bytes` instance. + + MEOS Functions: + stbox_as_ewkb, wkb_variant_from_endian + """ + return stbox_as_ewkb(self._inner, wkb_variant_from_endian(endian)) + + def as_hexewkb(self, endian: str = "") -> str: + """ + Returns the HEXEWKB representation of ``self``, carrying its SRID. + + Args: + endian: The byte order, ``"NDR"``, ``"XDR"`` or ``""`` for the + machine's own. + + Returns: + A :class:`str` instance. + + MEOS Functions: + stbox_as_hexewkb, wkb_variant_from_endian + """ + return stbox_as_hexewkb(self._inner, wkb_variant_from_endian(endian))[0] + # ------------------------- Output ---------------------------------------- - def __str__(self, max_decimals: int = 15): + def __str__(self) -> str: + """ + Returns the text representation of ``self``, its :meth:`as_text`. + """ + return self.as_text() + + def as_text(self, max_decimals: int = 15) -> str: """ Returns a string representation of ``self``. @@ -313,30 +399,38 @@ def __repr__(self): """ return f"{self.__class__.__name__}({self})" - def as_wkb(self) -> bytes: + def as_wkb(self, endian: str = "") -> bytes: """ Returns the WKB representation of ``self``. + Args: + endian: The byte order, ``"NDR"``, ``"XDR"`` or ``""`` for the + machine's own. + Returns: A :class:`str` object with the WKB representation of ``self``. MEOS Functions: - stbox_as_wkb + stbox_as_wkb, wkb_variant_from_endian """ - return stbox_as_wkb(self._inner, 4) + return stbox_as_wkb(self._inner, wkb_variant_from_endian(endian)) - def as_hexwkb(self) -> str: + def as_hexwkb(self, endian: str = "") -> str: """ Returns the WKB representation of ``self`` in hex-encoded ASCII. + Args: + endian: The byte order, ``"NDR"``, ``"XDR"`` or ``""`` for the + machine's own. + Returns: A :class:`str` object with the WKB representation of ``self`` in hex-encoded ASCII. MEOS Functions: - stbox_as_hexwkb + stbox_as_hexwkb, wkb_variant_from_endian """ - return stbox_as_hexwkb(self._inner, -1)[0] + return stbox_as_hexwkb(self._inner, wkb_variant_from_endian(endian))[0] # ------------------------- Conversions ---------------------------------- def to_geometry(self, precision: int = 15) -> shp.BaseGeometry: diff --git a/pymeos/boxes/tbox.py b/pymeos/boxes/tbox.py index 734b462f..d10d971e 100644 --- a/pymeos/boxes/tbox.py +++ b/pymeos/boxes/tbox.py @@ -237,8 +237,30 @@ def from_tnumber(temporal: TNumber) -> TBox: """ return TBox(_inner=tnumber_to_tbox(temporal._inner)) + @staticmethod + def from_text(text: str) -> TBox: + """ + Returns a :class:`TBox` from its text, the inverse of :meth:`as_text`. + + Args: + text: The text representation. + + Returns: + A new :class:`TBox` instance. + + MEOS Functions: + tbox_in + """ + return TBox(string=text) + # ------------------------- Output ---------------------------------------- - def __str__(self, max_decimals: int = 15): + def __str__(self) -> str: + """ + Returns the text representation of ``self``, its :meth:`as_text`. + """ + return self.as_text() + + def as_text(self, max_decimals: int = 15) -> str: """ Returns a string representation of ``self``. @@ -262,30 +284,38 @@ def __repr__(self): """ return f"{self.__class__.__name__}({self})" - def as_wkb(self) -> bytes: + def as_wkb(self, endian: str = "") -> bytes: """ Returns the WKB representation of ``self``. + Args: + endian: The byte order, ``"NDR"``, ``"XDR"`` or ``""`` for the + machine's own. + Returns: A :class:`str` object with the WKB representation of ``self``. MEOS Functions: - tbox_as_wkb + tbox_as_wkb, wkb_variant_from_endian """ - return tbox_as_wkb(self._inner, 4) + return tbox_as_wkb(self._inner, wkb_variant_from_endian(endian)) - def as_hexwkb(self) -> str: + def as_hexwkb(self, endian: str = "") -> str: """ Returns the WKB representation of ``self`` in hex-encoded ASCII. + Args: + endian: The byte order, ``"NDR"``, ``"XDR"`` or ``""`` for the + machine's own. + Returns: A :class:`str` object with the WKB representation of ``self`` in hex-encoded ASCII. MEOS Functions: - tbox_as_hexwkb + tbox_as_hexwkb, wkb_variant_from_endian """ - return tbox_as_hexwkb(self._inner, -1)[0] + return tbox_as_hexwkb(self._inner, wkb_variant_from_endian(endian))[0] # ------------------------- Conversions ---------------------------------- def to_floatspan(self) -> FloatSpan: diff --git a/pymeos/collections/base/set.py b/pymeos/collections/base/set.py index 50391327..5568f536 100644 --- a/pymeos/collections/base/set.py +++ b/pymeos/collections/base/set.py @@ -96,6 +96,22 @@ def from_hexwkb(cls: type[Self], hexwkb: str) -> Self: return _CollectionFactory.create_collection(set_from_hexwkb(hexwkb)) + @classmethod + def from_text(cls: type[Self], text: str) -> Self: + """ + Returns a :class:`Set` from its text, the inverse of :meth:`as_text`. + + Args: + text: The text representation. + + Returns: + A new :class:`Set` instance. + + MEOS Functions: + _in + """ + return cls(string=text) + # ------------------------- Output ---------------------------------------- @abstractmethod def __str__(self) -> str: @@ -119,27 +135,35 @@ def __repr__(self) -> str: """ return f"{self.__class__.__name__}({self})" - def as_wkb(self) -> bytes: + def as_wkb(self, endian: str = "") -> bytes: """ Returns the WKB representation of ``self``. + Args: + endian: The byte order, ``"NDR"``, ``"XDR"`` or ``""`` for the + machine's own. + Returns: A :class:`str` object with the WKB representation of ``self``. MEOS Functions: - set_as_wkb + set_as_wkb, wkb_variant_from_endian """ - return set_as_wkb(self._inner, 4) + return set_as_wkb(self._inner, wkb_variant_from_endian(endian)) - def as_hexwkb(self) -> str: + def as_hexwkb(self, endian: str = "") -> str: """ Returns the WKB representation of ``self`` in hex-encoded ASCII. + Args: + endian: The byte order, ``"NDR"``, ``"XDR"`` or ``""`` for the + machine's own. + Returns: A :class:`str` object with the WKB representation of ``self`` in hex-encoded ASCII. MEOS Functions: - set_as_hexwkb + set_as_hexwkb, wkb_variant_from_endian """ - return set_as_hexwkb(self._inner, -1)[0] + return set_as_hexwkb(self._inner, wkb_variant_from_endian(endian))[0] # ------------------------- Conversions ----------------------------------- @overload diff --git a/pymeos/collections/base/span.py b/pymeos/collections/base/span.py index 23f1cbb4..55ffdc00 100644 --- a/pymeos/collections/base/span.py +++ b/pymeos/collections/base/span.py @@ -96,6 +96,22 @@ def from_hexwkb(cls: type[Self], hexwkb: str) -> Self: result = span_from_hexwkb(hexwkb) return cls(_inner=result) + @classmethod + def from_text(cls: type[Self], text: str) -> Self: + """ + Returns a :class:`Span` from its text, the inverse of :meth:`as_text`. + + Args: + text: The text representation. + + Returns: + A new :class:`Span` instance. + + MEOS Functions: + _in + """ + return cls(string=text) + # ------------------------- Output ---------------------------------------- @abstractmethod def __str__(self) -> str: @@ -116,30 +132,38 @@ def __repr__(self): """ return f"{self.__class__.__name__}({self})" - def as_wkb(self) -> bytes: + def as_wkb(self, endian: str = "") -> bytes: """ Returns the WKB representation of ``self``. + Args: + endian: The byte order, ``"NDR"``, ``"XDR"`` or ``""`` for the + machine's own. + Returns: A :class:`str` object with the WKB representation of ``self``. MEOS Functions: - span_as_wkb + span_as_wkb, wkb_variant_from_endian """ - return span_as_wkb(self._inner, 4) + return span_as_wkb(self._inner, wkb_variant_from_endian(endian)) - def as_hexwkb(self) -> str: + def as_hexwkb(self, endian: str = "") -> str: """ Returns the WKB representation of ``self`` in hex-encoded ASCII. + Args: + endian: The byte order, ``"NDR"``, ``"XDR"`` or ``""`` for the + machine's own. + Returns: A :class:`str` object with the WKB representation of ``self`` in hex-encoded ASCII. MEOS Functions: - span_as_hexwkb + span_as_hexwkb, wkb_variant_from_endian """ - return span_as_hexwkb(self._inner, -1)[0] + return span_as_hexwkb(self._inner, wkb_variant_from_endian(endian))[0] # ------------------------- Conversions ----------------------------------- @abstractmethod diff --git a/pymeos/collections/base/spanset.py b/pymeos/collections/base/spanset.py index 9b63c0d0..39dcb096 100644 --- a/pymeos/collections/base/spanset.py +++ b/pymeos/collections/base/spanset.py @@ -92,6 +92,22 @@ def from_hexwkb(cls: type[Self], hexwkb: str) -> Self: result = spanset_from_hexwkb(hexwkb) return cls(_inner=result) + @classmethod + def from_text(cls: type[Self], text: str) -> Self: + """ + Returns a :class:`SpanSet` from its text, the inverse of :meth:`as_text`. + + Args: + text: The text representation. + + Returns: + A new :class:`SpanSet` instance. + + MEOS Functions: + _in + """ + return cls(string=text) + # ------------------------- Output ---------------------------------------- @abstractmethod def __str__(self): @@ -115,28 +131,36 @@ def __repr__(self): """ return f"{self.__class__.__name__}({self})" - def as_wkb(self) -> bytes: + def as_wkb(self, endian: str = "") -> bytes: """ Returns the WKB representation of ``self``. + Args: + endian: The byte order, ``"NDR"``, ``"XDR"`` or ``""`` for the + machine's own. + Returns: A :class:`str` object with the WKB representation of ``self``. MEOS Functions: - spanset_as_wkb + spanset_as_wkb, wkb_variant_from_endian """ - return spanset_as_wkb(self._inner, 4) + return spanset_as_wkb(self._inner, wkb_variant_from_endian(endian)) - def as_hexwkb(self) -> str: + def as_hexwkb(self, endian: str = "") -> str: """ Returns the WKB representation of ``self`` in hex-encoded ASCII. + Args: + endian: The byte order, ``"NDR"``, ``"XDR"`` or ``""`` for the + machine's own. + Returns: A :class:`str` object with the WKB representation of ``self`` in hex-encoded ASCII. MEOS Functions: - spanset_as_hexwkb + spanset_as_hexwkb, wkb_variant_from_endian """ - return spanset_as_hexwkb(self._inner, -1)[0] + return spanset_as_hexwkb(self._inner, wkb_variant_from_endian(endian))[0] # ------------------------- Conversions ----------------------------------- @abstractmethod diff --git a/pymeos/collections/cbuffer/cbuffer.py b/pymeos/collections/cbuffer/cbuffer.py index 234e23ee..3d549e25 100644 --- a/pymeos/collections/cbuffer/cbuffer.py +++ b/pymeos/collections/cbuffer/cbuffer.py @@ -170,30 +170,134 @@ def as_ewkt(self, max_decimals: int = 15) -> str: """ return cbuffer_as_ewkt(self._inner, max_decimals) - def as_wkb(self) -> bytes: + def as_wkb(self, endian: str = "") -> bytes: """ Returns the WKB representation of ``self``. + Args: + endian: The byte order, ``"NDR"``, ``"XDR"`` or ``""`` for the + machine's own. + Returns: A :class:`bytes` object with the WKB representation of ``self``. MEOS Functions: - cbuffer_as_wkb + cbuffer_as_wkb, wkb_variant_from_endian """ - return cbuffer_as_wkb(self._inner, 4) + return cbuffer_as_wkb(self._inner, wkb_variant_from_endian(endian)) - def as_hexwkb(self) -> str: + def as_hexwkb(self, endian: str = "") -> str: """ Returns the WKB representation of ``self`` in hex-encoded ASCII. + Args: + endian: The byte order, ``"NDR"``, ``"XDR"`` or ``""`` for the + machine's own. + Returns: A :class:`str` object with the WKB representation of ``self`` in hex-encoded ASCII. MEOS Functions: - cbuffer_as_hexwkb + cbuffer_as_hexwkb, wkb_variant_from_endian + """ + return cbuffer_as_hexwkb(self._inner, wkb_variant_from_endian(endian))[0] + + @staticmethod + def from_text(string: str) -> Cbuffer: + """ + Returns a :class:`Cbuffer` from its text, the inverse of :meth:`as_text`. + + Args: + string: The text representation. + + Returns: + A new :class:`Cbuffer` instance. + + MEOS Functions: + cbuffer_in + """ + return Cbuffer(_inner=cbuffer_in(string)) + + @staticmethod + def from_ewkt(ewkt: str) -> Cbuffer: + """ + Returns a :class:`Cbuffer` from its EWKT, the inverse of :meth:`as_ewkt`. + + Args: + ewkt: The EWKT representation. + + Returns: + A new :class:`Cbuffer` instance. + + MEOS Functions: + cbuffer_in + """ + return Cbuffer(_inner=cbuffer_in(ewkt)) + + @staticmethod + def from_ewkb(ewkb: bytes) -> Cbuffer: + """ + Returns a :class:`Cbuffer` from its EWKB, the inverse of :meth:`as_ewkb`. + + Args: + ewkb: The EWKB representation. + + Returns: + A new :class:`Cbuffer` instance. + + MEOS Functions: + cbuffer_from_wkb + """ + return Cbuffer(_inner=cbuffer_from_wkb(ewkb)) + + @staticmethod + def from_hexewkb(hexewkb: str) -> Cbuffer: + """ + Returns a :class:`Cbuffer` from its hex-encoded EWKB, the inverse of :meth:`as_hexewkb`. + + Args: + hexewkb: The HEXEWKB representation. + + Returns: + A new :class:`Cbuffer` instance. + + MEOS Functions: + cbuffer_from_hexwkb + """ + return Cbuffer(_inner=cbuffer_from_hexwkb(hexewkb)) + + def as_ewkb(self, endian: str = "") -> bytes: + """ + Returns its Extended Well-Known Binary, its WKB carrying its SRID. + + Args: + endian: The byte order, ``"NDR"``, ``"XDR"`` or ``""`` for the + machine's own. + + Returns: + A :class:`bytes` instance. + + MEOS Functions: + cbuffer_as_ewkb, wkb_variant_from_endian + """ + return cbuffer_as_ewkb(self._inner, wkb_variant_from_endian(endian)) + + def as_hexewkb(self, endian: str = "") -> str: + """ + Returns its Extended Well-Known Binary as a hex-encoded string. + + Args: + endian: The byte order, ``"NDR"``, ``"XDR"`` or ``""`` for the + machine's own. + + Returns: + A :class:`str` instance. + + MEOS Functions: + cbuffer_as_hexewkb, wkb_variant_from_endian """ - return cbuffer_as_hexwkb(self._inner, -1)[0] + return cbuffer_as_hexewkb(self._inner, wkb_variant_from_endian(endian))[0] # ------------------------- Conversions ----------------------------------- def to_geometry(self, precision: int = 15) -> shp.BaseGeometry: diff --git a/pymeos/collections/cbuffer/cbufferset.py b/pymeos/collections/cbuffer/cbufferset.py index 7be001f1..c5577d70 100644 --- a/pymeos/collections/cbuffer/cbufferset.py +++ b/pymeos/collections/cbuffer/cbufferset.py @@ -34,6 +34,90 @@ class CbufferSet(Set[Cbuffer]): # ------------------------- Constructors ---------------------------------- + @classmethod + def from_ewkb(cls: type[Self], ewkb: bytes) -> Self: + """ + Returns a :class:`CbufferSet` from its EWKB, the inverse of :meth:`as_ewkb`. + + Args: + ewkb: The EWKB representation. + + Returns: + A new :class:`CbufferSet` instance. + + MEOS Functions: + set_from_wkb + """ + from ...factory import _CollectionFactory + + return _CollectionFactory.create_collection(set_from_wkb(ewkb)) + + @classmethod + def from_hexewkb(cls: type[Self], hexewkb: str) -> Self: + """ + Returns a :class:`CbufferSet` from its HEXEWKB, the inverse of :meth:`as_hexewkb`. + + Args: + hexewkb: The HEXEWKB representation. + + Returns: + A new :class:`CbufferSet` instance. + + MEOS Functions: + set_from_hexwkb + """ + from ...factory import _CollectionFactory + + return _CollectionFactory.create_collection(set_from_hexwkb(hexewkb)) + + @classmethod + def from_ewkt(cls: type[Self], ewkt: str) -> Self: + """ + Returns a :class:`CbufferSet` from its EWKT, the inverse of :meth:`as_ewkt`. + + Args: + ewkt: The EWKT representation. + + Returns: + A new :class:`CbufferSet` instance. + + MEOS Functions: + _in + """ + return cls.from_text(ewkt) + + def as_ewkb(self, endian: str = "") -> bytes: + """ + Returns the EWKB representation of ``self``, carrying its SRID. + + Args: + endian: The byte order, ``"NDR"``, ``"XDR"`` or ``""`` for the + machine's own. + + Returns: + A :class:`bytes` instance. + + MEOS Functions: + spatialset_as_ewkb, wkb_variant_from_endian + """ + return spatialset_as_ewkb(self._inner, wkb_variant_from_endian(endian)) + + def as_hexewkb(self, endian: str = "") -> str: + """ + Returns the HEXEWKB representation of ``self``, carrying its SRID. + + Args: + endian: The byte order, ``"NDR"``, ``"XDR"`` or ``""`` for the + machine's own. + + Returns: + A :class:`str` instance. + + MEOS Functions: + spatialset_as_hexewkb, wkb_variant_from_endian + """ + return spatialset_as_hexewkb(self._inner, wkb_variant_from_endian(endian))[0] + # ------------------------- Output ---------------------------------------- def __str__(self, max_decimals: int = 15): diff --git a/pymeos/collections/geo/geoset.py b/pymeos/collections/geo/geoset.py index fd3efde4..a44b6c9c 100644 --- a/pymeos/collections/geo/geoset.py +++ b/pymeos/collections/geo/geoset.py @@ -22,12 +22,17 @@ minus_geo_set, minus_set_geo, minus_set_set, + set_from_hexwkb, + set_from_wkb, set_round, + spatialset_as_ewkb, spatialset_as_ewkt, + spatialset_as_hexewkb, spatialset_as_text, spatialset_srid, union_set_geo, union_set_set, + wkb_variant_from_endian, ) from ..base import Set @@ -42,6 +47,90 @@ class GeoSet(Set[shp.Geometry], ABC): # ------------------------- Constructors ---------------------------------- + @classmethod + def from_ewkb(cls: type[Self], ewkb: bytes) -> Self: + """ + Returns a :class:`GeoSet` from its EWKB, the inverse of :meth:`as_ewkb`. + + Args: + ewkb: The EWKB representation. + + Returns: + A new :class:`GeoSet` instance. + + MEOS Functions: + set_from_wkb + """ + from ...factory import _CollectionFactory + + return _CollectionFactory.create_collection(set_from_wkb(ewkb)) + + @classmethod + def from_hexewkb(cls: type[Self], hexewkb: str) -> Self: + """ + Returns a :class:`GeoSet` from its HEXEWKB, the inverse of :meth:`as_hexewkb`. + + Args: + hexewkb: The HEXEWKB representation. + + Returns: + A new :class:`GeoSet` instance. + + MEOS Functions: + set_from_hexwkb + """ + from ...factory import _CollectionFactory + + return _CollectionFactory.create_collection(set_from_hexwkb(hexewkb)) + + @classmethod + def from_ewkt(cls: type[Self], ewkt: str) -> Self: + """ + Returns a :class:`GeoSet` from its EWKT, the inverse of :meth:`as_ewkt`. + + Args: + ewkt: The EWKT representation. + + Returns: + A new :class:`GeoSet` instance. + + MEOS Functions: + _in + """ + return cls.from_text(ewkt) + + def as_ewkb(self, endian: str = "") -> bytes: + """ + Returns the EWKB representation of ``self``, carrying its SRID. + + Args: + endian: The byte order, ``"NDR"``, ``"XDR"`` or ``""`` for the + machine's own. + + Returns: + A :class:`bytes` instance. + + MEOS Functions: + spatialset_as_ewkb, wkb_variant_from_endian + """ + return spatialset_as_ewkb(self._inner, wkb_variant_from_endian(endian)) + + def as_hexewkb(self, endian: str = "") -> str: + """ + Returns the HEXEWKB representation of ``self``, carrying its SRID. + + Args: + endian: The byte order, ``"NDR"``, ``"XDR"`` or ``""`` for the + machine's own. + + Returns: + A :class:`str` instance. + + MEOS Functions: + spatialset_as_hexewkb, wkb_variant_from_endian + """ + return spatialset_as_hexewkb(self._inner, wkb_variant_from_endian(endian))[0] + # ------------------------- Output ---------------------------------------- def __str__(self, max_decimals: int = 15): diff --git a/pymeos/collections/npoint/npoint.py b/pymeos/collections/npoint/npoint.py index 138eeaca..67f9ab11 100644 --- a/pymeos/collections/npoint/npoint.py +++ b/pymeos/collections/npoint/npoint.py @@ -138,30 +138,38 @@ def __repr__(self): """ return f"{self.__class__.__name__}({self})" - def as_wkb(self) -> bytes: + def as_wkb(self, endian: str = "") -> bytes: """ Returns the WKB representation of ``self``. + Args: + endian: The byte order, ``"NDR"``, ``"XDR"`` or ``""`` for the + machine's own. + Returns: A :class:`bytes` object with the WKB representation of ``self``. MEOS Functions: - npoint_as_wkb + npoint_as_wkb, wkb_variant_from_endian """ - return npoint_as_wkb(self._inner, 4) + return npoint_as_wkb(self._inner, wkb_variant_from_endian(endian)) - def as_hexwkb(self) -> str: + def as_hexwkb(self, endian: str = "") -> str: """ Returns the WKB representation of ``self`` in hex-encoded ASCII. + Args: + endian: The byte order, ``"NDR"``, ``"XDR"`` or ``""`` for the + machine's own. + Returns: A :class:`str` object with the WKB representation of ``self`` in hex-encoded ASCII. MEOS Functions: - npoint_as_hexwkb + npoint_as_hexwkb, wkb_variant_from_endian """ - return npoint_as_hexwkb(self._inner, -1)[0] + return npoint_as_hexwkb(self._inner, wkb_variant_from_endian(endian))[0] def as_ewkt(self, max_decimals: int = 15) -> str: """ @@ -195,6 +203,102 @@ def as_text(self, max_decimals: int = 15) -> str: """ return npoint_as_text(self._inner, max_decimals) + @staticmethod + def from_text(string: str) -> Npoint: + """ + Returns a :class:`Npoint` from its text, the inverse of :meth:`as_text`. + + Args: + string: The text representation. + + Returns: + A new :class:`Npoint` instance. + + MEOS Functions: + npoint_in + """ + return Npoint(_inner=npoint_in(string)) + + @staticmethod + def from_ewkt(ewkt: str) -> Npoint: + """ + Returns a :class:`Npoint` from its EWKT, the inverse of :meth:`as_ewkt`. + + Args: + ewkt: The EWKT representation. + + Returns: + A new :class:`Npoint` instance. + + MEOS Functions: + npoint_in + """ + return Npoint(_inner=npoint_in(ewkt)) + + @staticmethod + def from_ewkb(ewkb: bytes) -> Npoint: + """ + Returns a :class:`Npoint` from its EWKB, the inverse of :meth:`as_ewkb`. + + Args: + ewkb: The EWKB representation. + + Returns: + A new :class:`Npoint` instance. + + MEOS Functions: + npoint_from_wkb + """ + return Npoint(_inner=npoint_from_wkb(ewkb)) + + @staticmethod + def from_hexewkb(hexewkb: str) -> Npoint: + """ + Returns a :class:`Npoint` from its hex-encoded EWKB, the inverse of :meth:`as_hexewkb`. + + Args: + hexewkb: The HEXEWKB representation. + + Returns: + A new :class:`Npoint` instance. + + MEOS Functions: + npoint_from_hexwkb + """ + return Npoint(_inner=npoint_from_hexwkb(hexewkb)) + + def as_ewkb(self, endian: str = "") -> bytes: + """ + Returns its Extended Well-Known Binary, its WKB carrying its SRID. + + Args: + endian: The byte order, ``"NDR"``, ``"XDR"`` or ``""`` for the + machine's own. + + Returns: + A :class:`bytes` instance. + + MEOS Functions: + npoint_as_ewkb, wkb_variant_from_endian + """ + return npoint_as_ewkb(self._inner, wkb_variant_from_endian(endian)) + + def as_hexewkb(self, endian: str = "") -> str: + """ + Returns its Extended Well-Known Binary as a hex-encoded string. + + Args: + endian: The byte order, ``"NDR"``, ``"XDR"`` or ``""`` for the + machine's own. + + Returns: + A :class:`str` instance. + + MEOS Functions: + npoint_as_hexewkb, wkb_variant_from_endian + """ + return npoint_as_hexewkb(self._inner, wkb_variant_from_endian(endian))[0] + # ------------------------- Conversions ----------------------------------- def to_geometry(self, precision: int = 15) -> shp.BaseGeometry: """ diff --git a/pymeos/collections/npoint/npointset.py b/pymeos/collections/npoint/npointset.py index a2c1b918..5c32de10 100644 --- a/pymeos/collections/npoint/npointset.py +++ b/pymeos/collections/npoint/npointset.py @@ -34,6 +34,90 @@ class NpointSet(Set[Npoint]): # ------------------------- Constructors ---------------------------------- + @classmethod + def from_ewkb(cls: type[Self], ewkb: bytes) -> Self: + """ + Returns a :class:`NpointSet` from its EWKB, the inverse of :meth:`as_ewkb`. + + Args: + ewkb: The EWKB representation. + + Returns: + A new :class:`NpointSet` instance. + + MEOS Functions: + set_from_wkb + """ + from ...factory import _CollectionFactory + + return _CollectionFactory.create_collection(set_from_wkb(ewkb)) + + @classmethod + def from_hexewkb(cls: type[Self], hexewkb: str) -> Self: + """ + Returns a :class:`NpointSet` from its HEXEWKB, the inverse of :meth:`as_hexewkb`. + + Args: + hexewkb: The HEXEWKB representation. + + Returns: + A new :class:`NpointSet` instance. + + MEOS Functions: + set_from_hexwkb + """ + from ...factory import _CollectionFactory + + return _CollectionFactory.create_collection(set_from_hexwkb(hexewkb)) + + @classmethod + def from_ewkt(cls: type[Self], ewkt: str) -> Self: + """ + Returns a :class:`NpointSet` from its EWKT, the inverse of :meth:`as_ewkt`. + + Args: + ewkt: The EWKT representation. + + Returns: + A new :class:`NpointSet` instance. + + MEOS Functions: + _in + """ + return cls.from_text(ewkt) + + def as_ewkb(self, endian: str = "") -> bytes: + """ + Returns the EWKB representation of ``self``, carrying its SRID. + + Args: + endian: The byte order, ``"NDR"``, ``"XDR"`` or ``""`` for the + machine's own. + + Returns: + A :class:`bytes` instance. + + MEOS Functions: + spatialset_as_ewkb, wkb_variant_from_endian + """ + return spatialset_as_ewkb(self._inner, wkb_variant_from_endian(endian)) + + def as_hexewkb(self, endian: str = "") -> str: + """ + Returns the HEXEWKB representation of ``self``, carrying its SRID. + + Args: + endian: The byte order, ``"NDR"``, ``"XDR"`` or ``""`` for the + machine's own. + + Returns: + A :class:`str` instance. + + MEOS Functions: + spatialset_as_hexewkb, wkb_variant_from_endian + """ + return spatialset_as_hexewkb(self._inner, wkb_variant_from_endian(endian))[0] + # ------------------------- Output ---------------------------------------- def __str__(self, max_decimals: int = 15): """ diff --git a/pymeos/collections/npoint/nsegment.py b/pymeos/collections/npoint/nsegment.py index e0ec2989..06547629 100644 --- a/pymeos/collections/npoint/nsegment.py +++ b/pymeos/collections/npoint/nsegment.py @@ -59,14 +59,20 @@ def __copy__(self) -> Nsegment: return Nsegment(_inner=nsegment_round(self._inner, 100)) # ------------------------- Output ---------------------------------------- - def __str__(self, max_decimals: int = 15) -> str: + def __str__(self) -> str: + """ + Returns the text representation of ``self``, its :meth:`as_text`. + """ + return self.as_text() + + def as_text(self, max_decimals: int = 15) -> str: """ Returns the string representation of ``self``. MEOS Functions: - nsegment_out + nsegment_as_text """ - return nsegment_out(self._inner, max_decimals) + return nsegment_as_text(self._inner, max_decimals) def __repr__(self) -> str: return f"{self.__class__.__name__}({self})" @@ -108,6 +114,181 @@ def srid(self) -> int: """ return nsegment_srid(self._inner) + @staticmethod + def from_text(string: str) -> Nsegment: + """ + Returns a :class:`Nsegment` from its text, the inverse of :meth:`as_text`. + + Args: + string: The text representation. + + Returns: + A new :class:`Nsegment` instance. + + MEOS Functions: + nsegment_in + """ + return Nsegment(_inner=nsegment_in(string)) + + @staticmethod + def from_ewkt(ewkt: str) -> Nsegment: + """ + Returns a :class:`Nsegment` from its EWKT, the inverse of :meth:`as_ewkt`. + + Args: + ewkt: The EWKT representation. + + Returns: + A new :class:`Nsegment` instance. + + MEOS Functions: + nsegment_in + """ + return Nsegment(_inner=nsegment_in(ewkt)) + + @staticmethod + def from_wkb(wkb: bytes) -> Nsegment: + """ + Returns a :class:`Nsegment` from its WKB, the inverse of :meth:`as_wkb`. + + Args: + wkb: The WKB representation. + + Returns: + A new :class:`Nsegment` instance. + + MEOS Functions: + nsegment_from_wkb + """ + return Nsegment(_inner=nsegment_from_wkb(wkb)) + + @staticmethod + def from_hexwkb(hexwkb: str) -> Nsegment: + """ + Returns a :class:`Nsegment` from its hex-encoded WKB, the inverse of :meth:`as_hexwkb`. + + Args: + hexwkb: The HEXWKB representation. + + Returns: + A new :class:`Nsegment` instance. + + MEOS Functions: + nsegment_from_hexwkb + """ + return Nsegment(_inner=nsegment_from_hexwkb(hexwkb)) + + @staticmethod + def from_ewkb(ewkb: bytes) -> Nsegment: + """ + Returns a :class:`Nsegment` from its EWKB, the inverse of :meth:`as_ewkb`. + + Args: + ewkb: The EWKB representation. + + Returns: + A new :class:`Nsegment` instance. + + MEOS Functions: + nsegment_from_wkb + """ + return Nsegment(_inner=nsegment_from_wkb(ewkb)) + + @staticmethod + def from_hexewkb(hexewkb: str) -> Nsegment: + """ + Returns a :class:`Nsegment` from its hex-encoded EWKB, the inverse of :meth:`as_hexewkb`. + + Args: + hexewkb: The HEXEWKB representation. + + Returns: + A new :class:`Nsegment` instance. + + MEOS Functions: + nsegment_from_hexwkb + """ + return Nsegment(_inner=nsegment_from_hexwkb(hexewkb)) + + def as_ewkt(self, max_decimals: int = 15) -> str: + """ + Returns its Extended Well-Known Text, its text preceded by its SRID. + + Args: + max_decimals: The number of decimal places of the coordinates. + + Returns: + A :class:`str` instance. + + MEOS Functions: + nsegment_as_ewkt + """ + return nsegment_as_ewkt(self._inner, max_decimals) + + def as_wkb(self, endian: str = "") -> bytes: + """ + Returns its plain Well-Known Binary, as the SQL ``asBinary`` writes it. + + Args: + endian: The byte order, ``"NDR"``, ``"XDR"`` or ``""`` for the + machine's own. + + Returns: + A :class:`bytes` instance. + + MEOS Functions: + nsegment_as_wkb, wkb_variant_from_endian + """ + return nsegment_as_wkb(self._inner, wkb_variant_from_endian(endian)) + + def as_hexwkb(self, endian: str = "") -> str: + """ + Returns its plain Well-Known Binary as a hex-encoded string, as the SQL ``asHexWKB`` writes it. + + Args: + endian: The byte order, ``"NDR"``, ``"XDR"`` or ``""`` for the + machine's own. + + Returns: + A :class:`str` instance. + + MEOS Functions: + nsegment_as_hexwkb, wkb_variant_from_endian + """ + return nsegment_as_hexwkb(self._inner, wkb_variant_from_endian(endian))[0] + + def as_ewkb(self, endian: str = "") -> bytes: + """ + Returns its Extended Well-Known Binary, its WKB carrying its SRID. + + Args: + endian: The byte order, ``"NDR"``, ``"XDR"`` or ``""`` for the + machine's own. + + Returns: + A :class:`bytes` instance. + + MEOS Functions: + nsegment_as_ewkb, wkb_variant_from_endian + """ + return nsegment_as_ewkb(self._inner, wkb_variant_from_endian(endian)) + + def as_hexewkb(self, endian: str = "") -> str: + """ + Returns its Extended Well-Known Binary as a hex-encoded string. + + Args: + endian: The byte order, ``"NDR"``, ``"XDR"`` or ``""`` for the + machine's own. + + Returns: + A :class:`str` instance. + + MEOS Functions: + nsegment_as_hexewkb, wkb_variant_from_endian + """ + return nsegment_as_hexewkb(self._inner, wkb_variant_from_endian(endian))[0] + # ------------------------- Conversions ----------------------------------- def to_geometry(self, precision: int = 15) -> shp.BaseGeometry: """ diff --git a/pymeos/collections/number/floatset.py b/pymeos/collections/number/floatset.py index 23a77774..2d67661f 100644 --- a/pymeos/collections/number/floatset.py +++ b/pymeos/collections/number/floatset.py @@ -64,9 +64,15 @@ class FloatSet(Set[float]): # ------------------------- Output ---------------------------------------- - def __str__(self, max_decimals: int = 15): + def __str__(self) -> str: """ - Return the string representation of the content of ``self``. + Returns the text representation of ``self``, its :meth:`as_text`. + """ + return self.as_text() + + def as_text(self, max_decimals: int = 15) -> str: + """ + Return the text representation of the content of ``self``. Returns: A new :class:`str` instance diff --git a/pymeos/collections/number/floatspan.py b/pymeos/collections/number/floatspan.py index 0750e220..4ca773f6 100644 --- a/pymeos/collections/number/floatspan.py +++ b/pymeos/collections/number/floatspan.py @@ -70,9 +70,15 @@ class FloatSpan(Span[float]): _make_function = floatspan_make # ------------------------- Output ---------------------------------------- - def __str__(self, max_decimals: int = 15) -> str: + def __str__(self) -> str: """ - Return the string representation of the content of ``self``. + Returns the text representation of ``self``, its :meth:`as_text`. + """ + return self.as_text() + + def as_text(self, max_decimals: int = 15) -> str: + """ + Return the text representation of the content of ``self``. Returns: A new :class:`str` instance diff --git a/pymeos/collections/number/floatspanset.py b/pymeos/collections/number/floatspanset.py index 911411dc..3592dd94 100644 --- a/pymeos/collections/number/floatspanset.py +++ b/pymeos/collections/number/floatspanset.py @@ -58,9 +58,15 @@ class FloatSpanSet(SpanSet[float]): _parse_value_function = lambda span: floatspanset_in(span)[0] if isinstance(span, str) else span._inner[0] # ------------------------- Output ---------------------------------------- - def __str__(self, max_decimals: int = 15): + def __str__(self) -> str: """ - Return the string representation of the content of ``self``. + Returns the text representation of ``self``, its :meth:`as_text`. + """ + return self.as_text() + + def as_text(self, max_decimals: int = 15) -> str: + """ + Return the text representation of the content of ``self``. Returns: A new :class:`str` instance diff --git a/pymeos/collections/number/intset.py b/pymeos/collections/number/intset.py index 299ad444..3eb96247 100644 --- a/pymeos/collections/number/intset.py +++ b/pymeos/collections/number/intset.py @@ -63,9 +63,15 @@ class IntSet(Set[int]): # ------------------------- Output ---------------------------------------- - def __str__(self): + def __str__(self) -> str: """ - Return the string representation of the content of ``self``. + Returns the text representation of ``self``, its :meth:`as_text`. + """ + return self.as_text() + + def as_text(self) -> str: + """ + Return the text representation of the content of ``self``. Returns: A new :class:`str` instance diff --git a/pymeos/collections/number/intspan.py b/pymeos/collections/number/intspan.py index a42238d8..fab27b5f 100644 --- a/pymeos/collections/number/intspan.py +++ b/pymeos/collections/number/intspan.py @@ -69,9 +69,15 @@ class IntSpan(Span[int]): _make_function = intspan_make # ------------------------- Output ---------------------------------------- - def __str__(self): + def __str__(self) -> str: """ - Return the string representation of the content of ``self``. + Returns the text representation of ``self``, its :meth:`as_text`. + """ + return self.as_text() + + def as_text(self) -> str: + """ + Return the text representation of the content of ``self``. Returns: A new :class:`str` instance diff --git a/pymeos/collections/number/intspanset.py b/pymeos/collections/number/intspanset.py index f7d13967..df91ff15 100644 --- a/pymeos/collections/number/intspanset.py +++ b/pymeos/collections/number/intspanset.py @@ -58,9 +58,15 @@ class IntSpanSet(SpanSet[int]): _parse_value_function = lambda span: intspanset_in(span)[0] if isinstance(span, str) else span._inner[0] # ------------------------- Output ---------------------------------------- - def __str__(self): + def __str__(self) -> str: """ - Return the string representation of the content of ``self``. + Returns the text representation of ``self``, its :meth:`as_text`. + """ + return self.as_text() + + def as_text(self) -> str: + """ + Return the text representation of the content of ``self``. Returns: A new :class:`str` instance diff --git a/pymeos/collections/pose/pose.py b/pymeos/collections/pose/pose.py index 0dd806a4..991201dd 100644 --- a/pymeos/collections/pose/pose.py +++ b/pymeos/collections/pose/pose.py @@ -186,30 +186,134 @@ def as_ewkt(self, max_decimals: int = 15) -> str: """ return pose_as_ewkt(self._inner, max_decimals) - def as_wkb(self) -> bytes: + def as_wkb(self, endian: str = "") -> bytes: """ Returns the WKB representation of ``self``. + Args: + endian: The byte order, ``"NDR"``, ``"XDR"`` or ``""`` for the + machine's own. + Returns: A :class:`bytes` object with the WKB representation of ``self``. MEOS Functions: - pose_as_wkb + pose_as_wkb, wkb_variant_from_endian """ - return pose_as_wkb(self._inner, 4) + return pose_as_wkb(self._inner, wkb_variant_from_endian(endian)) - def as_hexwkb(self) -> str: + def as_hexwkb(self, endian: str = "") -> str: """ Returns the WKB representation of ``self`` in hex-encoded ASCII. + Args: + endian: The byte order, ``"NDR"``, ``"XDR"`` or ``""`` for the + machine's own. + Returns: A :class:`str` object with the WKB representation of ``self`` in hex-encoded ASCII. MEOS Functions: - pose_as_hexwkb + pose_as_hexwkb, wkb_variant_from_endian + """ + return pose_as_hexwkb(self._inner, wkb_variant_from_endian(endian))[0] + + @staticmethod + def from_text(string: str) -> Pose: + """ + Returns a :class:`Pose` from its text, the inverse of :meth:`as_text`. + + Args: + string: The text representation. + + Returns: + A new :class:`Pose` instance. + + MEOS Functions: + pose_from_text + """ + return Pose(_inner=pose_from_text(string)) + + @staticmethod + def from_ewkt(ewkt: str) -> Pose: + """ + Returns a :class:`Pose` from its EWKT, the inverse of :meth:`as_ewkt`. + + Args: + ewkt: The EWKT representation. + + Returns: + A new :class:`Pose` instance. + + MEOS Functions: + pose_from_text + """ + return Pose(_inner=pose_from_text(ewkt)) + + @staticmethod + def from_ewkb(ewkb: bytes) -> Pose: + """ + Returns a :class:`Pose` from its EWKB, the inverse of :meth:`as_ewkb`. + + Args: + ewkb: The EWKB representation. + + Returns: + A new :class:`Pose` instance. + + MEOS Functions: + pose_from_wkb + """ + return Pose(_inner=pose_from_wkb(ewkb)) + + @staticmethod + def from_hexewkb(hexewkb: str) -> Pose: + """ + Returns a :class:`Pose` from its hex-encoded EWKB, the inverse of :meth:`as_hexewkb`. + + Args: + hexewkb: The HEXEWKB representation. + + Returns: + A new :class:`Pose` instance. + + MEOS Functions: + pose_from_hexwkb + """ + return Pose(_inner=pose_from_hexwkb(hexewkb)) + + def as_ewkb(self, endian: str = "") -> bytes: + """ + Returns its Extended Well-Known Binary, its WKB carrying its SRID. + + Args: + endian: The byte order, ``"NDR"``, ``"XDR"`` or ``""`` for the + machine's own. + + Returns: + A :class:`bytes` instance. + + MEOS Functions: + pose_as_ewkb, wkb_variant_from_endian + """ + return pose_as_ewkb(self._inner, wkb_variant_from_endian(endian)) + + def as_hexewkb(self, endian: str = "") -> str: + """ + Returns its Extended Well-Known Binary as a hex-encoded string. + + Args: + endian: The byte order, ``"NDR"``, ``"XDR"`` or ``""`` for the + machine's own. + + Returns: + A :class:`str` instance. + + MEOS Functions: + pose_as_hexewkb, wkb_variant_from_endian """ - return pose_as_hexwkb(self._inner, -1)[0] + return pose_as_hexewkb(self._inner, wkb_variant_from_endian(endian))[0] # ------------------------- Conversions ----------------------------------- def to_geometry(self, precision: int = 15) -> shp.BaseGeometry: diff --git a/pymeos/collections/pose/poseset.py b/pymeos/collections/pose/poseset.py index 5162a67a..f1b443a3 100644 --- a/pymeos/collections/pose/poseset.py +++ b/pymeos/collections/pose/poseset.py @@ -34,6 +34,90 @@ class PoseSet(Set[Pose]): # ------------------------- Constructors ---------------------------------- + @classmethod + def from_ewkb(cls: type[Self], ewkb: bytes) -> Self: + """ + Returns a :class:`PoseSet` from its EWKB, the inverse of :meth:`as_ewkb`. + + Args: + ewkb: The EWKB representation. + + Returns: + A new :class:`PoseSet` instance. + + MEOS Functions: + set_from_wkb + """ + from ...factory import _CollectionFactory + + return _CollectionFactory.create_collection(set_from_wkb(ewkb)) + + @classmethod + def from_hexewkb(cls: type[Self], hexewkb: str) -> Self: + """ + Returns a :class:`PoseSet` from its HEXEWKB, the inverse of :meth:`as_hexewkb`. + + Args: + hexewkb: The HEXEWKB representation. + + Returns: + A new :class:`PoseSet` instance. + + MEOS Functions: + set_from_hexwkb + """ + from ...factory import _CollectionFactory + + return _CollectionFactory.create_collection(set_from_hexwkb(hexewkb)) + + @classmethod + def from_ewkt(cls: type[Self], ewkt: str) -> Self: + """ + Returns a :class:`PoseSet` from its EWKT, the inverse of :meth:`as_ewkt`. + + Args: + ewkt: The EWKT representation. + + Returns: + A new :class:`PoseSet` instance. + + MEOS Functions: + _in + """ + return cls.from_text(ewkt) + + def as_ewkb(self, endian: str = "") -> bytes: + """ + Returns the EWKB representation of ``self``, carrying its SRID. + + Args: + endian: The byte order, ``"NDR"``, ``"XDR"`` or ``""`` for the + machine's own. + + Returns: + A :class:`bytes` instance. + + MEOS Functions: + spatialset_as_ewkb, wkb_variant_from_endian + """ + return spatialset_as_ewkb(self._inner, wkb_variant_from_endian(endian)) + + def as_hexewkb(self, endian: str = "") -> str: + """ + Returns the HEXEWKB representation of ``self``, carrying its SRID. + + Args: + endian: The byte order, ``"NDR"``, ``"XDR"`` or ``""`` for the + machine's own. + + Returns: + A :class:`str` instance. + + MEOS Functions: + spatialset_as_hexewkb, wkb_variant_from_endian + """ + return spatialset_as_hexewkb(self._inner, wkb_variant_from_endian(endian))[0] + # ------------------------- Output ---------------------------------------- def __str__(self, max_decimals: int = 15): diff --git a/pymeos/collections/text/textset.py b/pymeos/collections/text/textset.py index 28eb0e8b..bd5871cd 100644 --- a/pymeos/collections/text/textset.py +++ b/pymeos/collections/text/textset.py @@ -35,9 +35,15 @@ class TextSet(Set[str]): # ------------------------- Output ---------------------------------------- - def __str__(self): + def __str__(self) -> str: """ - Return the string representation of the content of ``self``. + Returns the text representation of ``self``, its :meth:`as_text`. + """ + return self.as_text() + + def as_text(self) -> str: + """ + Return the text representation of the content of ``self``. Returns: A new :class:`str` instance diff --git a/pymeos/collections/time/dateset.py b/pymeos/collections/time/dateset.py index 29e64672..35ff3fbb 100644 --- a/pymeos/collections/time/dateset.py +++ b/pymeos/collections/time/dateset.py @@ -69,9 +69,15 @@ class DateSet(Set[date], TimeCollection[date]): # ------------------------- Constructors ---------------------------------- # ------------------------- Output ---------------------------------------- - def __str__(self): + def __str__(self) -> str: """ - Return the string representation of the content of ``self``. + Returns the text representation of ``self``, its :meth:`as_text`. + """ + return self.as_text() + + def as_text(self) -> str: + """ + Return the text representation of the content of ``self``. Returns: A new :class:`str` instance diff --git a/pymeos/collections/time/datespan.py b/pymeos/collections/time/datespan.py index 45f24c3b..5bf41043 100644 --- a/pymeos/collections/time/datespan.py +++ b/pymeos/collections/time/datespan.py @@ -75,9 +75,15 @@ class DateSpan(Span[date], TimeCollection[date]): # ------------------------- Constructors ---------------------------------- # ------------------------- Output ---------------------------------------- - def __str__(self): + def __str__(self) -> str: """ - Return the string representation of the content of ``self``. + Returns the text representation of ``self``, its :meth:`as_text`. + """ + return self.as_text() + + def as_text(self) -> str: + """ + Return the text representation of the content of ``self``. Returns: A new :class:`str` instance diff --git a/pymeos/collections/time/datespanset.py b/pymeos/collections/time/datespanset.py index 2b177f21..74b724c5 100644 --- a/pymeos/collections/time/datespanset.py +++ b/pymeos/collections/time/datespanset.py @@ -70,9 +70,15 @@ class DateSpanSet(SpanSet[date], TimeCollection[date]): ) # ------------------------- Output ---------------------------------------- - def __str__(self): + def __str__(self) -> str: """ - Return the string representation of the content of ``self``. + Returns the text representation of ``self``, its :meth:`as_text`. + """ + return self.as_text() + + def as_text(self) -> str: + """ + Return the text representation of the content of ``self``. Returns: A new :class:`str` instance diff --git a/pymeos/collections/time/tstzset.py b/pymeos/collections/time/tstzset.py index 839862f6..6403760a 100644 --- a/pymeos/collections/time/tstzset.py +++ b/pymeos/collections/time/tstzset.py @@ -45,9 +45,15 @@ class TsTzSet(Set[datetime], TimeCollection[datetime]): # ------------------------- Constructors ---------------------------------- # ------------------------- Output ---------------------------------------- - def __str__(self): + def __str__(self) -> str: """ - Return the string representation of the content of ``self``. + Returns the text representation of ``self``, its :meth:`as_text`. + """ + return self.as_text() + + def as_text(self) -> str: + """ + Return the text representation of the content of ``self``. Returns: A new :class:`str` instance diff --git a/pymeos/collections/time/tstzspan.py b/pymeos/collections/time/tstzspan.py index 017b22e5..913842f5 100644 --- a/pymeos/collections/time/tstzspan.py +++ b/pymeos/collections/time/tstzspan.py @@ -47,9 +47,15 @@ class TsTzSpan(Span[datetime], TimeCollection[datetime]): # ------------------------- Constructors ---------------------------------- # ------------------------- Output ---------------------------------------- - def __str__(self): + def __str__(self) -> str: """ - Return the string representation of the content of ``self``. + Returns the text representation of ``self``, its :meth:`as_text`. + """ + return self.as_text() + + def as_text(self) -> str: + """ + Return the text representation of the content of ``self``. Returns: A new :class:`str` instance diff --git a/pymeos/collections/time/tstzspanset.py b/pymeos/collections/time/tstzspanset.py index 424491ad..03d21bcf 100644 --- a/pymeos/collections/time/tstzspanset.py +++ b/pymeos/collections/time/tstzspanset.py @@ -45,9 +45,15 @@ class TsTzSpanSet(SpanSet[datetime], TimeCollection[datetime]): ) # ------------------------- Output ---------------------------------------- - def __str__(self): + def __str__(self) -> str: """ - Return the string representation of the content of ``self``. + Returns the text representation of ``self``, its :meth:`as_text`. + """ + return self.as_text() + + def as_text(self) -> str: + """ + Return the text representation of the content of ``self``. Returns: A new :class:`str` instance diff --git a/pymeos/main/tbool.py b/pymeos/main/tbool.py index 2ddacc92..368c41d1 100644 --- a/pymeos/main/tbool.py +++ b/pymeos/main/tbool.py @@ -101,7 +101,13 @@ def from_mfjson(cls: type[Self], mfjson: str) -> Self: return Temporal._factory(result) # ------------------------- Output ---------------------------------------- - def __str__(self): + def __str__(self) -> str: + """ + Returns the text representation of ``self``, its :meth:`as_text`. + """ + return self.as_text() + + def as_text(self) -> str: """ Returns the string representation of `self`. diff --git a/pymeos/main/tcbuffer.py b/pymeos/main/tcbuffer.py index 8b5b0fd7..a1705eec 100644 --- a/pymeos/main/tcbuffer.py +++ b/pymeos/main/tcbuffer.py @@ -8,7 +8,7 @@ from ..collections import * from ..collections.cbuffer import Cbuffer, CbufferSet -from ..mixins import TTemporallyComparable +from ..mixins import TSpatialIO, TTemporallyComparable from ..temporal import Temporal, TInstant, TInterpolation, TSequence, TSequenceSet from .tfloat import TFloat from .tpoint import TPoint @@ -23,6 +23,7 @@ class TCbuffer( Temporal[Cbuffer, "TCbuffer", "TCbufferInst", "TCbufferSeq", "TCbufferSeqSet"], TTemporallyComparable, + TSpatialIO, ABC, ): """ @@ -59,7 +60,13 @@ def from_point_radius(point: TPoint, radius: TFloat) -> TCbuffer: return Temporal._factory(result) # ------------------------- Output ---------------------------------------- - def __str__(self): + def __str__(self) -> str: + """ + Returns the text representation of ``self``, its :meth:`as_text`. + """ + return self.as_text() + + def as_text(self, max_decimals: int = 15) -> str: """ Returns the string representation of `self`. @@ -69,7 +76,7 @@ def __str__(self): MEOS Functions: tspatial_as_text """ - return tspatial_as_text(self._inner, 15) + return tspatial_as_text(self._inner, max_decimals) def as_wkt(self, precision: int = 15) -> str: """ diff --git a/pymeos/main/tfloat.py b/pymeos/main/tfloat.py index acf659da..432a3cab 100644 --- a/pymeos/main/tfloat.py +++ b/pymeos/main/tfloat.py @@ -119,7 +119,13 @@ def from_mfjson(cls: type[Self], mfjson: str) -> Self: return Temporal._factory(result) # ------------------------- Output ---------------------------------------- - def __str__(self, max_decimals: int = 15): + def __str__(self) -> str: + """ + Returns the text representation of ``self``, its :meth:`as_text`. + """ + return self.as_text() + + def as_text(self, max_decimals: int = 15) -> str: """ Returns a string representation of `self`. diff --git a/pymeos/main/tint.py b/pymeos/main/tint.py index 7d218c3b..3862c8a6 100644 --- a/pymeos/main/tint.py +++ b/pymeos/main/tint.py @@ -107,7 +107,13 @@ def from_mfjson(cls: type[Self], mfjson: str) -> Self: return Temporal._factory(result) # ------------------------- Output ---------------------------------------- - def __str__(self): + def __str__(self) -> str: + """ + Returns the text representation of ``self``, its :meth:`as_text`. + """ + return self.as_text() + + def as_text(self) -> str: """ Returns a string representation of `self`. diff --git a/pymeos/main/tnpoint.py b/pymeos/main/tnpoint.py index 03974145..88db3093 100644 --- a/pymeos/main/tnpoint.py +++ b/pymeos/main/tnpoint.py @@ -8,7 +8,7 @@ from ..collections import * from ..collections.npoint import Npoint, NpointSet, Nsegment -from ..mixins import TTemporallyComparable +from ..mixins import TSpatialIO, TTemporallyComparable from ..temporal import Temporal, TInstant, TInterpolation, TSequence, TSequenceSet if TYPE_CHECKING: @@ -24,6 +24,7 @@ class TNpoint( Temporal[Npoint, "TNpoint", "TNpointInst", "TNpointSeq", "TNpointSeqSet"], TTemporallyComparable, + TSpatialIO, ABC, ): """ @@ -120,7 +121,13 @@ def from_mfjson(cls: type[Self], mfjson: str) -> Self: return Temporal._factory(result) # ------------------------- Output ---------------------------------------- - def __str__(self, max_decimals: int = 15) -> str: + def __str__(self) -> str: + """ + Returns the text representation of ``self``, its :meth:`as_text`. + """ + return self.as_text() + + def as_text(self, max_decimals: int = 15) -> str: """ Returns the string representation of `self`. @@ -128,9 +135,9 @@ def __str__(self, max_decimals: int = 15) -> str: A string with the string representation of `self`. MEOS Functions: - tnpoint_out + tspatial_as_text """ - return tnpoint_out(self._inner, max_decimals) + return tspatial_as_text(self._inner, max_decimals) def as_wkt(self, max_decimals: int = 15) -> str: """ @@ -140,9 +147,9 @@ def as_wkt(self, max_decimals: int = 15) -> str: A string with the Well-Known Text representation of `self`. MEOS Functions: - tnpoint_out + tspatial_as_text """ - return tnpoint_out(self._inner, max_decimals) + return tspatial_as_text(self._inner, max_decimals) # ------------------------- Value Accessors ------------------------------- def start_value(self) -> Npoint: diff --git a/pymeos/main/tpoint.py b/pymeos/main/tpoint.py index fb5e2ffe..c528f30e 100644 --- a/pymeos/main/tpoint.py +++ b/pymeos/main/tpoint.py @@ -12,7 +12,7 @@ from pymeos_cffi import * from ..collections import * -from ..mixins import TSimplifiable +from ..mixins import TSimplifiable, TSpatialIO from ..temporal import Temporal, TInstant, TInterpolation, TSequence, TSequenceSet from .tbool import TBool from .tfloat import TFloat, TFloatInst, TFloatSeq, TFloatSeqSet @@ -41,7 +41,7 @@ def import_geopandas(): TF = TypeVar("TF", bound="TFloat", covariant=True) -class TPoint(Temporal[shp.Point, TG, TI, TS, TSS], TSimplifiable, ABC): +class TPoint(Temporal[shp.Point, TG, TI, TS, TSS], TSimplifiable, TSpatialIO, ABC): """ Abstract class for temporal points. """ @@ -58,7 +58,13 @@ def from_hexwkb(cls: type[Self], hexwkb: str, srid: int | None = None) -> Self: return result.set_srid(srid) if srid is not None else result # ------------------------- Output ---------------------------------------- - def __str__(self): + def __str__(self) -> str: + """ + Returns the text representation of ``self``, its :meth:`as_text`. + """ + return self.as_text() + + def as_text(self, max_decimals: int = 15) -> str: """ Returns the string representation of the temporal point. @@ -68,7 +74,7 @@ def __str__(self): MEOS Functions: tspatial_as_text """ - return tspatial_as_text(self._inner, 15) + return tspatial_as_text(self._inner, max_decimals) def as_wkt(self, precision: int = 15) -> str: """ diff --git a/pymeos/main/tpose.py b/pymeos/main/tpose.py index 21ad36e6..b1b4e136 100644 --- a/pymeos/main/tpose.py +++ b/pymeos/main/tpose.py @@ -8,7 +8,7 @@ from ..collections import * from ..collections.pose import Pose, PoseSet -from ..mixins import TTemporallyComparable +from ..mixins import TSpatialIO, TTemporallyComparable from ..temporal import Temporal, TInstant, TInterpolation, TSequence, TSequenceSet from .tbool import TBool from .tfloat import TFloat @@ -23,6 +23,7 @@ class TPose( Temporal[Pose, "TPose", "TPoseInst", "TPoseSeq", "TPoseSeqSet"], TTemporallyComparable, + TSpatialIO, ABC, ): """ @@ -138,7 +139,13 @@ def from_tpoint_tfloat(tpoint: TPoint, tradius: TFloat) -> TPose: return Temporal._factory(result) # ------------------------- Output ---------------------------------------- - def __str__(self): + def __str__(self) -> str: + """ + Returns the text representation of ``self``, its :meth:`as_text`. + """ + return self.as_text() + + def as_text(self, max_decimals: int = 15) -> str: """ Returns the string representation of `self`. @@ -148,7 +155,7 @@ def __str__(self): MEOS Functions: tspatial_as_text """ - return tspatial_as_text(self._inner, 15) + return tspatial_as_text(self._inner, max_decimals) def as_wkt(self, precision: int = 15) -> str: """ diff --git a/pymeos/main/trgeometry.py b/pymeos/main/trgeometry.py index 5388cd10..4cbb25b5 100644 --- a/pymeos/main/trgeometry.py +++ b/pymeos/main/trgeometry.py @@ -8,7 +8,7 @@ from ..collections import * from ..collections.pose import Pose -from ..mixins import TTemporallyComparable +from ..mixins import TSpatialIO, TTemporallyComparable from ..temporal import Temporal, TInstant, TInterpolation, TSequence, TSequenceSet from .tbool import TBool from .tfloat import TFloat @@ -30,6 +30,7 @@ class TRgeometry( "TRgeometrySeqSet", ], TTemporallyComparable, + TSpatialIO, ABC, ): """ @@ -109,7 +110,13 @@ def from_geometry_tpose(geometry: shpb.BaseGeometry, tpose: TPose) -> TRgeometry return Temporal._factory(result) # ------------------------- Output ---------------------------------------- - def __str__(self): + def __str__(self) -> str: + """ + Returns the text representation of ``self``, its :meth:`as_text`. + """ + return self.as_text() + + def as_text(self, max_decimals: int = 15) -> str: """ Returns the string representation of `self`. @@ -119,7 +126,7 @@ def __str__(self): MEOS Functions: trgeometry_as_text """ - return trgeometry_as_text(self._inner, 15) + return trgeometry_as_text(self._inner, max_decimals) def as_wkt(self, precision: int = 15) -> str: """ diff --git a/pymeos/main/ttext.py b/pymeos/main/ttext.py index 2dfd521b..61898471 100644 --- a/pymeos/main/ttext.py +++ b/pymeos/main/ttext.py @@ -108,6 +108,12 @@ def from_mfjson(cls: type[Self], mfjson: str) -> Self: # ------------------------- Output ---------------------------------------- def __str__(self) -> str: + """ + Returns the text representation of ``self``, its :meth:`as_text`. + """ + return self.as_text() + + def as_text(self) -> str: """ Returns the string representation of `self`. diff --git a/pymeos/mixins/__init__.py b/pymeos/mixins/__init__.py index cf55cde0..e220550e 100644 --- a/pymeos/mixins/__init__.py +++ b/pymeos/mixins/__init__.py @@ -1,10 +1,12 @@ from .comparison import TComparable from .simplify import TSimplifiable +from .spatial_io import TSpatialIO from .temporal_comparison import TTemporallyComparable, TTemporallyEquatable __all__ = [ "TComparable", "TSimplifiable", + "TSpatialIO", "TTemporallyComparable", "TTemporallyEquatable", ] diff --git a/pymeos/mixins/spatial_io.py b/pymeos/mixins/spatial_io.py new file mode 100644 index 00000000..6693cb36 --- /dev/null +++ b/pymeos/mixins/spatial_io.py @@ -0,0 +1,129 @@ +from __future__ import annotations + +from typing import TYPE_CHECKING, Any, TypeVar + +from pymeos_cffi import ( + temporal_from_hexwkb, + temporal_from_wkb, + tspatial_as_ewkb, + tspatial_as_ewkt, + tspatial_as_hexewkb, + wkb_variant_from_endian, +) + +if TYPE_CHECKING: + from ..temporal import Temporal + +Self = TypeVar("Self", bound="Temporal[Any]") + + +class TSpatialIO: + """ + The input and output forms carrying the SRID of a spatial temporal value, + as the SQL ``asEWKT``, ``asEWKB``, ``asHexEWKB`` and their readers write + and read them; a mixin of the spatial temporal classes as #TSimplifiable + is of #TPoint, its ``as_ewkt`` the one #TPoint states. + """ + + def as_ewkt(self, max_decimals: int = 15) -> str: + """ + Returns the Extended Well-Known Text representation of ``self``, its + text preceded by its SRID. + + Args: + max_decimals: The number of decimal places of the coordinates. + + Returns: + A :class:`str` with the EWKT of ``self``. + + MEOS Functions: + tspatial_as_ewkt + """ + return tspatial_as_ewkt(self._inner, max_decimals) + + def as_ewkb(self, endian: str = "") -> bytes: + """ + Returns the Extended Well-Known Binary representation of ``self``, its + WKB carrying its SRID. + + Args: + endian: The byte order, ``"NDR"``, ``"XDR"`` or ``""`` for the + machine's own. + + Returns: + The EWKB of ``self``. + + MEOS Functions: + tspatial_as_ewkb, wkb_variant_from_endian + """ + return tspatial_as_ewkb(self._inner, wkb_variant_from_endian(endian)) + + def as_hexewkb(self, endian: str = "") -> str: + """ + Returns the Extended Well-Known Binary representation of ``self`` as a + hex-encoded string. + + Args: + endian: The byte order, ``"NDR"``, ``"XDR"`` or ``""`` for the + machine's own. + + Returns: + A :class:`str` with the hex-encoded EWKB of ``self``. + + MEOS Functions: + tspatial_as_hexewkb, wkb_variant_from_endian + """ + return tspatial_as_hexewkb(self._inner, wkb_variant_from_endian(endian))[0] + + @classmethod + def from_ewkt(cls: type[Self], ewkt: str) -> Self: + """ + Returns a temporal value from its EWKT, the inverse of :meth:`as_ewkt`. + + Args: + ewkt: The EWKT. + + Returns: + A temporal value of the class's type. + + MEOS Functions: + _in + """ + return cls.from_text(ewkt) + + @classmethod + def from_ewkb(cls: type[Self], ewkb: bytes) -> Self: + """ + Returns a temporal value from its EWKB, the inverse of :meth:`as_ewkb`. + + Args: + ewkb: The EWKB. + + Returns: + A temporal value of the class's type. + + MEOS Functions: + temporal_from_wkb + """ + from ..temporal import Temporal + + return Temporal._factory(temporal_from_wkb(ewkb)) + + @classmethod + def from_hexewkb(cls: type[Self], hexewkb: str) -> Self: + """ + Returns a temporal value from its hex-encoded EWKB, the inverse of + :meth:`as_hexewkb`. + + Args: + hexewkb: The hex-encoded EWKB. + + Returns: + A temporal value of the class's type. + + MEOS Functions: + temporal_from_hexwkb + """ + from ..temporal import Temporal + + return Temporal._factory(temporal_from_hexwkb(hexewkb)) diff --git a/pymeos/temporal/temporal.py b/pymeos/temporal/temporal.py index 9099df25..49d4050e 100644 --- a/pymeos/temporal/temporal.py +++ b/pymeos/temporal/temporal.py @@ -150,6 +150,23 @@ def from_hexwkb(cls: type[Self], hexwkb: str) -> Self: result = temporal_from_hexwkb(hexwkb) return Temporal._factory(result) + @classmethod + def from_text(cls: type[Self], text: str) -> Self: + """ + Returns a temporal object from its text representation, the inverse + of :meth:`as_text`. + + Args: + text: The text representation. + + Returns: + A temporal object. + + MEOS Functions: + _in + """ + return Temporal._factory(cls._parse_function(text)) + @classmethod def from_merge(cls: type[Self], *temporals: TG) -> Self: """ @@ -222,29 +239,49 @@ def as_mfjson( """ return temporal_as_mfjson(self._inner, with_bbox, flags, precision, srs) - def as_wkb(self) -> bytes: + def as_text(self) -> str: """ - Returns the temporal object as a hex-encoded WKB string. + Returns the text representation of the temporal object, its WKT for + a spatial one. Returns: - The temporal object as a hex-encoded WKB string. + The text representation of the temporal object. + """ + return self.as_wkt() + + def as_wkb(self, endian: str = "") -> bytes: + """ + Returns the temporal object as WKB bytes, plain WKB for a spatial one, + as the SQL ``asBinary`` writes it. + + Args: + endian: The byte order, ``"NDR"``, ``"XDR"`` or ``""`` for the + machine's own. + + Returns: + The temporal object as WKB bytes. MEOS Functions: - temporal_as_hexwkb + temporal_as_wkb, wkb_variant_from_endian """ - return temporal_as_wkb(self._inner, 4) + return temporal_as_wkb(self._inner, wkb_variant_from_endian(endian)) - def as_hexwkb(self) -> str: + def as_hexwkb(self, endian: str = "") -> str: """ - Returns the temporal object as a hex-encoded WKB string. + Returns the temporal object as a hex-encoded WKB string, plain WKB for + a spatial one, as the SQL ``asHexWKB`` writes it. + + Args: + endian: The byte order, ``"NDR"``, ``"XDR"`` or ``""`` for the + machine's own. Returns: The temporal object as a hex-encoded WKB string. MEOS Functions: - temporal_as_hexwkb + temporal_as_hexwkb, wkb_variant_from_endian """ - return temporal_as_hexwkb(self._inner, 4)[0] + return temporal_as_hexwkb(self._inner, wkb_variant_from_endian(endian))[0] # ------------------------- Accessors ------------------------------------- def bounding_box(self) -> TsTzSpan | Box: diff --git a/tests/io_forms_test.py b/tests/io_forms_test.py new file mode 100644 index 00000000..8f5db045 --- /dev/null +++ b/tests/io_forms_test.py @@ -0,0 +1,150 @@ +import pytest + +from pymeos import ( + Cbuffer, + CbufferSet, + FloatSet, + FloatSpan, + GeometrySet, + IntSet, + IntSpan, + IntSpanSet, + Npoint, + NpointSet, + Nsegment, + Pose, + PoseSet, + STBox, + TBoolInst, + TBox, + TCbufferInst, + TextSet, + TFloatSeq, + TGeogPointInst, + TGeomPointSeq, + TIntSeq, + TNpointInst, + TPoseInst, + TRgeometryInst, + TsTzSet, + TsTzSpan, + TsTzSpanSet, + TTextInst, +) +from tests.conftest import TestPyMEOS + +# Every value with the text its as_text writes, which str returns. +TEXT = [ + (TFloatSeq("[1.5@2001-01-01, 2.5@2001-01-02]"), "[1.5@2001-01-01 00:00:00+00, 2.5@2001-01-02 00:00:00+00]"), + (TIntSeq("[1@2001-01-01, 2@2001-01-02]"), "[1@2001-01-01 00:00:00+00, 2@2001-01-02 00:00:00+00]"), + (TBoolInst("t@2001-01-01"), "t@2001-01-01 00:00:00+00"), + (TTextInst("AAA@2001-01-01"), '"AAA"@2001-01-01 00:00:00+00'), + ( + TGeomPointSeq("SRID=4326;[Point(1 1)@2001-01-01, Point(2 2)@2001-01-02]"), + "[POINT(1 1)@2001-01-01 00:00:00+00, POINT(2 2)@2001-01-02 00:00:00+00]", + ), + (TGeogPointInst("SRID=4326;Point(1 1)@2001-01-01"), "POINT(1 1)@2001-01-01 00:00:00+00"), + (TCbufferInst("SRID=3812;Cbuffer(Point(1 1),0.5)@2001-01-01"), "Cbuffer(POINT(1 1),0.5)@2001-01-01 00:00:00+00"), + (TPoseInst("SRID=3812;Pose(Point(1 1),0.5)@2001-01-01"), "Pose(POINT(1 1),0.5)@2001-01-01 00:00:00+00"), + (TNpointInst("NPoint(1,0.5)@2001-01-01"), "NPoint(1,0.5)@2001-01-01 00:00:00+00"), + ( + TRgeometryInst("SRID=3812;Polygon((0 0,1 0,0 1,0 0));Pose(Point(1 1),0.5)@2001-01-01"), + "POLYGON((0 0,1 0,0 1,0 0));Pose(POINT(1 1),0.5)@2001-01-01 00:00:00+00", + ), + (IntSet("{1, 2}"), "{1, 2}"), + (FloatSet("{1.5, 2.5}"), "{1.5, 2.5}"), + (TextSet('{"a", "b"}'), '{"a", "b"}'), + (TsTzSet("{2001-01-01, 2001-01-02}"), '{"2001-01-01 00:00:00+00", "2001-01-02 00:00:00+00"}'), + (GeometrySet("SRID=4326;{Point(1 1), Point(2 2)}"), '{"POINT(1 1)", "POINT(2 2)"}'), + (PoseSet('SRID=3812;{"Pose(Point(1 1),0.5)"}'), '{"Pose(POINT(1 1),0.5)"}'), + (CbufferSet('SRID=3812;{"Cbuffer(Point(1 1),0.5)"}'), '{"Cbuffer(POINT(1 1),0.5)"}'), + (NpointSet('{"NPoint(1,0.5)"}'), '{"NPoint(1,0.5)"}'), + (IntSpan("[1, 3)"), "[1, 3)"), + (FloatSpan("[1.5, 2.5]"), "[1.5, 2.5]"), + (TsTzSpan("[2001-01-01, 2001-01-02]"), "[2001-01-01 00:00:00+00, 2001-01-02 00:00:00+00]"), + (IntSpanSet("{[1, 3), [4, 5)}"), "{[1, 3), [4, 5)}"), + (TsTzSpanSet("{[2001-01-01, 2001-01-02]}"), "{[2001-01-01 00:00:00+00, 2001-01-02 00:00:00+00]}"), + ( + TBox("TBOXFLOAT XT([1.5, 2.5],[2001-01-01, 2001-01-02])"), + "TBOXFLOAT XT([1.5, 2.5],[2001-01-01 00:00:00+00, 2001-01-02 00:00:00+00])", + ), + ( + STBox("SRID=4326;STBOX XT(((1,1),(2,2)),[2001-01-01, 2001-01-02])"), + "SRID=4326;STBOX XT(((1,1),(2,2)),[2001-01-01 00:00:00+00, 2001-01-02 00:00:00+00])", + ), + (Pose("SRID=3812;Pose(Point(1 1),0.5)"), "Pose(POINT(1 1),0.5)"), + (Cbuffer("SRID=3812;Cbuffer(Point(1 1),0.5)"), "Cbuffer(POINT(1 1),0.5)"), + (Npoint("NPoint(1,0.5)"), "NPoint(1,0.5)"), + (Nsegment("NSegment(1,0.2,0.5)"), "NSegment(1,0.2,0.5)"), +] +IDS = [type(v).__name__ for v, _ in TEXT] + +# Every value carrying an SRID with the EWKT its as_ewkt writes. +EWKT = [ + (TEXT[i][0], srid + TEXT[i][1]) + for i, srid in ( + (4, "SRID=4326;"), + (5, "SRID=4326;"), + (6, "SRID=3812;"), + (7, "SRID=3812;"), + (8, "SRID=5676;"), + (9, "SRID=3812;"), + (14, "SRID=4326;"), + (15, "SRID=3812;"), + (16, "SRID=3812;"), + (17, "SRID=5676;"), + (25, "SRID=3812;"), + (26, "SRID=3812;"), + (27, "SRID=5676;"), + (28, "SRID=5676;"), + ) +] +EWKT_IDS = [type(v).__name__ for v, _ in EWKT] +# The values whose binary forms carry an SRID: those of EWKT, and STBox, whose +# text carries it. +EWKB = [v for v, _ in EWKT] + [TEXT[24][0]] +EWKB_IDS = [type(v).__name__ for v in EWKB] + + +class TestIOForms(TestPyMEOS): + """ + The portable readers and writers of every class, as #TestPose states + ``test_str`` and ``test_from_as_hexwkb`` for a pose: ``str`` is ``as_text``, + the plain forms carry no SRID and the E forms carry it, as the SQL + ``asText``, ``asBinary``, ``asEWKT`` and ``asEWKB`` write them. + """ + + @pytest.mark.parametrize("value, text", TEXT, ids=IDS) + def test_str_is_as_text(self, value, text): + assert value.as_text() == text + assert str(value) == text + + @pytest.mark.parametrize("value, text", TEXT, ids=IDS) + def test_from_text(self, value, text): + assert type(value).from_text(text).as_text() == text + + @pytest.mark.parametrize("value, text", TEXT, ids=IDS) + def test_wkb_endian(self, value, text): + assert value.as_hexwkb("NDR")[:2] == "01" + assert value.as_hexwkb("XDR")[:2] == "00" + assert value.as_wkb("NDR") == bytes.fromhex(value.as_hexwkb("NDR")) + reread = type(value).from_hexwkb(value.as_hexwkb("XDR")) + assert reread.as_hexwkb("NDR") == value.as_hexwkb("NDR") + + @pytest.mark.parametrize("value, ewkt", EWKT, ids=EWKT_IDS) + def test_as_ewkt(self, value, ewkt): + assert value.as_ewkt() == ewkt + assert type(value).from_ewkt(ewkt) == value + + @pytest.mark.parametrize("value", EWKB, ids=EWKB_IDS) + def test_ewkb_round_trip(self, value): + cls = type(value) + assert cls.from_ewkb(value.as_ewkb()) == value + assert cls.from_hexewkb(value.as_hexewkb()) == value + assert cls.from_hexewkb(value.as_hexewkb("XDR")) == value + + def test_plain_wkb_carries_no_srid(self): + value = TGeomPointSeq("SRID=4326;[Point(1 1)@2001-01-01, Point(2 2)@2001-01-02]") + assert TGeomPointSeq.from_hexwkb(value.as_hexwkb()).srid() == 0 + assert TGeomPointSeq.from_hexewkb(value.as_hexewkb()).srid() == 4326 + assert value.as_hexwkb() != value.as_hexewkb()