o
    EDi,F                  	   @   sP  d dl Z d dlZd dlZd dlZzd dlZW n ey%   d dlZeZY nw d dl mZ dZdd Z	dd Z
d:d	d
Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zeeeeeeed1Zeeeeeeed2Z e!d3d4d5d6d7d8d9d2Z"dS );    N)utilzInvalid WKT: `%s`c                 C   s   | t|  dS )a  
    Dump GeoJSON-like `dict` to WKT and write it to the `dest_file`.

    :param dict obj:
        A GeoJSON-like dictionary. It must at least the keys 'type' and
        'coordinates'.
    :param dest_file:
        Open and writable file-like object.
    N)writedumps)obj	dest_file r   C/var/www/Datamplify/venv/lib/python3.10/site-packages/geomet/wkt.pydump   s   
r	   c                 C   s   t |  S )z
    Load a GeoJSON `dict` object from a ``source_file`` containing WKT.

    :param source_file:
        Open and readable file-like object.

    :returns:
        A GeoJSON `dict` representing the geometry read from the file.
    )loadsread)source_filer   r   r   load,   s   
r      c                 C   s&  z7| d }t |}|du rt| |dkr"t| d dkr!W dS nttt| d dkr6d|  W S W n tyE   t	
d	|  w || |}| d
i d}| di di d}|durk|dd}|dur|durt|t|krtd||f |p|}|durd||f }|S )z5
    Dump a GeoJSON-like `dict` to a WKT string.
    typeNGeometryCollection
geometriesr   zGEOMETRYCOLLECTION EMPTYcoordinatesz%s EMPTYzInvalid GeoJSON: %smetasridcrs
propertiesnameEPSG z$Ambiguous CRS/SRID values: %s and %sz
SRID=%s;%s)_dumps_registryget_unsupported_geom_typelenlistr   flatten_multi_dimupperKeyErrorgeometInvalidGeoJSONExceptionreplacestr
ValueError)r   decimals	geom_typeexporterresult	meta_sridcrs_sridr   r   r   r   r   9   s<   


r   c                 C   s$   t | }||kstd||f d S )NzExpected "%s" but found "%s")nextr&   )sequenceexpected
next_tokenr   r   r   _assert_next_tokeng   s   
r1   c           	      C   s   t  | }dd t|jD }t|}t|}d}|}|dkr5t|d tt|}t|d t|}n|}t	|}|du rDt
| t|}|dkr_|dkrWtd	g d
S tt| g dS t|g|}||| }|durvt|d|d< |S )z9
    Construct a GeoJSON `dict` from WKT (`string`).
    c                 s   s    | ]}|d  V  qdS )   Nr   ).0xr   r   r   	<genexpr>u       zloads.<locals>.<genexpr>NSRID=;EMPTYGEOMETRYCOLLECTIONr   r   r   r   r   )r   r   )StringIOtokenizegenerate_tokensreadline_tokenize_wktr-   r1   int_loads_registryr   r   sixadvance_iteratordict_type_map_caps_to_mixed	itertoolschain)	stringsiotokensgeom_type_or_sridr   r(   importerpeekr*   r   r   r   r
   o   s6   






r
   c                 c   s<    d}| D ]}|dkrd}q|rd| V  n|V  d}qdS )z
    Since the tokenizer treats "-" and numeric strings as separate values,
    combine them and yield them as a single token. This utility encapsulates
    parsing of negative numeric values from WKT can be used generically in all
    parsers.
    F-Tz-%sNr   )rM   negativetr   r   r   rB      s   rB   c                 C   s   t d|  )NzUnsupported geometry type '%s')r&   )r(   r   r   r   r      s   r   c                 C   sf   t | tr|dkrt| } n|dkrttt| |S tt| |}|d|t|dd   7 }|S )a;  
    Round the input value to `decimals` places, and pad with 0's
    if the resulting value is less than `decimals`.

    :param value:
        The value to round
    :param decimals:
        Number of decimals places which should be displayed after the rounding.
    :return:
        str of the rounded value
    r   0.r2   )
isinstancerC   floatreprroundr   split)valuer'   roundedr   r   r   _round_and_pad   s   
r]   c                    s(   | d }dd  fdd|D  }|S )a\  
    Dump a GeoJSON-like Point object to WKT.

    :param dict obj:
        A GeoJSON-like `dict` representing a Point.
    :param int decimals:
        int which indicates the number of digits to display after the
        decimal point when formatting coordinates.

    :returns:
        WKT representation of the input GeoJSON Point ``obj``.
    r   z
POINT (%s) c                 3       | ]}t | V  qd S Nr]   r3   cr'   r   r   r5          z_dump_point.<locals>.<genexpr>join)r   r'   coordsptr   rd   r   _dump_point   s
   
