a
    1$eh^                     @   s  d dl mZ d dlZd dlZd dlZd dlmZmZ d dl	m
Z d dl	mZ d dlmZ d dlmZ d dlZddlmZ d	Zg 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dZd,ddZd-ddZd.d d!Zd/d"d#Z d$d% Z!d0d&d'Z"d1d(d)Z#dS )2    )VersionN)	DataFrameSeries)import_optional_dependency)from_wkb)GeoDataFrame   )_expand_user1.0.0-beta.1)0.1.0z0.4.0r
   c                 C   s   t | tod| v o| d S )Nz://)zhttp://zhttps://)
isinstancestr
startswith)url r   N/var/www/html/django/DPS/env/lib/python3.9/site-packages/geopandas/io/arrow.py_is_fsspec_url-   s
    

r   c                 C   sR   |   D ]D\}}t|tr$t| q|dkrt|tr|D ]}|dd q:qdS )aL  
    Older PROJ versions will not recognize IDs of datum ensemble members that
    were added in more recent PROJ database versions.

    Cf https://github.com/opengeospatial/geoparquet/discussions/110
    and https://github.com/OSGeo/PROJ/pull/3221

    Mimicking the patch to GDAL from https://github.com/OSGeo/gdal/pull/5872
    membersidN)itemsr   dict#_remove_id_from_member_of_ensembleslistpop)Z	json_dictkeyvaluememberr   r   r   r   5   s    


r   c           	      C   s
  |pt }|tvr$tddt i }| j| jdk D ]}| | }tt|j	 
 }|d dkrd}t|dkr|d }nd}d	}|jr|d
kr|j }n|j }t| ddd|||i||< |j }t| r8||| d< q8| j||pt dtjddS )a*  Create and encode geo metadata dict.

    Parameters
    ----------
    df : GeoDataFrame
    schema_version : {'0.1.0', '0.4.0', '1.0.0-beta.1', None}
        GeoParquet specification version; if not provided will default to
        latest supported version.

    Returns
    -------
    dict
    zschema_version must be one of: z, geometryr   0Zgeometry_typer   geometry_typesNr   encodingWKBcrsbbox	geopandas)Zlibraryversion)primary_columncolumnsr%   Zcreator)METADATA_VERSIONSUPPORTED_VERSIONS
ValueErrorjoinr'   dtypessortedr   Z	geom_typeuniqueZdropnalenr"   Zto_wktZto_json_dictr   Ztotal_boundstolistnpisfiniteallZ_geometry_column_namer$   __version__)	dfschema_versioncolumn_metadatacolseriesr   Zgeometry_types_namer"   r#   r   r   r   _create_metadataG   s@    



r:   c                 C   s   t | dS )zEncode metadata dict to UTF-8 JSON string

    Parameters
    ----------
    metadata : dict

    Returns
    -------
    UTF-8 encoded JSON string
    utf-8)jsondumpsencode)metadatar   r   r   _encode_metadata   s    r@   c                 C   s   | du rdS t | dS )zDecode a UTF-8 encoded JSON string to dict

    Parameters
    ----------
    metadata_str : string (UTF-8 encoded)

    Returns
    -------
    dict
    Nr;   )r<   loadsdecode)Zmetadata_strr   r   r   _decode_metadata   s    rC   c                 C   sL   t | tstd| jjdvr&tdtdd | jjD }|sHtddS )zValidate that the GeoDataFrame conforms to requirements for writing
    to Parquet format.

    Raises `ValueError` if the GeoDataFrame is not valid.

    copied from `pandas.io.parquet`

    Parameters
    ----------
    df : GeoDataFrame
    z;Writing to Parquet/Feather only supports IO with DataFrames>   unicodeemptystringz7Writing to Parquet/Feather requires string column namesc                 s   s    | ]}|d urt |tV  qd S )N)r   r   ).0namer   r   r   	<genexpr>   s   z&_validate_dataframe.<locals>.<genexpr>z!Index level names must be stringsN)r   r   r*   r'   Zinferred_typer3   indexnames)r5   Zvalid_namesr   r   r   _validate_dataframe   s    
