2016-09-06 74 views
1

我有一個緯度/經度和海拔的飛行路徑,我需要將它轉換爲cartesin X,Y,Z for cesium.js。 我跑到牆上試圖轉換這個,因爲我似乎沒有從我的功能得到正確的結果。極座標直角座標功能不輸出正確的數據

var R = 6371; 
function polarToCartesian(latitude, longitude, elevation){ 
    x = (R+elevation) * math.cos(latitude) * math.cos(longitude); 
    y = (R+elevation) * math.cos(latitude) * math.sin(longitude); 
    z = (R+elevation) * math.sin(latitude); 

    var ar = [x,y,z]; 
    return ar; 
} 

我不能有極地笛卡爾的正確公式,或者我沒有正確的地球半徑。我在某處發現我的半徑應該是6371,但似乎無法找到相同的SO問題供參考。

我在部分地檢查我的代碼是否正確,方法是手動添加地球半徑+給定位置的飛行路徑高度,並查看是否等於我的x,y,z向量的長度。

例如:X,Y,Z (3689.2472215653725,3183.2401988117012,13306.90338789763) 當我給我的功能這

-93.028,44.6942,7800 

緯度,經度,海拔輸出

有人能指出我找到合適的js代碼來完成這個轉換?

+0

在地球上定位點時,我不熟悉極座標[我檢查了維基百科頁面](https://en.wikipedia.org/wiki/Polar_coordinate_system#Converting_between_polar_and_Cartesian_coordinates),並且認爲你沒有權利投入。你能鏈接到地球上有極座標的資源嗎?另外,你認爲最重要的是什麼? – broguinn

+0

另外,如果高程被定義爲離地球中心的距離,那麼與三維網格在地球核心處具有(0,0)的情況下,它不應該與'z'的值相同嗎? – broguinn

回答

1

您應該使用銫的內置功能這一點。見Cartesian3.fromDegreesCartesian3.fromDegreesArray

例如:

var result = Cesium.Cartesian3.fromDegrees(latitude, longitude, elevation); 

注意,結果將是銫預計:以米爲單位,而不是公里。這也考慮了Ellipsoid的形狀,默認爲WGS84(地球不是一個完美的球體,因爲你的函數假設)。

1

Yavascript 本身沒有任何問題。但是,你的方程是不正確的。你正在尋找從緯度/長/轉換爲球面(又名笛卡爾),which was answered here

所以,你可以重寫以上爲:

function polarToCartesian(latitude, longitude, elevation){ 
    const x = math.cos(latitude) * math.cos(longitude) * elevation; 
    const y = math.cos(latitude) * math.sin(longitude) * elevation; 
    const z = math.sin(latitude) * elevation; 

    return [x, y, z]; 
}