
    i>/                         d dl Zd dlmZmZmZ d dl	m
Z ddZd ZddZd Z G d	 d
          Zd dddddZd ddddZd Zd Zd Zd ZddZd Zd Zd Zd ZdS )    N)
as_bin_strtakeendian_token)wkbTc                 L    |                     t          | |                     dS )am  
    Dump GeoJSON-like `dict` to GeoPackage binary
    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.
    :param bool big_endian:
        specify endianess of the dumped object.

    :return:
    N)writedumps)obj	dest_file
big_endians      c/Users/user/workspace/sujinbaek/cqa-test-app/venv/lib/python3.11/site-packages/geomet/geopackage.pydumpr      s&     OOE#z**+++++    c                 D    t          |                                           S )a  
    Load a GeoJSON `dict` object from a ``source_file`` containing
    GeoPackage (as a byte string).

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

    :return:
        A GeoJSON `dict` representing the geometry read from the file.
    )loadsread)source_files    r   loadr   +   s     !!##$$$r   c                 \    t          | |           }t          j        | |d          }||z   S )a  
    Dump a GeoJSON-like dict to a GeoPackage bytestring.


    If the dict contains a top-level 'meta' key like so:

    ```
    'meta': {'srid': 4326}
    ```
    then the srid will be added to the geopackage header, but *not*
    to the WKB geometry header.


    If the dict contains a top-level 'bbox' key like so:

    ```
    'bbox': [0, 0, 3, 3]
    ```

    Then an envelope will be added to the geopackage header
    with this information.


    If the geometry's coordinates are empty (an empty list)
    then the geopackage header's "empty" flag will be set,
    denoting that this geometry has no coordinates.

    Please note that while this library can parse geopackages
    with a mixed byte-order in the header, it will only produce
    blobs with consistent byte order (albeit properly marked as such).
    That means you cannot product a geopackage with e.g. little-endian
    header and big-endian WKB geometry.

    :param dict obj:
        The geojson geometry to dump
    :param bool big_endian:
        if True, the geopackage binary will use big-endian
        byte order, little-endian otherwise.

    :return bytes:
        bytestring representing the geometry in geopackage
        format.
    F)include_meta)_build_geopackage_header_wkb_dumps)r
   r   headerresults       r   r	   r	   9   s6    X &cz>::F[ju===FF?r   c                    t          |           } t          t          t          j        |                     }t          |           t          |          \  }}}}}}}t          |          }	|	t          j        z
  }
t          t          |
|                     }|rt          |||          }t          j
        |           }|r!dt          |          i|d<   ddd|z  id|d<   |r||d<   |S )a  
    Construct a GeoJSON `dict` from geopackage (string).

    This function strips the geopackage header from the
    string and passes the remaining WKB geometry to the
    `geomet.wkb.loads` function.

    The envelope, if present, is added to the GeoJSON as
    a key called 'bbox' as per the GeoJSON spec, [1].

    If an SRID is specified in the geopackage header
    AND the wkb header, the SRID in the geopackage header
    will take precedence and will replace that SRID
    in the returned dict.

    [1] https://tools.ietf.org/html/rfc7946#section-5

    :param bytes string:
        geopackage byte string.
    :return dict:
        GeoJSON represented the parsed geopackage binary.
    sridmetanamezEPSG%s)type
propertiescrsbbox)iter_as_bin_str_take_GeoPackage
HEADER_LEN_check_is_valid_parse_header_get_wkb_offset_parse_enveloper   r   int)stringr   gpversionemptyenvelope_indicatoris_little_endianr   
wkb_offsetleft_to_takeenvelope_dataenveloper   s                 r   r   r   j   s   . &\\F{5v>>??FFf EAq'5,.> !!344J!77LlF ; ;<<M 
"/?
 
 ZF 
 #d)),v!8d?3
 
u
  "!vMr   c                   >    e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdZdS )r'   zy
    Much more information on geopackage structure
    can be found here: http://www.geopackage.org/spec/#gpb_format
    G   P   r      BBBBI    0   @            N)__name__
__module____qualname____doc__MAGIC1MAGIC2VERSION1r(   HEADER_PACK_FMTENVELOPE_2D_LENENVELOPE_3D_LENENVELOPE_4D_LENENVELOPE_MASKEMPTY_GEOM_MASKENDIANNESS_MASK r   r   r'   r'      sQ         
 FFHJOOOOM O OOOr   r'         r<   )r   rC         rS   rC   rU   )r   rS   rT   r<   c                     t          | dd                   \  }}}}}}}|t          j        k    s|t          j        k    rdS |t          j        k    rdS |dk     s|dk    rdS dS )	a(  
    Check if the data represents a valid geopackage
    geometry. Input can be either the full geometry or
    just the header.

    :param bytes data:
        bytes representing the geopackage binary.

    :return (bool, str):
        Is the geopackage valid, if not, string describing why
    Nr<   )Fz%Missing Geopackage header magic bytes)FzGeopackage version must be 0r   rS   )Fz&Envelope indicator must be between 0-4)T )r*   r'   rH   rI   rJ   )datar/   r0   r1   _r3   s         r   is_validr[      s     2?tBQBx1H1H.Aq'1(!Q	[Q+*<%<%<==+&&&44Q$6$:$:>>8r   c                 ^    t          j        d| dd                   \  }|t          j        z  S )a  
    Check to see if the header is encoded
    as little endian or big endian.

    Either the entire binary blob or
    just the header can be passed in.

    :param bytes header:
        geopackage header or binary blob

    :return bool: is the header little endian
    BrV   rS   )_structunpackr'   rQ   )r   flagss     r   _header_is_little_endianra      s-     ~c6!A#;//HU;...r   c                     t          |           }t          |          t          j        z   }t	          j        || dt          j                           \  }}}}}t          |          \  }}	}
