21
A
回答
23
計算兩點之間距離(GPX文件中的每對航點)的傳統方法是使用Haversine公式。
我有一個實現算法的SQL Server函數。這應該很容易翻譯成其他語言:
create function dbo.udf_Haversine(@lat1 float, @long1 float,
@lat2 float, @long2 float) returns float begin
declare @dlon float, @dlat float, @rlat1 float,
@rlat2 float, @rlong1 float, @rlong2 float,
@a float, @c float, @R float, @d float, @DtoR float
select @DtoR = 0.017453293
select @R = 3959 -- Earth radius
select
@rlat1 = @lat1 * @DtoR,
@rlong1 = @long1 * @DtoR,
@rlat2 = @lat2 * @DtoR,
@rlong2 = @long2 * @DtoR
select
@dlon = @rlong1 - @rlong2,
@dlat = @rlat1 - @rlat2
select @a = power(sin(@dlat/2), 2) + cos(@rlat1) *
cos(@rlat2) * power(sin(@dlon/2), 2)
select @c = 2 * atn2(sqrt(@a), sqrt([email protected]))
select @d = @R * @c
return @d
end
這將返回以英里爲單位的距離。對於公里,將地球半徑替換爲相當於km的公里。
Here是一個更深入的解釋。
編輯:此功能足夠快且足夠準確,可以使用郵政編碼數據庫進行半徑搜索。多年來,它一直在this site上做得很好(但現在不再這樣做了,因爲鏈接現在已被破壞)。
1
Mike Gavaghan has an algorithm在他的網站上進行距離計算。有一個C#和一個JAVA版本的代碼。
1
德爾福執行Vincenty formulae可以找到here。
1
這是一個Scala實現。
3958.761是以英里爲單位的mean radius of the Earth。要在km(或其他單位)中得到結果,只需更改此數字即可。
// The Haversine formula
def haversineDistance(pointA: (Double, Double), pointB: (Double, Double)): Double = {
val deltaLat = math.toRadians(pointB._1 - pointA._1)
val deltaLong = math.toRadians(pointB._2 - pointA._2)
val a = math.pow(math.sin(deltaLat/2), 2) + math.cos(math.toRadians(pointA._1)) * math.cos(math.toRadians(pointB._1)) * math.pow(math.sin(deltaLong/2), 2)
val greatCircleDistance = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
3958.761 * greatCircleDistance
}
// A sequence of gpx trackpoint lat,long pairs parsed from the track GPX data
val trkpts: Seq[(Double, Double)] = {
val x = scala.xml.XML.loadString(track)
(x \\ "trkpt").map(trkpt => ((trkpt \ "@lat").text.toDouble, (trkpt \ "@lon").text.toDouble))
}
// Distance of track in miles using Haversine formula
val trackDistance: Double = {
trkpts match {
case head :: tail => tail.foldLeft(head, 0.0)((accum, elem) => (elem, accum._2 + haversineDistance(accum._1, elem)))._2
case Nil => 0.0
}
}
0
這個問題是相當老,但我想添加一個python選項的完整性。 GeoPy既有great-circle distance
也有Vincenty distance
。
相關問題
- 1. 如何計算距離cellID的距離?
- 2. 如何計算距離?
- 3. 計算距離
- 4. 計算距離
- 5. 距離計算
- 6. 計算距離
- 7. 計算距離
- 8. 計算距離
- 9. 計算距離
- 10. 計算距離
- 11. 計算距離
- 12. 計算距離
- 13. 計算距離
- 14. 計算距離
- 15. 計算距離位置的距離 - iPhone
- 16. 在PDB計算距離文件
- 17. Dijkstra算法 - 如何計算距離?
- 18. 計算距離CoreLocation
- 19. Matlab計算距離
- 20. PostGis距離計算
- 21. MATLAB計算距離
- 22. 計算Levenshtein距離
- 23. 如何計算距離連續圖像的距離
- 24. 如何計算距離路徑的最短距離?
- 25. 如何計算2ndArray中的距離?
- 26. 如何計算UIRefreshControl的拉距離
- 27. 如何計算到位置的距離
- 28. 如何計算點之間的距離?
- 29. 如何通過Haversine計算短距離和長距離?
- 30. Cloudant中的距離計算
非常感謝。我將把它移植到java併發布到這裏。 @DtoR是什麼意思?到半徑的距離? – guerda 2009-02-20 16:34:38
這是將度數轉換爲Radians pi/180的因素。 – cdonner 2009-02-21 00:23:33