添加到特定纬度/经度位置的距离



Rails 4+Postgres。地理空间新手。很乐意接受涉及RGeo、Geokit、Geocoder或任何其他有助于解决此问题的解决方案。

模型包含两个字段latitudelongitude

我有一个包含以米为单位的距离的offset属性和一个包含4个基本方向(N、E、W、S)之一的orientation属性。

示例: offset: 525.5 orientation: W

offset距离添加到lat-long位置的标准方法是什么?作为距离添加的结果,给我一个新的lat-long对?

对于几百米等小偏移:

你可以处理N&S方向知道:

R * (lat1-lat2)= NorthSouth distance

其中R是地球半径(6335km)。

你可以处理E&W方向知道:

R * cos(lat)* (lon1-lon2) = EastWest distance.

对不起,我不会说Ruby,但翻译这个伪代码应该很容易:

R=6335000         // This is in metres
PI=3.14159265     // Your compiler may have a better constant/macro
if(orientation is North or orientation is South)
  x = offset * 180 / (PI * R)
  if(orientation is South)
     x = -x
  endif
  newLatitude = latitude + x
else
  x = offset * 180 / (PI * R * cos(lat))
  if(orientation is West)
     x = -x
  endif
  newLongitude = longitude + x
endif

经过一点挖掘,发现有一个奇异的ish库函数(考虑曲率、几何、投影、位置和数学假设)可以帮助添加到特定曲面位置的距离。

功能:ST_Project

以下使用:

SELECT ST_AsGeoJSON(ST_Project('POINT(longitude latitude)'::geography, offset, radians(orientation)))

它来自PostGIS,因此可以在Ruby/Rails中使用,尽管还不是原生Ruby对象(gem还没有封装它),而是作为PostGIS查询。

offset以米为单位。orientation以度为单位('N'=0,'E'=90等)

希望这个解决方案能帮助其他人寻找同样的东西。

最新更新