a
    J5d                     @   s   d dl mZ d dlmZ d dlZd dlZddlmZ dej	eej	 e
ee
ej	d	d
dZej	ej	e
e
dddZej	ej	e
e
e
dddZdS )    )ceil)OptionalN   )process_inputhttp://localhost:5000  	distances)sourcesdestinationsosrm_server_addressosrm_batch_size	cost_typereturnc                 C   s   t | |\} }| jd }|jd }t||f}t|| }t|| }	t|D ]~}
|
| }t|
d | |}t|	D ]V}|| }t|d | |}| || }||| }t||||d|||||f< qvqP|S )a  Compute distance matrix from sources to destinations using OSRM service

    Parameters
    ----------
    sources, destinations
        2D Arrays of coordinates in the form [lat, lng] for each row
        Also, if ``destinations`` is `None`, compute the distance between each
        source in ``sources``.

    osrm_server_address
        Base address of the OSRM server instance

    osrm_batch_size
        Subset of sources and destinations for each request. This reduces the
        request size, thus alleviating max table issues, but slows down the
        search

    cost_type
        "distances" to get the street distances and "durations" for the street
        time

    Returns
    -------
    Distance (or duration) matrix of size `num_sources x num_destinations`
    r   r   )r   )r   shapenpZzerosr   rangemin_get_batch_osrm_distance)r	   r
   r   r   r   num_sourcesnum_destinationsZcost_matrixZnum_batches_iZnum_batches_jiZstart_iZend_ijZstart_jZend_jsources_batchdestinations_batch r   ^/var/www/html/django/DPS/env/lib/python3.9/site-packages/python_tsp/distances/osrm_distance.pyosrm_distance_matrix
   s0     

	r   )r   r   r   r   c                 C   s2   t | |||}t|}|  t| | S )z2Request the OSRM distance matrix for a given batch)_format_osrm_urlrequestsgetraise_for_statusr   arrayjson)r   r   r   r   urlrespr   r   r   r   I   s    
r   )r   r   r   r   r   c              	   C   s   |dd }d dd | D }t| |rN| jd dkrN| d| d	| S d d
d |D }|d | }| jd }|jd }	d dd t|D }
d dd t|||	 D }| d| d|
 d| d| 	S )ai  Format OSRM url string with sources and destinations

    Notes
    -----
    Consider the N sources in the form
        (lat_src1, lgn_src1), (lat_src2, lgn_src2), ...

    and the M destinations in the form
        (lat_dest1, lgn_dest1), (lat_dest2, lgn_dest2), ...

    This function converts these properties in a URL of the form
        {OSRM_SERVER_ADDRESS}/table/v1/driving/
        lng_src1,lat_src1;lng_src2,lat_src2;...;lng_srcN,lat_srcN;
        lng_dest1,lat_dest1;lng_dest2,lat_dest2;...;lng_destM,lng_destM
        ?sources=0;1;...;N-1
        &destinations=N;N+1;...;N+M-1
        &annotations=distance

    In the simpler case when sources == destinations, the URL is simplified to
        {OSRM_SERVER_ADDRESS}/table/v1/driving/
        lng_src1,lat_src1;lng_src2,lat_src2;...;lng_srcN,lat_srcN
        ?annotations=distance

    Obs: Replace "distance" with "duration" if a time matrix is required
    Obs2: The matrix type follows the singular form in the URL (e.g.,
    "distance"), but the returned JSON follows the plural form (e.g.,
    "distances"). Thus, we ignore the last letter of the input type
    N;c                 s   s$   | ]}|d   d|d  V  qdS r   ,r   Nr   ).0sourcer   r   r   	<genexpr>}   s   z#_format_osrm_url.<locals>.<genexpr>r   r   z/table/v1/driving/z?annotations=c                 s   s$   | ]}|d   d|d  V  qdS r'   r   )r)   destinationr   r   r   r+      s   c                 s   s   | ]}t |V  qd S Nstrr)   indexr   r   r   r+          c                 s   s   | ]}t |V  qd S r-   r.   r0   r   r   r   r+      s   z	?sources=z&destinations=z&annotations=)joinr   Zarray_equalr   r   )r   r   r   r   Zurl_cost_typeZsources_coordZdestinations_coordZlocations_coordr   r   Zsources_indicesZdestinations_indicesr   r   r   r   Y   sD    "





r   )Nr   r   r   )mathr   typingr   numpyr   r   Zdata_processingr   Zndarrayr/   intr   r   r   r   r   r   r   <module>   s6       @