而在Python中就有角度弧度转化的函数 radians()方法:转换角度为弧度的 degrees()方法:从弧度转换到角度
所以Python版本的代码应如下,其中输入为经纬度的list,例如geolocations = ((lat1,lon1), (lat2,lon2),),输出则为中心点的经纬度(center_lon,center_lat)
#-*- coding: UTF-8 -*- from math import cos, sin, atan2, sqrt, pi ,radians, degrees def center_geolocation(geolocations): x = 0 y = 0 z = 0 lenth = len(geolocations) for lon, lat in geolocations: lon = radians(float(lon)) lat = radians(float(lat)) x += cos(lat) * cos(lon) y += cos(lat) * sin(lon) z += sin(lat) x = float(x / lenth) y = float(y / lenth) z = float(z / lenth) return (degrees(atan2(y, x)), degrees(atan2(z, sqrt(x * x + y * y)))) if __name__ == '__main__': locations = [[116.568627,39.994879],[116.564791,39.990511],[116.575012,39.984311]] print center_geolocation(locations)
参考链接
http://stackoverflow.com/questions/6671183/calculate-the-center-point-of-multiple-latitude-longitude-coordinate-pairs