多边形求中心点(经纬度坐标点)
多边形求中心点
因项目需要,需要求多个坐标点的中心点,在csdn中找到相关资料,该博主是参考stackoverflow中的版本,然后进行修改,这里做一个标记。
#-*- 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)
参考链接: