【问题标题】:How to calculate the center of the bounding box? [closed]如何计算边界框的中心? [关闭]
【发布时间】:2016-04-05 14:57:00
【问题描述】:

我试图用这些给定点计算边界框的中心

(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

谁能解释我做错了什么,或者谁能纠正我可能做的错误,

谢谢你:)

【问题讨论】:

  • 你将什么作为参数传递给函数?
  • 我试图传递我计算的边界框的这些坐标(50.607041876988994,-1.3187316344406208,52.40735812301099,1.5737316344406207)

标签: python api geolocation latitude-longitude bounding-box


【解决方案1】:

使用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定义为(西南经度、西南纬度、东北经度、东北纬度)。

【讨论】:

    【解决方案2】:
    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)))
    

    在您给出的示例中没有浮动错误...。我不喜欢输出 - 但这不是问题。

    【讨论】:

    • 一个问题,它打印出 (-1.4093883217883079, 0.6741482698408712) 应该打印 51.51608899635712, 0.09891956707558282 ,你知道我哪里出错了吗?
    • 这个解决方案似乎对度数进行了所有数学运算,我认为它们应该首先转换为弧度,最后再转换为度数,即开头的lat = float(lat) * pi / 180(lon 相同)和atan2(y, x) * 180 / pi(lon 相同)。
    • 另外,我相信输出是 (center_lon, center_lat),不像描述的那样。
    猜你喜欢
    • 1970-01-01
    • 2021-02-20
    • 1970-01-01
    • 2021-11-07
    • 2011-04-10
    • 1970-01-01
    • 2011-10-22
    • 1970-01-01
    • 2023-01-01
    相关资源
    最近更新 更多