U
    ҭc                     @   sJ   d Z ddlZddlmZ dZdddZdd	 Zd
d Zdd Zdd ZdS )z/Performs requests to the Google Maps Roads API.    N)convertzhttps://roads.googleapis.comFc                 C   s6   dt |i}|rd|d< | jd|tdtddg S )aK  Snaps a path to the most likely roads travelled.

    Takes up to 100 GPS points collected along a route, and returns a similar
    set of data with the points snapped to the most likely roads the vehicle
    was traveling along.

    :param path: The path to be snapped.
    :type path: a single location, or a list of locations, where a
        location is a string, dict, list, or tuple

    :param interpolate: Whether to interpolate a path to include all points
        forming the full road-geometry. When true, additional interpolated
        points will also be returned, resulting in a path that smoothly follows
        the geometry of the road, even around corners and through tunnels.
        Interpolated paths may contain more points than the original path.
    :type interpolate: bool

    :rtype: A list of snapped points.
    pathtrueinterpolatez/v1/snapToRoadsFZbase_urlZaccepts_clientidZextract_bodysnappedPointsr   location_list_request_ROADS_BASE_URL_roads_extractget)clientr   r   params r   M/home/ubuntu/graampay/app_env/lib/python3.8/site-packages/googlemaps/roads.pysnap_to_roads   s     r   c                 C   s*   dt |i}| jd|tdtddg S )a  Find the closest road segments for each point

    Takes up to 100 independent coordinates, and returns the closest road
    segment for each point. The points passed do not need to be part of a
    continuous path.

    :param points: The points for which the nearest road segments are to be
        located.
    :type points: a single location, or a list of locations, where a
        location is a string, dict, list, or tuple

    :rtype: A list of snapped points.
    pointsz/v1/nearestRoadsFr   r   r   )r   r   r   r   r   r   nearest_roads:   s     r   c                 C   s0   dd t |D }| jd|tdtddg S )a#  Returns the posted speed limit (in km/h) for given road segments.

    :param place_ids: The Place ID of the road segment. Place IDs are returned
        by the snap_to_roads function. You can pass up to 100 Place IDs.
    :type place_ids: str or list

    :rtype: list of speed limits.
    c                 S   s   g | ]}d |fqS )ZplaceIdr   ).0Zplace_idr   r   r   
<listcomp>Z   s     z speed_limits.<locals>.<listcomp>/v1/speedLimitsFr   ZspeedLimits)r   Zas_listr
   r   r   r   )r   Z	place_idsr   r   r   r   speed_limitsP   s    
 r   c                 C   s"   dt |i}| jd|tdtdS )a  Returns the posted speed limit (in km/h) for given road segments.

    The provided points will first be snapped to the most likely roads the
    vehicle was traveling along.

    :param path: The path of points to be snapped.
    :type path: a single location, or a list of locations, where a
        location is a string, dict, list, or tuple

    :rtype: dict with a list of speed limits and a list of the snapped points.
    r   r   Fr   )r   r	   r
   r   r   )r   r   r   r   r   r   snapped_speed_limitsb   s    r   c                 C   s   z|   }W n2   | jdkr,tj| jtjddY nX d|kr|d }|d }|dkrttj||dtj||d| jdkrtj| j|S )z1Extracts a result from a Roads API HTTP response.   ZUNKNOWN_ERRORzReceived a malformed response.errorstatusZRESOURCE_EXHAUSTEDmessage)jsonZstatus_code
googlemaps
exceptions	HTTPErrorZApiErrorZ_OverQueryLimitr   )Zrespjr   r   r   r   r   r   w   s&    


r   )F)	__doc__r   r   r   r   r   r   r   r   r   r   r   r   <module>   s   
