从某个点获取最近的位置,然后从第二个最近的位置获取?

时间:2019-01-23 13:47:44

标签: python pandas

我正在研究一种寻找最佳产品交付途径的模型。我有必须交付产品的地方的经度和纬度。第一批机队从仓库(具有经度和纬度)出发。舰队必须到达最近的降落地点(例如S1),然后必须从S1重新找到最近的降落地点。

下面是示例数据

Society_ID  Order_Count Delivery_Time   Latitude    Longitude
     1          605       1:08:42        28.413494  77.045502
     2          168       1:15:57        28.426756  77.039543
     3          431       1:57:06        28.403252  77.047073
     7          216       1:18:04        28.401499  77.052734
     8          212       1:05:29        28.394922  77.029305
     9          65        0:39:01        28.393707  77.028603
     10         59        0:43:00        28.42454   77.034126
     11         199       1:22:37        28.424129  77.036095
     12         54        0:22:32        28.404266  77.045349
     14         117       0:56:28        28.407686  77.059799
     15         143       1:07:25        28.414696  77.069862
     16         183       0:50:37        28.425392  77.042679
     17         151       0:31:52        28.413244  77.111946
     18         209       1:10:47        28.406481  77.04248
     19         118       0:43:25        28.412949  77.069412
     20         131       0:44:55        28.41283   77.057426

仓库位置:28.387842、76.985951

我能够得到两个地点之间的距离,但不能得到最佳路线。

import pandas as pd
import googlemaps
from itertools import tee

# Requires API key 
gmaps = googlemaps.Client(key='API KEY') 

df1 = df.iloc[[0,1,2,3,4,5,6,7,8,9]]

def pairwise(iterable):
    a, b = tee(iterable)
    next(b, None)
    return zip(a, b)

#empty list - will be used to store calculated distances
list = [0]

# Loop through each row in the data frame using pairwise
for (i1, row1), (i2, row2) in pairwise(df1.iterrows()):
      #Assign latitude and longitude as origin/departure points
      LatOrigin = row1['latitude'] 
      LongOrigin = row1['longitude']
      origins = (LatOrigin,LongOrigin)

      #Assign latitude and longitude from the next row as the destination point
      LatDest = row2['latitude']   # Save value as lat
      LongDest = row2['longitude'] # Save value as lat
      destination = (LatDest,LongDest)

      #pass origin and destination variables to distance_matrix function# output in meters
      result = gmaps.distance_matrix(origins, destination, mode='driving')["rows"][0]["elements"][0]["distance"]["value"]

      #append result to list
      list.append(result)

df1['Distance'] = list

df1.head()

我想要这样的路线:

仓库> S1> S3> S6> S7 ...

即车队从仓库开始,然后到达最近的地点S1,然后从S1到达最接近S1的S3。

0 个答案:

没有答案