rj   c                    s,   | d }d}|d  fdd|D ; }|S )z
    Dump a GeoJSON-like LineString object to WKT.

    Input parameters and return value are the LINESTRING equivalent to
    :func:`_dump_point`.
    r   zLINESTRING (%s), c                 3   (    | ]}d   fdd|D V  qdS )r^   c                 3   r_   r`   ra   rb   rd   r   r   r5      re   z-_dump_linestring.<locals>.<genexpr>.<genexpr>Nrf   r3   ri   rd   r   r   r5          z#_dump_linestring.<locals>.<genexpr>rf   )r   r'   rh   lsr   rd   r   _dump_linestring   s   
rp   c                    >   | d }d} fdd|D }dd |D }|d |; }|S )z
    Dump a GeoJSON-like Polygon object to WKT.

    Input parameters and return value are the POLYGON equivalent to
    :func:`_dump_point`.
    r   zPOLYGON (%s)c                 3   rl   )rk   c                 3   rl   )r^   c                 3   r_   r`   ra   rb   rd   r   r   r5      re   z4_dump_polygon.<locals>.<genexpr>.<genexpr>.<genexpr>Nrf   rm   rd   r   r   r5      rn   z*_dump_polygon.<locals>.<genexpr>.<genexpr>Nrf   r3   ringrd   r   r   r5      s    z _dump_polygon.<locals>.<genexpr>c                 s       | ]}d | V  qdS (%s)Nr   )r3   rr   r   r   r5      r6   rk   rf   )r   r'   rh   polyringsr   rd   r   _dump_polygon   s   
rz   c                    rq   )z
    Dump a GeoJSON-like MultiPoint object to WKT.

    Input parameters and return value are the MULTIPOINT equivalent to
    :func:`_dump_point`.
    r   zMULTIPOINT (%s)c                 3   rl   )r^   c                 3   r_   r`   ra   rb   rd   r   r   r5     re   z-_dump_multipoint.<locals>.<genexpr>.<genexpr>Nrf   rm   rd   r   r   r5     rn   z#_dump_multipoint.<locals>.<genexpr>c                 s   rt   ru   r   rm   r   r   r   r5   	  r6   rk   rf   )r   r'   rh   mppointsr   rd   r   _dump_multipoint   s   
r}   c                    s:   | d }d} fdd|D }|d dd |D ; }|S )z
    Dump a GeoJSON-like MultiLineString object to WKT.

    Input parameters and return value are the MULTILINESTRING equivalent to
    :func:`_dump_point`.
    r   zMULTILINESTRING (%s)c                 3   ,    | ]}d d  fdd|D  V  qdS )rv   rk   c                 3   rl   )r^   c                 3   r_   r`   ra   rb   rd   r   r   r5     re   z<_dump_multilinestring.<locals>.<genexpr>.<genexpr>.<genexpr>Nrf   rm   rd   r   r   r5     rn   z2_dump_multilinestring.<locals>.<genexpr>.<genexpr>Nrf   )r3   linestrrd   r   r   r5     s    z(_dump_multilinestring.<locals>.<genexpr>rk   c                 s   s    | ]}|V  qd S r`   r   )r3   ro   r   r   r   r5     s    rf   )r   r'   rh   mllslinestrsr   rd   r   _dump_multilinestring  s   
r   c                    s0   | d }d}d  fdd|D }||; }|S )z
    Dump a GeoJSON-like MultiPolygon object to WKT.

    Input parameters and return value are the MULTIPOLYGON equivalent to
    :func:`_dump_point`.
    r   zMULTIPOLYGON (%s)rk   c                 3   r~   )rv   rk   c                 3   r~   )rv   rk   c                 3   rl   )r^   c                 3   r_   r`   ra   rb   rd   r   r   r5   1  s    zC_dump_multipolygon.<locals>.<genexpr>.<genexpr>.<genexpr>.<genexpr>Nrf   rm   rd   r   r   r5   /  s
    
z9_dump_multipolygon.<locals>.<genexpr>.<genexpr>.<genexpr>Nrf   rr   rd   r   r   r5   ,  s    
z/_dump_multipolygon.<locals>.<genexpr>.<genexpr>Nrf   )r3   rx   rd   r   r   r5   )  s    
z%_dump_multipolygon.<locals>.<genexpr>rf   )r   r'   rh   r{   polysr   rd   r   _dump_multipolygon  s   r   c                 C   sJ   d}| d }g }|D ]}|d }| t||| q
|d|; }|S )a  
    Dump a GeoJSON-like GeometryCollection object to WKT.

    Input parameters and return value are the GEOMETRYCOLLECTION equivalent to
    :func:`_dump_point`.

    The WKT conversions for each geometry in the collection are delegated to
    their respective functions.
    zGEOMETRYCOLLECTION (%s)r   r   ,)appendr   r   rg   )r   r'   gcgeoms	geoms_wktgeomr(   r   r   r   _dump_geometrycollection:  s   
