a
    û1$e  ã                   @   s`   d dl mZ d dlZd dlZd dlmZ d dlZdd„ Zddd„Z	ddd	„Z
d
d„ Zdd„ ZdS )é    )ÚdefaultdictN)ÚPointc                 C   s    ddl }| |jjkrdS dS dS )z‹
    Amount of time to wait between requests to a geocoding API, for providers
    that specify rate limits in their terms of service.
    r   Né   )Úgeopy.geocodersZ	geocodersZ	Nominatim)ÚproviderZgeopy© r   úU/var/www/html/django/DPS/env/lib/python3.9/site-packages/geopandas/tools/geocoding.pyÚ_get_throttle_time   s    r	   c                 K   s*   |du rd}t |ƒ}t| d||fi |¤ŽS )ab  
    Geocode a set of strings and get a GeoDataFrame of the resulting points.

    Parameters
    ----------
    strings : list or Series of addresses to geocode
    provider : str or geopy.geocoder
        Specifies geocoding service to use. If none is provided,
        will use 'photon' (see the Photon's terms of service at:
        https://photon.komoot.io).

        Either the string name used by geopy (as specified in
        geopy.geocoders.SERVICE_TO_GEOCODER) or a geopy Geocoder instance
        (e.g., geopy.geocoders.Photon) may be used.

        Some providers require additional arguments such as access keys
        See each geocoder's specific parameters in geopy.geocoders

    Notes
    -----
    Ensure proper use of the results by consulting the Terms of Service for
    your provider.

    Geocoding requires geopy. Install it using 'pip install geopy'. See also
    https://github.com/geopy/geopy

    Examples
    --------
    >>> df = geopandas.tools.geocode(  # doctest: +SKIP
    ...         ["boston, ma", "1600 pennsylvania ave. washington, dc"]
    ...     )
    >>> df  # doctest: +SKIP
                        geometry                                            address
    0  POINT (-71.05863 42.35899)                          Boston, MA, United States
    1  POINT (-77.03651 38.89766)  1600 Pennsylvania Ave NW, Washington, DC 20006...
    NÚphotonT©r	   Ú_query)Ústringsr   ÚkwargsÚthrottle_timer   r   r   Úgeocode   s    &r   c                 K   s*   |du rd}t |ƒ}t| d||fi |¤ŽS )a  
    Reverse geocode a set of points and get a GeoDataFrame of the resulting
    addresses.

    The points

    Parameters
    ----------
    points : list or Series of Shapely Point objects.
        x coordinate is longitude
        y coordinate is latitude
    provider : str or geopy.geocoder (opt)
        Specifies geocoding service to use. If none is provided,
        will use 'photon' (see the Photon's terms of service at:
        https://photon.komoot.io).

        Either the string name used by geopy (as specified in
        geopy.geocoders.SERVICE_TO_GEOCODER) or a geopy Geocoder instance
        (e.g., geopy.geocoders.Photon) may be used.

        Some providers require additional arguments such as access keys
        See each geocoder's specific parameters in geopy.geocoders

    Notes
    -----
    Ensure proper use of the results by consulting the Terms of Service for
    your provider.

    Reverse geocoding requires geopy. Install it using 'pip install geopy'.
    See also https://github.com/geopy/geopy

    Examples
    --------
    >>> from shapely.geometry import Point
    >>> df = geopandas.tools.reverse_geocode(  # doctest: +SKIP
    ...     [Point(-71.0594869, 42.3584697), Point(-77.0365305, 38.8977332)]
    ... )
    >>> df  # doctest: +SKIP
                         geometry                                            address
    0  POINT (-71.05941 42.35837)       29 Court Sq, Boston, MA 02108, United States
    1  POINT (-77.03641 38.89766)  1600 Pennsylvania Ave NW, Washington, DC 20006...
    Nr
   Fr   )Zpointsr   r   r   r   r   r   Úreverse_geocodeF   s    ,r   c              
   K   sè   ddl m} ddlm} |r4t| tjƒsJt | ¡} nt| tjƒsJt | ¡} t|t	ƒr\||ƒ}|f i |¤Ž}i }|  
¡ D ]d\}	}
z2|r”| |
¡||	< n|j|
j|
jfdd||	< W n |tfyÎ   d||	< Y n0 t |¡ qvt|ƒ}|S )Nr   )ÚGeocoderQueryError)Úget_geocoder_for_serviceT)Zexactly_one)NN)Zgeopy.geocoders.baser   r   r   Ú
isinstanceÚpdZSeriesÚ	geopandasZ	GeoSeriesÚstrÚitemsr   ÚreverseÚyÚxÚ
ValueErrorÚtimeÚsleepÚ_prepare_geocode_result)ÚdataÚforwardr   r   r   r   r   ZcoderÚresultsÚiÚsÚdfr   r   r   r   y   s*    

r   c           	      C   s–   t tƒ}g }|  ¡ D ]l\}}|du r0tƒ }d}n*|\}}|du rHtƒ }nt|d |d ƒ}|d  |¡ |d  |¡ | |¡ qtj||dd}|S )z›
    Helper function for the geocode function

    Takes a dict where keys are index entries, values are tuples containing:
    (address, (lat, lon))

    Nr   r   ZgeometryÚaddressz	EPSG:4326)ÚindexZcrs)r   Úlistr   r   Úappendr   ZGeoDataFrame)	r"   Údr'   r#   r$   Úpr&   Úlocr%   r   r   r   r   ˜   s    	r   )N)N)Úcollectionsr   r   Zpandasr   Zshapely.geometryr   r   r	   r   r   r   r   r   r   r   r   Ú<module>   s   
-
3