Python 如何计算边界框的中心?
我试图用这些给定的点计算边界框的中心Python 如何计算边界框的中心?,python,api,geolocation,latitude-longitude,bounding-box,Python,Api,Geolocation,Latitude Longitude,Bounding Box,我试图用这些给定的点计算边界框的中心 (50.607041876988994, -1.3187316344406208, 52.40735812301099, 1.5737316344406207) Edit Solved:这是我用python编写的代码 from math import cos, sin, atan2, sqrt def center_geolocation(geolocations): """ Provide a relativel
(50.607041876988994, -1.3187316344406208, 52.40735812301099, 1.5737316344406207)
Edit Solved:这是我用python编写的代码
from math import cos, sin, atan2, sqrt
def center_geolocation(geolocations):
"""
Provide a relatively accurate center lat, lon returned as a list pair, given
a list of list pairs.
ex: in: geolocations = ((lat1,lon1), (lat2,lon2),)
out: (center_lat, center_lon)
"""
x = 0
y = 0
z = 0
for lat, lon in geolocations:
lat = float(lat)
lon = float(lon)
x += cos(lat) * cos(lon)
y += cos(lat) * sin(lon)
z += sin(lat)
x = float(x / len(geolocations))
y = float(y / len(geolocations))
z = float(z / len(geolocations))
return (atan2(y, x), atan2(z, sqrt(x * x + y * y)))
谢谢:)
您给出的示例中没有浮点错误。。。。我不喜欢输出-但这不是问题。你将什么作为参数传递给函数?我试图传递我计算的边界框的坐标(50.6070418769494,-1.3187316344406208,52.40735812301099,1.5737316344406207)一个问题,它打印出来(-1.4093883217883079,0.6741482698408712)它应该打印51.51608899635712,0.09891956707558282,你知道我可能出了什么问题吗?这个解决方案似乎对度做了所有的计算,我认为它们应该首先转换成弧度,然后在最后转换成度,即
lat=float(lat)*pi/180
(与lon相同)和atan2(y,x)*180/pi
(与lon相同)。此外,我认为输出是(center_lon,center_lat),与描述的不同。
from math import *
def center_geolocation(geolocations):
"""
Provide a relatively accurate center lat, lon returned as a list pair, given
a list of list pairs.
ex: in: geolocations = ((lat1,lon1), (lat2,lon2),)
out: (center_lat, center_lon)
"""
x = 0
y = 0
z = 0
for lat, lon in geolocations:
lat = float(lat)
lon = float(lon)
x += cos(lat) * cos(lon)
y += cos(lat) * sin(lon)
z += sin(lat)
x = float(x / len(geolocations))
y = float(y / len(geolocations))
z = float(z / len(geolocations))
return (atan2(y, x), atan2(z, sqrt(x * x + y * y)))
center_geolocation(
((50.607041876988994, -1.3187316344406208),
(52.40735812301099, 1.5737316344406207)))