o
    Dfy                  	   @   s   d Z ddlZddlmZ ddlmZmZ ddlmZ dedefdd	Z	dedefd
dZ
G dd dejZG dd deZdedeeef fddZddededeeef fddZdededefddZdS )z&
Utility functions used within pyproj
    N)array)Enumauto)Anyvaluereturnc                 C   s   | | kp| du S )z'
    Check if value is NaN or None
    N r   r   r   E/home/ubuntu/webapp/venv/lib/python3.10/site-packages/pyproj/utils.pyis_null
   s   r   c                 C   s4   t |  } | dv rdS | dv rdS td|  d)z
    https://docs.python.org/3.9/distutils/apiref.html#distutils.util.strtobool

    Here since distutils is deprecated.

    Convert a string representation of truth to True or False.
    )yyesttrueon1T)nnoffalseoff0Fzinvalid truth value: '')strlower
ValueErrorr	   r   r   r
   	strtobool   s   r   c                   @   s   e Zd ZdZdd ZdS )NumpyEncoderz1
    Handle numpy types when dumping to JSON
    c                 C   sp   z|  W S  ty   Y nw z|jjdkrt|W S |jjdkr&t|W S W n	 ty0   Y nw tj| |S )Nr   i)	tolistAttributeErrordtypekindfloatintjsonJSONEncoderdefault)selfobjr   r   r
   r'   '   s   


zNumpyEncoder.defaultN)__name__
__module____qualname____doc__r'   r   r   r   r
   r   "   s    r   c                   @   s(   e Zd ZdZe Ze Ze Ze ZdS )DataTypezA
    Data type for copy to buffer and convertback operations
    N)	r*   r+   r,   r-   r   FLOATLISTTUPLEARRAYr   r   r   r
   r.   7   s    
r.   xxxc                 C   s2   zt dt| ftjfW S  ty   tddw )a  
    Prepares scalar for PROJ C-API:
    - Makes a copy because PROJ modifies buffer in place
    - Make sure dtype is double as that is what PROJ expects
    - Makes sure object supports Python Buffer API

    Parameters
    -----------
    xxx: float or 0-d numpy array

    Returns
    -------
    tuple[Any, DataType]
        The copy of the data prepared for the PROJ API & Python Buffer API.
    dzinput must be a scalarN)r   r#   r.   r/   	Exception	TypeError)r3   r   r   r
   _copytobuffer_return_scalarB   s
   
r7   Finplacec                 C   s   t | dst | drt| jr|  } t | dr-| jdkr!t| S | jdd| dtjfS tj}t| t	rE|r<| j
dkrAt	d| } | |fS t| trVt	d| } tj}| |fS t| trgt	d| } tj}| |fS t| S )a  
    Prepares data for PROJ C-API:
    - Makes a copy because PROJ modifies buffer in place
    - Make sure dtype is double as that is what PROJ expects
    - Makes sure object supports Python Buffer API

    If the data is a numpy array, it ensures the data is in C order.

    Parameters
    ----------
    xxx: Any
        A scalar, list, tuple, numpy.array,
        pandas.Series, xaray.DataArray, or dask.array.Array.
    inplace: bool, default=False
        If True, will return the array without a copy if it
        meets the requirements of the Python Buffer API & PROJ C-API.

    Returns
    -------
    tuple[Any, DataType]
        The copy of the data prepared for the PROJ API & Python Buffer API.
    hardmask	__array__shaper   r4   C)ordercopy)hasattrcallabler:   r;   r7   astyper.   r2   
isinstancer   typecodelistr0   tupler1   )r3   r8   	data_typer   r   r
   _copytobufferX   s2   



	



rG   rF   inxc                 C   s:   | t jkr	|d S | t jkr| S | t jkrt|S |S )Nr   )r.   r/   r0   r   r1   rE   )rF   rH   r   r   r
   _convertback   s   


rI   )F)r-   r%   r   enumr   r   typingr   boolr   r   r&   r   r.   rE   r7   rG   rI   r   r   r   r
   <module>   s     9