|||||	|
|fS )ac  
    Unpack all information from the geopackage
    header, including "magic" GP bytes. Returns
    all of them so we can confirm that this
    geopackage is validly formed. Can also accept
    the full binary blob.

    :param header:
        the header or the full geometry.

    :return 7-tuple:
        all attributes stored in the binary header.
    N)ra   _endian_tokenr'   rK   r^   r_   r(   _parse_flags)r   r4   fmtr/   r0   r1   r`   r   r2   r3   
endiannesss              r   r*   r*      s     077
(
)
)K,G
GC!(V+[++," "Aq'5$ -9,?,?)Eza%!3ZEEr   c                 r    | t           j        z  }| t           j        z  dz	  }| t           j        z  dz	  }|||fS )a  
    Parse the bits in the "flags" byte
    of the geopackage header to retrieve
    useful information. We specifically parse
    the endianness, the envelope indicator,
    and the "empty" flag.

    Much more info can be found in
    the documentation [1].

    [1] http://www.geopackage.org/spec/#gpb_format
    :param byte flags:
        The "flags" byte of a geopackage header.

    :return tuple:
    rC   rS   )r'   rQ   rO   rP   )r`   rf   r3   r2   s       r   rd   rd     sD    " 44J+";;A[00Q6E$j00r   c                 8    d}| r|dz  dz  }|r||z  }|dz  |z  S )aU  
    Create the "flags" byte which goes into
    the geopackage header. Much more info
    can be found in the documentation [1].

    [1] http://www.geopackage.org/spec/#gpb_format

    :param int empty:
        0 or 1 indicating whether the geometry is empty.
        True and False also work as expected.
    :param int envelope_indicator:
        indicates the dimensionality of the envelope.
    :param int is_little_endian:
        0 or 1 (or False / True) indicating
        whether the header should be
        little-endian encoded.

    :return byte:
        geopackage header flags
    r   rC   rV   rR   )r2   r3   r4   r`   s       r   _build_flagsri   $  sB    * E !q  +**QJ***r   c                 B   t          | d                   dk    rdnd}|                     dg           }|                     di                               dd          }	 t          t          |                   }n# t          $ r t	          d          w xY wt
          j        t
          j        t
          j        t          |||          |g}t          |          t
          j        z   }|dt          |         z  z  }|                    |           t          j        |g|R  S )	ax  
    Create the geopackage header for the input object.
    Looks for a 'bbox' key on the geometry to use
    for an envelope, and a 'meta' key with an
    SRID to encode into the header.

    :param dict obj:
        a geojson object
    :param bool is_little_endian:
        which endianness to use when
        encoding the data.

    :return bytes: geopackage header.
    coordinatesr   rC   r#   r   r   zoBounding box must be of length 2*n where n is the number of dimensions represented in the contained geometries.d)lenget_dim_to_indicatorKeyError
ValueErrorr'   rH   rI   rJ   ri   rc   rK   _indicator_to_dimextendr^   pack)r
   r4   r2   r8   r   r3   	pack_argspack_fmts           r   r   r   B  s2     S'((A--AA1Ewwvr""H7762""61--D9.s8}}= 9 9 9 8 9 9 	99 	
 	U.0@AA
I -..1LLH ();<<=HX<-9----s   A: :Bc                 R    t          |           \  }}|st          d|z             dS )z|
    Raise if the header is not valid geopackage.

    :param bytes data: Geopackage data or header.

    :return None:
    z6Could not read Geopackage geometry because of errors: N)r[   rq   )rY   validreasons      r   r)   r)   r  sD     TNNME6 9 /178 9 9 	99 9r   c                 @    t           j        }|t          |          z  |z   S )a  
    Get the full byte offset at which the WKB geometry lies
    in the geopackage geometry.

    :param int envelope_indicator:
        indicates the dimensionality of the envelope.

    :return int:
        number of bytes until the beginning of the
        WKB geometry.

    )r'   r(   rr   )r3   base_lens     r   r+   r+     s#     %H();<<HHr   c                 p    t          |          }|dt          |          z  z  }t          j        ||          S )an  
    Parse a geopackage envelope bytestring into an n-tuple
    of floats.

    :param int envelope_indicator:
        indicates the dimensionality of the envelope.
    :param bytes envelope:
        Bytestring of the envelope values.
    :param bool is_little_endian:
        how to pack the bytes in the envelope.

    :return tuple[float]: Geometry envelope.
    rl   )rc   rr   r^   r_   )r3   r8   r4   rv   s       r   r,   r,     s:     -..H();<<=H>(H---r   )T)rC   )structr^   geomet.utilr   r%   r   r&   r   rc   geometr   r   r   r   r	   r   r'   rr   ro   r[   ra   r*   rd   ri   r   r)   r+   r,   rR   r   r   <module>r      s               
      , , , ,$% % %. . . .b5 5 5p! ! ! ! ! ! ! !2 	   		    ,/ / /"F F F01 1 1.+ + + +<-. -. -.`9 9 9I I I". . . . .r   