rL   c                 C   s   | st d| d| d}|s*t dd}|D ]$}| |ddu r2t dj|dq2t| d	 tsnt d
d}| d	  D ]h\}}|D ]}||vrt dj||dq|d dkrt d|dddkr~tjd| dtdd q~dS )zValidate geo metadata.
    Must not be empty, and must contain the structure specified above.

    Raises ValueError if metadata is not valid.

    Parameters
    ----------
    metadata : dict
    9Missing or malformed geo metadata in Parquet/Feather filer%   r6   zI'geo' metadata in Parquet/Feather file is missing required key: 'version')r&   r'   NzG'geo' metadata in Parquet/Feather file is missing required key: '{key}')r   r'   z*'columns' in 'geo' metadata must be a dict)r    zY'geo' metadata in Parquet/Feather file is missing required key '{key}' for column '{col}')r   r8   r    r!   z'Only WKB geometry encoding is supportededgesZplanarZ	sphericalz'The geo metadata indicate that column 'z' has spherical edges, but because GeoPandas currently does not support spherical geometry, it ignores this metadata and will interpret the edges of the geometries as planar.   
stacklevel)	r*   getformatr   r   r   warningswarnUserWarning)r?   r%   required_keysr   Zrequired_col_keysr8   r7   r   r   r   _validate_metadata   sD    
rX   c           
      C   s   ddl m} t|  t| |d}i }tjr4ddi}n:| j| jdk D ](}| | }|j	 rDt
jddd	  qnqD| jf i |} |j| |d
}|jj}	|	dt|i ||	S )zL
    Helper function with main, shared logic for to_parquet/to_feather.
    r   )Table)r6   ZflavorZisor   zThe GeoDataFrame contains 3D geometries, and when using shapely < 2.0, such geometries will be written not exactly following to the GeoParquet spec (not using ISO WKB). For most use cases this should not be a problem (GeoPandas can read such files fine).   rP   )Zpreserve_index   geo)pyarrowrY   rL   r:   compatZUSE_SHAPELY_20r'   r,   Zhas_zanyrT   rU   Zto_wkbZfrom_pandasschemar?   updater@   Zreplace_schema_metadata)
r5   rJ   r6   rY   Zgeo_metadatakwargsr8   r9   tabler?   r   r   r   _geopandas_to_arrow   s&    

rc   snappyc                 K   s   t ddd}|rRd|v rR|d durR|du rR|d tv rRtjdtdd |d}t|}t| ||d	}|j||fd
|i| dS )a  
    Write a GeoDataFrame to the Parquet format.

    Any geometry columns present are serialized to WKB format in the file.

    Requires 'pyarrow'.

    This is tracking version 1.0.0-beta.1 of the GeoParquet specification at:
    https://github.com/opengeospatial/geoparquet. Writing older versions is
    supported using the `schema_version` keyword.

    .. versionadded:: 0.8

    Parameters
    ----------
    path : str, path object
    index : bool, default None
        If ``True``, always include the dataframe's index(es) as columns
        in the file output.
        If ``False``, the index(es) will not be written to the file.
        If ``None``, the index(ex) will be included as columns in the file
        output except `RangeIndex` which is stored as metadata only.
    compression : {'snappy', 'gzip', 'brotli', None}, default 'snappy'
        Name of the compression to use. Use ``None`` for no compression.
    schema_version : {'0.1.0', '0.4.0', '1.0.0-beta.1', None}
        GeoParquet specification version; if not provided will default to
        latest supported version.
    **kwargs
        Additional keyword arguments passed to pyarrow.parquet.write_table().
    pyarrow.parquet(pyarrow is required for Parquet support.extrar%   Nzthe `version` parameter has been replaced with `schema_version`. `version` will instead be passed directly to the underlying parquet writer unless `version` is 0.1.0 or 0.4.0.rZ   rP   rJ   r6   compression)	r   r)   rT   rU   FutureWarningr   r	   rc   Zwrite_table)r5   pathrJ   rj   r6   ra   parquetrb   r   r   r   _to_parquet   s    !
