2009-10-26 34 views
3

我需要在php或javascript中找到在kml文件中給出的地面覆蓋圖的緯度/經度。從kml文件計算地面覆蓋角落的經度/ lng

I.e.對於一個具體的例子,我需要得到:

<LatLonBox> 
    <north>60.406505416667</north> 
    <south>60.400570555556</south> 
    <east>5.3351572222222</east> 
    <west>5.3190577777778</west> 
    <rotation>3.7088732260919</rotation> 
    </LatLonBox> 

到頂點座標

SW: 60.400316388889;5.3194425 
SE: 60.400824722222;5.3355405555556 
NE: 60.406759444444;5.3347738888889 
NW: 60.406251388889;5.3186730555556 

我可以

$w=($nw_lng+$sw_lng)/2; 
$e=($ne_lng+$se_lng)/2; 
$n=($ne_lat+$nw_lat)/2; 
$s=($se_lat+$sw_lat)/2; 
$rot= rad2deg (atan (($nw_lng - $sw_lng)/($sw_lat - $nw_lat)/2 )); 

得到其他方式(大約至少,PHP代碼中給出)應該很容易回來,但我已經用了幾個小時沒有到達那裏。有小費嗎?

+0

Python的實現將會很好 - 我可以將其轉換爲JavaScript/PHP。我需要在一個項目中根據GPS數據在地圖上顯示您當前的位置(例如Garmin自定義地圖kmz文件)。一款適用於手機的網絡應用程序 - 大部分已完成,但這部分內容未能實現。不是作業。 – jankoc 2009-10-26 13:14:09

回答

3

您需要使用spherical trigonometry,作爲spherical geometry的一部分才能獲得完整的準確性。然而,由於你只處理球體的一小部分,如果你記住一件事情,歐幾里德幾何圖形就會做。

隨着緯度的增加,經度線越來越接近。例如,在北極附近,緯線幾乎接觸。因此,要處理你的緯度差異,通過多因素cos(緯度)來減少它們。這會爲您的應用帶來足夠的準確度。

$n = 60.406505416667; 
$s = 60.400570555556; 
$e = 5.3351572222222; 
$w = 5.3190577777778; 
$rotn = 3.7088732260919; 

$a = ($e + $w)/2.0; 
$b = ($n + $s)/2.0; 
$squish = cos(deg2rad($b)); 
$x = $squish * ($e - $w)/2.0; 
$y = ($n - $s)/2.0; 

$ne = array(
    $a + ($x * cos(deg2rad($rotn)) - $y * sin(deg2rad($rotn))) /$squish, 
    $b + $x * sin(deg2rad($rotn)) + $y *cos(deg2rad($rotn)) 
    ); 
$nw = array(
    $a - ($x * cos(deg2rad($rotn)) + $y * sin(deg2rad($rotn))) /$squish, 
    $b - $x * sin(deg2rad($rotn)) + $y *cos(deg2rad($rotn)) 
    ); 
$sw = array(
    $a - ($x * cos(deg2rad($rotn)) - $y * sin(deg2rad($rotn))) /$squish, 
    $b - $x * sin(deg2rad($rotn)) - $y *cos(deg2rad($rotn)) 
    ); 
$se = array(
    $a + ($x * cos(deg2rad($rotn)) + $y * sin(deg2rad($rotn))) /$squish, 
    $b + $x * sin(deg2rad($rotn)) - $y *cos(deg2rad($rotn)) 
    ); 
print_r(array(
'sw'=>$sw, 
'se'=>$se, 
'ne'=>$ne, 
'nw'=>$nw, 
)); 

我的$squish變量是我提到的cos(lat)。有水平長度的相對部分去除。正弦表看起來像這樣:

NE: (a + x cos A - y sin A, b + x sin A + y cos A) 
NW: (a - x cos A - y sin A, b - x sin A + y cos A) 
SW: (a - x cos A + y sin A, b - x sin A - y cos A) 
SE: (a + x cos A + y sin A, b + x sin A - y cos A) 

也許tttppp可能會解釋tttppp表中的差異。

+0

距離對我來說似乎很小,所以也許一個歐幾里德近似就足夠了 – Victor 2009-10-26 13:20:46

+0

我已經知道我需要使用某種三角。我認爲一個歐幾里德近似可以做到(這就是反向公式所使用的,它對我認爲的地圖工作正常)。但是,即使在工作了幾個小時之後,我仍然沒有設法爲前向案例獲得三角法。逆公式很容易計算出來...... – jankoc 2009-10-26 13:26:36

+0

現在看起來非常好!也許我的逆公式還需要一些修復才能找回確切的答案。這給了Array([sw] => Array([0] => 5.3194632953201 [1] => 60.400319597408)[se] => Array([0] => 5.3355290212874 [1] => 60.400833943604)[ne] => Array ([0] => 5.3347517046799 [1] => 60.406756374815)[nw] => Array([0] => 5.3186859787126 [1] => 60.406242028619)) – jankoc 2009-10-26 15:47:52