Add distance to a particular latitude/longitude position

荒凉一梦 提交于 2019-12-12 09:22:08

问题


Rails 4 + Postgres. New to geospatial. Happy to accept solutions that involve RGeo, Geokit, Geocoder, or any other gem that helps solve this issue.

Model contains two fields latitude and longitude.

I have an offset attribute that contains a distance in meters and an orientation attribute that contains one of the 4 cardinal directions (N, E, W, S).

Example: offset: 525.5 orientation: W

What's a standard way of adding the offset distance to the lat-long position, to give me a new lat-long pair as the result of the distance addition?


回答1:


For small offsets such as a few hundred metres:

You can handle the N&S orientations knowing that:

R * (lat1-lat2)= NorthSouth distance

where R is the Earth radius (6335km).

You can handle the E&W orientations knowing that:

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

I'm sorry, I don't speak Ruby, but it should be pretty easy to translate this pseudo-code:

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



回答2:


It took a little bit of digging, and it turns out that there is a singular-ish library function (accounting for curvature, geometric, projection, location & mathematical assumptions) that helps add a distance to a specific surface position.

Function: ST_Project

Used below:

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

It's from PostGIS and therefore useable in Ruby/Rails, although not yet as native Ruby objects (gems haven't wrapped it yet), but as a PostGIS query instead.

offset is in meters. orientation is in degrees ('N'=0, 'E'=90, etc.)

Hopefully, this solution helps others looking for the same thing.



来源:https://stackoverflow.com/questions/20079536/add-distance-to-a-particular-latitude-longitude-position

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!