rn   c           	      K   s   t ddd}ddl}t|jtdk r.td|rtd|v rt|d durt|du rt|d tv rttjd	td
d |	d}t
|}t| ||d}|j||fd|i| dS )a  
    Write a GeoDataFrame to the Feather format.

    Any geometry columns present are serialized to WKB format in the file.

    Requires 'pyarrow' >= 0.17.

    This is tracking version 1.0.0-beta.1 of the GeoParquet specification for
    the metadata at: https://github.com/opengeospatial/geoparquet. Writing
    older versions is supported using the `schema_version` keyword.

    .. versionadded:: 0.8

    Parameters
    ----------
    path : str, path object
    index : bool, default None
        If ``True``, always include the dataframe's index(es) as columns
        in the file output.
        If ``False``, the index(es) will not be written to the file.
        If ``None``, the index(ex) will be included as columns in the file
        output except `RangeIndex` which is stored as metadata only.
    compression : {'zstd', 'lz4', 'uncompressed'}, optional
        Name of the compression to use. Use ``"uncompressed"`` for no
        compression. By default uses LZ4 if available, otherwise uncompressed.
    schema_version : {'0.1.0', '0.4.0', '1.0.0-beta.1', None}
        GeoParquet specification version for the metadata; if not provided
        will default to latest supported version.
    kwargs
        Additional keyword arguments passed to pyarrow.feather.write_feather().
    pyarrow.feather(pyarrow is required for Feather support.rg   r   N0.17.0,pyarrow >= 0.17 required for Feather supportr%   zthe `version` parameter has been replaced with `schema_version`. `version` will instead be passed directly to the underlying feather writer unless `version` is 0.1.0 or 0.4.0.rZ   rP   ri   rj   )r   r\   r   r4   ImportErrorr)   rT   rU   rk   r   r	   rc   Zwrite_feather)	r5   rl   rJ   rj   r6   ra   featherr\   rb   r   r   r   _to_featherU  s"     
ru   c              	   C   s&  |   }|p| jj}|du s$d|vr,tdzt|dd}W n" ttjj	fyb   tdY n0 t
| |j|d }t|std|d }t|r||vr|d	 }t|d
krtjddd |D ]N}|d | }d|v r|d }t|trt| nd}t|| j|d||< qt||dS )zP
    Helper function with main, shared logic for read_parquet/read_feather.
    Nr[   ziMissing geo metadata in Parquet/Feather file.
            Use pandas.read_parquet/read_feather() instead.    rM   r'   zNo geometry columns are included in the columns read from
            the Parquet/Feather file.  To read this file without geometry columns,
            use pandas.read_parquet/read_feather() instead.r&   r   r   zMultiple non-primary geometry columns read from Parquet/Feather file. The first column read was promoted to the primary geometry.   rP   r"   z	OGC:CRS84)r"   )r   )Z	to_pandasr_   r?   r*   rC   rR   	TypeErrorr<   decoderJSONDecodeErrorrX   r'   intersectionr/   rT   rU   r   r   r   r   valuesr   )rb   r?   r5   Zgeometry_columnsr   r8   Zcol_metadatar"   r   r   r   _arrow_to_geopandas  sB    

r}   c                 C   s   ddl }t| trf|du rf|du rft|jtdkrfzddlm} || \}} W n tyd   Y n0 t	| r|du rt
ddd}|jj| fi |pi \}} |du r|rtd| || fS )	z
    Get the filesystem and path for a given filesystem and path.

    If the filesystem is not None then it's just returned as is.
    r   Nz5.0.0)
FileSystemfsspecz(fsspec is requred for 'storage_options'.rg   z:Cannot provide 'storage_options' with non-fsspec path '{}')r\   r   r   r   r4   Z
pyarrow.fsr~   Zfrom_uri	Exceptionr   r   coreZ	url_to_fsr*   rS   )rl   
filesystemstorage_optionsr\   r~   r   r   r   r   _get_filesystem_path  s0    r   c                 C   s\   ddl m} t| |jr| S zddl}W n ty:   Y n0 t| |jrX||| S | S )z
    Simplified version of pyarrow.fs._ensure_filesystem. This is only needed
    below because `pyarrow.parquet.read_metadata` does not yet accept a
    filesystem keyword (https://issues.apache.org/jira/browse/ARROW-16719)
    r   )fsN)	r\   r   r   r~   r   rs   ZAbstractFileSystemZPyFileSystemZFSSpecHandler)r   r   r   r   r   r   _ensure_arrow_fs  s    r   c           
      K   s   t ddd}|dd}t| ||d\}} t| } d|d< |j| f||d	|}d}|jjdu snd
