• Как в Python вычислить ближайшие точки по имеющимся GPS-координатам?

    adrian_fox
    @adrian_fox
    from math import sin, cos, sqrt, atan2, radians
    
    resturants = [[34.146263,-118.268293, 'Ресторан #1'],[34.152159,-118.246821, 'Ресторан #2'],[34.125732,-118.244208, 'Ресторан #3'],[34.132126,-118.233931, 'Ресторан #4']] 
    R = 6373.0
    mylatitute = 34.145979
    mylongtitute = -118.254809
    def findDist(lat1, lon1, lat2, lon2):
      global R
      lat1 = radians(lat1)
      lon1 = radians(lon1)
      lat2 = radians(lat2)
      lon2 = radians(lon2)
    
      dlon = lon2 - lon1
      dlat = lat2 - lat1
    
      a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2
      c = 2 * atan2(sqrt(a), sqrt(1 - a))
      return R * c
    
    def minDist():
      closeDist = 0
      name = ''
      for location in resturants:
        distance = findDist(mylatitute, mylongtitute, location[0], location[1]) 
        if(closeDist==0):
          closeDist = distance
          name = location[2]
        elif(closeDist>distance):
          closeDist = distance
          name = location[2]
        else:
          continue
      closeDist = round(closeDist, 2)
      print('Ближайший ресторан '+name+' находится в '+str(closeDist)+' км отсюда')
    minDist()