2009-10-30 51 views
4

給定一個經緯度和距離,我想找到一個邊界框,其中的距離小於給定的距離。給定一個經緯度和距離,我想找到一個邊界框

這個問題,在這裏問:How to calculate the bounding box for a given lat/lng location?

我DONOT想這個partcularly準確,所以我已經修改和簡化它

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

+0

張貼的代碼不遠距離工作或接近極點。 – 2009-10-30 10:12:13

+0

Nicolai:只希望它可以在短距離內工作,所以可以假設一個平坦的地球等等。對於經度比較,我們正在做一個「pradius」的劃分,所以它沒有考慮到兩極附近的變化? – agiliq 2009-10-30 10:17:02

+0

halfSideInKm是從點的距離還是不是?你能解釋一下在這裏要做什麼嗎? – 2016-06-06 11:22:42

回答

2

該行將邊界框單位從公里轉換爲米。

+1

呃..現在你明顯提到它,但是'latMin = lat - halfSide/radius'似乎是錯誤的。 1Km!= 1000/6371緯度距離爲1deg緯度變化爲〜110 km距離變化。 – agiliq 2009-10-30 10:15:10

8

這段代碼也不太工作中,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) 
+0

這個答案救了我去IRC,謝謝:) – Sevenearths 2014-03-17 12:29:42

+0

這工作完美。謝謝。一個問題:什麼是half_side_in_miles?我如何計算?我搜查了intrewebs並沒有發現任何東西。 – tmthyjames 2015-05-29 17:05:20

+0

順便說一句,第二個'assert'不正確。緯度範圍從-90°到90°,而不是-180°到180°。 – 2016-03-25 08:01:25

相關問題