Python3 计算城市距离
2017-07-20 10:46
176 查看
利用上一篇得到的城市经纬度算城市距离
1 def geocode(address): 2 url= 'http://api.map.baidu.com/geocoder?output=json&key=f247cdb592eb43ebac6ccd27f796e2d2&address='+str(address) 3 response = requests.get(url) 4 answer = response.json() 5 #print(address + "的经纬度:", answer['geocodes'][0]['location']) 6 if answer['status'] == 'INVALID_PARAMETERS': 7 return 0,0 8 else: 9 lon = float(answer['result']['location']['lng']) 10 lat = float(answer['result']['location']['lat']) 11 return lon ,lat 12 def distence(address1,address2): 13 14 lon1, lat1 = geocode(address1) 15 lon2, lat2 = geocode(address2) 16 17 #计算距离 18 from math import radians, cos, sin, asin, sqrt 19 20 # 将十进制度数转化为弧度 21 lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2]) 22 23 # haversine公式 24 dlon = lon2 - lon1 25 dlat = lat2 - lat1 26 a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2 27 c = 2 * asin(sqrt(a)) 28 r = 6371 # 地球平均半径,单位为公里 29 return int(c * r) 30 def distence_list(city_array): 31 distence_array = [] 32 for i in range(a.shape[0]): 33 distence_array.append(distence(city_array[i][0],city_array[i][1])) 34 return np.array(distence_array)
相关文章推荐
- Python 根据百度 API 获得经纬度,根据经纬度计算城市间距离
- Python基于动态规划算法计算单词距离
- python学习[4]_利用numpy做一些距离的计算
- MachingLearning中的距离和相似性计算以及python实现
- python pymongo 经纬度坐标距离计算
- [042]各类距离计算函数——Python版
- python根据经纬度计算两点距离
- Python文本相似性计算之编辑距离详解
- MachingLearning中的距离和相似性计算以及python实现
- 利用python对中文文本数据进行LDA训练、计算概率距离
- Python实现计算最小编辑距离
- 如何运用Python计算地图上两点的实际距离
- 聚类之距离计算及Python实现
- python微信跳一跳系列之自动计算跳一跳距离
- ArcGIS教程:基于路网的城市间距离计算方法
- python根据经纬度计算距离示例
- python计算两个地址之间的距离
- python计算两个地址之间的距离方法
- python实现根据两点经纬度计算实际距离
- 用Python根据两点经纬度计算距离