a
    ‹'fh  ã                   @   s    d Z ddlZddlZddlmZ ddlmZ ddlmZ e 	ej
¡e 	d¡k r~ddlmZ dd	lmZ dd
lmZ ddd„ZnddlmZ dd„ Zddd„ZdS )zèThis module provides utility functions for integrating with Shapely.

.. note::

    As GeoAlchemy 2 itself has no dependency on `Shapely`, applications using
    functions of this module have to ensure that `Shapely` is available.
é    N)Úversioné   )Ú
WKBElement)Ú
WKTElementz1.7)Úgeom_factory)Ú	WKBWriter)ÚlgeosFc                 K   s\   |dur0t  | j¡}t  ||¡ t|ƒ} d|d< tt fi |¤Ž}|rN| | ¡S | | ¡S dS )a?  Dump a WKB representation of a geometry to a byte or hex string.

        Args:
            ob (geometry): The geometry to export to well-known binary (WKB) representation
            hex (bool): If true, export the WKB as a hexadecimal string. The default is to
                return a binary string/bytes object.
            srid (int): Spatial reference system ID to include in the output. The default
                value means no SRID is included.

        Keyword Args:
            kwargs: See available keyword output settings in ``shapely.geos.WKBWriter``.
        NTZinclude_srid)r   ZGEOSGeom_cloneZ_geomZGEOSSetSRIDr   r   Z	write_hexÚwrite)ÚobÚhexÚsridÚkwZgeomÚwriter© r   ú_/var/www/staging/api/virtual_environments/venv/lib/python3.9/site-packages/geoalchemy2/shape.pyÚdumps   s    
r   )r   c                 C   s   t | ttfƒsJ ‚t | tƒrTt | jtƒr2| jdfnt| jƒdf\}}tjj||dS t | tƒrŒ| j	r~tj
 | j dd¡d ¡S tj
 | j¡S dS )a	  Function to convert a :class:`geoalchemy2.types.SpatialElement` to a Shapely geometry.

    Args:
        element: The element to convert into a ``Shapely`` object.

    Example::

        lake = Session.query(Lake).get(1)
        polygon = to_shape(lake.geom)
    TF)r   ú;r   N)Ú
isinstancer   r   ÚdataÚstrÚbytesÚshapelyZwkbÚloadsÚextendedZwktÚsplit)Úelementr   r   r   r   r   Úto_shape4   s    
"ÿ
r   éÿÿÿÿc                 C   s"   t tt| |r|nddƒ||dS )aG  Function to convert a Shapely geometry to a :class:`geoalchemy2.types.WKBElement`.

    Args:
        shape: The shape to convert.
        srid: An integer representing the spatial reference system. E.g. ``4326``.
            Default value is ``-1``, which means no/unknown reference system.
        extended: A boolean to switch between WKB and EWKB.
            Default value is False.

    Example::

        from shapely.geometry import Point
        wkb_element = from_shape(Point(5, 45), srid=4326)
        ewkb_element = from_shape(Point(5, 45), srid=4326, extended=True)
    N)r   )r   r   )r   Ú
memoryviewr   )Úshaper   r   r   r   r   Ú
from_shapeL   s
    ýr    )FN)r   F)Ú__doc__Zshapely.wkbr   Zshapely.wktÚ	packagingr   Úelementsr   r   ÚparseÚ__version__Zshapely.geometry.baser   Zshapely.geosr   r   r   r   r    r   r   r   r   Ú<module>   s   