zoukankan      html  css  js  c++  java
  • 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)

  • 相关阅读:
    网络数据处理
    进程间通信和网络
    附加的操作系统服务
    通用操作系统服务
    UIScrollView 子控件的自动布局经验
    UIImage 添加水印
    数据类型
    ios 获取手机的IP地址
    UILAbel 设置了attributedText 后省略号不显示
    swift
  • 原文地址:https://www.cnblogs.com/zle1992/p/7209957.html
Copyright © 2011-2022 走看看