gpt4 book ai didi

python - 给定经纬度和距离,我想找到一个边界框

转载 作者:太空狗 更新时间:2023-10-29 22:17:24 25 4
gpt4 key购买 nike

给定经纬度和距离,我想找到一个距离小于给定距离的边界框。

这里提出了这个问题:How to calculate the bounding box for a given lat/lng location?

我不希望这个特别准确,所以我修改并简化了它

def boundingBox(latitudeInDegrees, longitudeInDegrees, halfSideInKm):
lat = math.radians(latitudeInDegrees)
lon = math.radians(longitudeInDegrees)
halfSide = 1000*halfSideInKm

RADIUS_OF_EARTH = 6371
# Radius of the parallel at given latitude
pradius = radius*math.cos(lat)

latMin = lat - halfSide/radius
latMax = lat + halfSide/radius
lonMin = lon - halfSide/pradius
lonMax = lon + halfSide/pradius
rad2deg = math.degrees
return (rad2deg(latMin), rad2deg(lonMin), rad2deg(latMax), rad2deg(lonMax))

但我无法理解这是如何工作的,尤其是这一行对我来说毫无意义 halfSide = 1000*halfSideInKm

最佳答案

这段代码不太有效,它在 KM 和 M 之间跳转。

修复了代码,使名称更符合 PEP8 风格,并添加了一个简单的盒子对象:

class BoundingBox(object):
def __init__(self, *args, **kwargs):
self.lat_min = None
self.lon_min = None
self.lat_max = None
self.lon_max = None


def get_bounding_box(latitude_in_degrees, longitude_in_degrees, half_side_in_miles):
assert half_side_in_miles > 0
assert latitude_in_degrees >= -90.0 and latitude_in_degrees <= 90.0
assert longitude_in_degrees >= -180.0 and longitude_in_degrees <= 180.0

half_side_in_km = half_side_in_miles * 1.609344
lat = math.radians(latitude_in_degrees)
lon = math.radians(longitude_in_degrees)

radius = 6371
# Radius of the parallel at given latitude
parallel_radius = radius*math.cos(lat)

lat_min = lat - half_side_in_km/radius
lat_max = lat + half_side_in_km/radius
lon_min = lon - half_side_in_km/parallel_radius
lon_max = lon + half_side_in_km/parallel_radius
rad2deg = math.degrees

box = BoundingBox()
box.lat_min = rad2deg(lat_min)
box.lon_min = rad2deg(lon_min)
box.lat_max = rad2deg(lat_max)
box.lon_max = rad2deg(lon_max)

return (box)

关于python - 给定经纬度和距离,我想找到一个边界框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1648917/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com