• Python3 计算城市距离


    利用上一篇得到的城市经纬度算城市距离

     1 import requests
     2 from math import radians, cos, sin, asin, sqrt  
     3 
     4 def geocode(address):
     5     url= 'http://api.map.baidu.com/geocoder?output=json&key=f247cdb592eb43ebac6ccd27f796e2d2&address='+str(address)
     6     response = requests.get(url)
     7     answer = response.json()
     8     #print(address + "的经纬度:", answer['geocodes'][0]['location'])
     9     if answer['status']  == 'INVALID_PARAMETERS':
    10         return 0,0
    11     else:
    12         lon = float(answer['result']['location']['lng'])
    13         lat = float(answer['result']['location']['lat'])
    14         return lon ,lat 
    15 def distence(address1,address2):
    16     
    17     lon1, lat1 = geocode(address1) 
    18     lon2, lat2 = geocode(address2)  
    19     
    20    
    21 
    22     # 将十进制度数转化为弧度  
    23     lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])  
    24 
    25     # haversine公式  
    26     dlon = lon2 - lon1   
    27     dlat = lat2 - lat1   
    28     a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2  
    29     c = 2 * asin(sqrt(a))   
    30     r = 6371 # 地球平均半径,单位为公里  
    31     return int(c * r) 
    32 def distence_list(city_array):
    33     distence_array = []
    34     for i in range(a.shape[0]):
    35         distence_array.append(distence(city_array[i][0],city_array[i][1]))
    36     return np.array(distence_array)

  • 相关阅读:
    oc 基本基础类型之NSString
    oc 内存管理
    自定义的init方法和重写的init方法
    property属性
    iOS 开发朗读文字
    获取当前最顶层的ViewController
    二维码扫描的简单封装
    OC百度导航类的封装
    OC上传图片的封装(配合AFNetWorkiing)
    集成百度地图报错41个解决方法(转)
  • 原文地址:https://www.cnblogs.com/zle1992/p/7209957.html
Copyright © 2020-2023  润新知