我试图用这些给定点计算边界框的中心
(50.607041876988994, -1.3187316344406208, 52.40735812301099, 1.5737316344406207)
这是我在python 中完成的代码
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)))
然而,我不断得到这个错误
line 64, in center_geolocation
for lat, lon in geolocations:
TypeError: 'float' object is not iterable
,有人能解释我做错了什么吗?或者有人能纠正我可能犯的错误吗
谢谢:)
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)))
你给出的例子中没有浮动错误。。。。我不喜欢输出,但这不是问题所在。
使用shapely
库的解决方案:
from shapely.geometry import box
bounds = (-1.3187316344406208, 50.607041876988994, 1.5737316344406207, 52.40735812301099)
polygon = box(*bounds)
print(polygon.centroid.x, polygon.centroid.y)
bounds
定义为(西南经度、西南纬度、东北经度、东北纬度)。