r   c                 C   sn   t | dkstt| g }z| D ]}|dkr n|t| qW n tjy0   tt| w td|dS )a  
    :param tokens:
        A generator of string tokens for the input WKT, begining just after the
        geometry type. The geometry type is consumed before we get to here. For
        example, if :func:`loads` is called with the input 'POINT(0.0 1.0)',
        ``tokens`` would generate the following values:

        .. code-block:: python
            ['(', '0.0', '1.0', ')']
    :param str string:
        The original WKT string.

    :returns:
        A GeoJSON `dict` Point representation of the WKT ``string``.
    ()Pointr=   r-   r&   INVALID_WKT_FMTr   rW   r?   
TokenErrorrG   )rM   rK   rh   rS   r   r   r   _load_pointN  s   r   c                 C   s   t | dkstt| g }z'g }| D ] }|dkr ||  n|dkr,|| g }q|t| qW n tjyC   tt| w td|dS )z
    Has similar inputs and return value to to :func:`_load_point`, except is
    for handling LINESTRING geometry.

    :returns:
        A GeoJSON `dict` LineString representation of the WKT ``string``.
    r   r   r   
LineStringr=   r   )rM   rK   rh   ri   rS   r   r   r   _load_linestringn  s$   

r   c                 C   s   t | t | f}|dkstt| g }g }d}zKg }| D ]D}|dkr2|r2|| || d}q|dkr:|s: n(|dkrEg }g }d}q|dkrS|rS|| g }q|dkrZ|sZq|t| qW n tjyq   tt| w td|dS )	z
    Has similar inputs and return value to to :func:`_load_point`, except is
    for handling POLYGON geometry.

    :returns:
        A GeoJSON `dict` Polygon representation of the WKT ``string``.
    )r   r   Tr   Fr   r   Polygonr=   r   )rM   rK   open_parensrh   rs   on_ringri   rS   r   r   r   _load_polygon  s<   


r   c                 C   s   t | }|dkstt| g }g }d}z7| D ]2}|dkr"|d7 }q|dkr1|d8 }|dkr0 nq|dkr6q|dkrB|| g }q|t| qW n tjyY   tt| w t|dkre|| td|dS )	z
    Has similar inputs and return value to to :func:`_load_point`, except is
    for handling MULTIPOINT geometry.

    :returns:
        A GeoJSON `dict` MultiPoint representation of the WKT ``string``.
    r   r2   r   r   r   r   
MultiPointr=   )	r-   r&   r   r   rW   r?   r   r   rG   )rM   rK   
open_parenrh   ri   paren_depthrS   r   r   r   _load_multipoint  s8   


r   c                 C   |   t | }|dkstt| g }	 zt| |}||d  t | }|dkr(W nW n ty6   tt| w qtd|dS )z
    Has similar inputs and return value to to :func:`_load_point`, except is
    for handling MULTIPOLYGON geometry.

    :returns:
        A GeoJSON `dict` MultiPolygon representation of the WKT ``string``.
    r   Tr   r   MultiPolygonr=   )r-   r&   r   r   r   StopIterationrG   )rM   rK   r   polygonsrx   rS   r   r   r   _load_multipolygon  "   
r   c                 C   r   )z
    Has similar inputs and return value to to :func:`_load_point`, except is
    for handling MULTILINESTRING geometry.

    :returns:
        A GeoJSON `dict` MultiLineString representation of the WKT ``string``.
    r   Tr   r   MultiLineStringr=   )r-   r&   r   r   r   r   rG   )rM   rK   r   r   r   rS   r   r   r   _load_multilinestring  r   r   c           	      C   s   t | }|dkstt| g }td|d}	 z$t | }|dkr#W |S |dkr)W q|}t|}|| |}|| W n tyH   tt| w q)a*  
    Has similar inputs and return value to to :func:`_load_point`, except is
    for handling GEOMETRYCOLLECTIONs.

    Delegates parsing to the parsers for the individual geometry types.

    :returns:
        A GeoJSON `dict` GeometryCollection representation of the WKT
        ``string``.
    r   r   r<   Tr   r   )r-   r&   r   rG   rD   r   r   r   )	rM   rK   r   r   r*   rS   r(   	load_funcr   r   r   r   _load_geometrycollection$  s*   

r   )r   r   r   r   r   r   r   )POINT
LINESTRINGPOLYGON
MULTIPOINTMULTILINESTRINGMULTIPOLYGONr;   r   r   r   r   r   r   r   )r   )#r"   rI   rE   r?   r>   ImportErrorior   r   r	   r   r   r1   r
   rB   r   r]   rj   rp   rz   r}   r   r   r   r   r   r   r   r   r   r   r   rD   rG   rH   r   r   r   r   <module>   sv   
.*  3+$