|jjvrzX|durt|}|| }	|	|	j}W d   q1 s0    Y  n|	| j}W n t
y   Y n0 t||S )a	  
    Load a Parquet object from the file path, returning a GeoDataFrame.

    You can read a subset of columns in the file using the ``columns`` parameter.
    However, the structure of the returned GeoDataFrame will depend on which
    columns you read:

    * if no geometry columns are read, this will raise a ``ValueError`` - you
      should use the pandas `read_parquet` method instead.
    * if the primary geometry column saved to this file is not included in
      columns, the first available geometry column will be set as the geometry
      column of the returned GeoDataFrame.

    Supports versions 0.1.0, 0.4.0 and 1.0.0-beta.1 of the GeoParquet
    specification at: https://github.com/opengeospatial/geoparquet

    If 'crs' key is not present in the GeoParquet metadata associated with the
    Parquet object, it will default to "OGC:CRS84" according to the specification.

    Requires 'pyarrow'.

    .. versionadded:: 0.8

    Parameters
    ----------
    path : str, path object
    columns : list-like of strings, default=None
        If not None, only these columns will be read from the file.  If
        the primary geometry column is not included, the first secondary
        geometry read from the file will be set as the geometry column
        of the returned GeoDataFrame.  If no geometry columns are present,
        a ``ValueError`` will be raised.
    storage_options : dict, optional
        Extra options that make sense for a particular storage connection, e.g. host,
        port, username, password, etc. For HTTP(S) URLs the key-value pairs are
        forwarded to urllib as header options. For other URLs (e.g. starting with
        "s3://", and "gcs://") the key-value pairs are forwarded to fsspec. Please
        see fsspec and urllib for more details.

        When no storage options are provided and a filesystem is implemented by
        both ``pyarrow.fs`` and ``fsspec`` (e.g. "s3://") then the ``pyarrow.fs``
        filesystem is preferred. Provide the instantiated fsspec filesystem using
        the ``filesystem`` keyword if you wish to use its implementation.
    **kwargs
        Any additional kwargs passed to pyarrow.parquet.read_table().

    Returns
    -------
    GeoDataFrame

    Examples
    --------
    >>> df = geopandas.read_parquet("data.parquet")  # doctest: +SKIP

    Specifying columns to read:

    >>> df = geopandas.read_parquet(
    ...     "data.parquet",
    ...     columns=["geometry", "pop_est"]
    ... )  # doctest: +SKIP
    re   rf   rg   r   N)r   r   TZuse_pandas_metadata)r'   r   r[   )r   r   r   r	   
read_tabler_   r?   r   Zopen_input_fileZread_metadatar   r}   )
rl   r'   r   ra   rm   r   rb   r?   Zpa_filesystemsourcer   r   r   _read_parquet  s*    ?
,r   c                 K   sT   t ddd}ddl}t|jtdk r.tdt| } |j| fd|i|}t|S )	a  
    Load a Feather object from the file path, returning a GeoDataFrame.

    You can read a subset of columns in the file using the ``columns`` parameter.
    However, the structure of the returned GeoDataFrame will depend on which
    columns you read:

    * if no geometry columns are read, this will raise a ``ValueError`` - you
      should use the pandas `read_feather` method instead.
    * if the primary geometry column saved to this file is not included in
      columns, the first available geometry column will be set as the geometry
      column of the returned GeoDataFrame.

    Supports versions 0.1.0, 0.4.0 and 1.0.0-beta.1 of the GeoParquet
    specification at: https://github.com/opengeospatial/geoparquet

    If 'crs' key is not present in the Feather metadata associated with the
    Parquet object, it will default to "OGC:CRS84" according to the specification.

    Requires 'pyarrow' >= 0.17.

    .. versionadded:: 0.8

    Parameters
    ----------
    path : str, path object
    columns : list-like of strings, default=None
        If not None, only these columns will be read from the file.  If
        the primary geometry column is not included, the first secondary
        geometry read from the file will be set as the geometry column
        of the returned GeoDataFrame.  If no geometry columns are present,
        a ``ValueError`` will be raised.
    **kwargs
        Any additional kwargs passed to pyarrow.feather.read_table().

    Returns
    -------
    GeoDataFrame

    Examples
    --------
    >>> df = geopandas.read_feather("data.feather")  # doctest: +SKIP

    Specifying columns to read:

    >>> df = geopandas.read_feather(
    ...     "data.feather",
    ...     columns=["geometry", "pop_est"]
    ... )  # doctest: +SKIP
    ro   rp   rg   r   Nrq   rr   r'   )r   r\   r   r4   rs   r	   r   r}   )rl   r'   ra   rt   r\   rb   r   r   r   _read_featherm  s    4r   )N)NN)Nrd   N)NNN)N)NN)NN)N)$Zpackaging.versionr   r<   rT   numpyr1   Zpandasr   r   Zgeopandas._compat_compatr]   r   Zgeopandas.arrayr   r$   r   filer	   r(   r)   r   r   r:   r@   rC   rL   rX   rc   rn   ru   r}   r   r   r   r   r   r   r   r   <module>   s8   
=:
( 
5
9
A
&
a