Python 输入多个经纬度坐标,找出中心点

根据工作需要,将多个坐标点的中心点找出,在stackoverflow中发现了一个解决方法,也有人提供了Python版本,但运行之后的结果却是几个带负号的小于1的小数,再观察其他版本的例子时发现需要将经纬度进行转化,将角度化为弧度就需用角度乘以π/180,反之就除以(π/180),所以在C#版本中就有

//角度转弧度
var latitude = geoCoordinate.Latitude * Math.PI / 180;
var longitude = geoCoordinate.Longitude * Math.PI / 180;
....
//弧度转角度
return new GeoCoordinate(centralLatitude * 180 / Math.PI, centralLongitude * 180 / Math.PI);

而在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]]
    coordinate = center_geolocation(locations)
    print(coordinate)

参考链接

http://stackoverflow.com/questions/6671183/calculate-the-center-point-of-multiple-latitude-longitude-coordinate-pairs

  • 7
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值