Обсуждение: Miidpoint between two long/lat points? (earthdistance?)
I need to calculate the long/lat values between a line demarcated by two long/lat points.
The points will be very close, but there is the 180 degree problem to consider, so a simple average won’t work.
Does anyone know of a function or have a formula that will work using geo long/lat values? I don’t see anything obvious in the earthdistance module.
On Wed, May 25, 2011 at 9:47 AM, Carlo Stonebanks <stonec.register@sympatico.ca> wrote:
I need to calculate the long/lat values between a line demarcated by two long/lat points.
The points will be very close, but there is the 180 degree problem to consider, so a simple average won’t work.
Does anyone know of a function or have a formula that will work using geo long/lat values? I don’t see anything obvious in the earthdistance module.
The simplest way to deal with "the 180 degree problem" is to remember that you can add 360 degrees to a long and get a value that should continue to work. So, assuming "West" is negative, -175 (175 degrees West) is the same as -175+360 = 185 (185 degrees East). Then you don't have to worry about wraparound. If the result is > 180, subtract 360.
--
Rick Genter
rick.genter@gmail.com
On Wed, May 25, 2011 at 12:47 PM, Carlo Stonebanks <stonec.register@sympatico.ca> wrote: > I need to calculate the long/lat values between a line demarcated by two > long/lat points. > > > > The points will be very close, but there is the 180 degree problem to > consider, so a simple average won’t work. > > > > Does anyone know of a function or have a formula that will work using geo > long/lat values? I don’t see anything obvious in the earthdistance module. Conerted from javascript from here: http://en.wikipedia.org/wiki/Atan2 btw i'm not huge fan of earthdistance module either -- it's easier to just rig functions to do what you want. merlin create or replace function midpoint( lat1 float8, lon1 float8, lat2 float8, lon2 float8, lat_mid out float8, lon_mid out float8) returns record as $$ select atan2(sin(lat1)+sin(lat2), sqrt( (cos(lat1)+Bx)*(cos(lat1)+Bx) + By*By) ) * 57.2957795, (lon1 + atan2(By, cos(lat1) + Bx)) * 57.2957795 from ( select lat1, lat2, lon1, cos(lat2) * cos(dlon) as bx, cos(lat2) * sin(dlon) as by from ( select $1 * 0.0174532925 as lat1, $3 * 0.0174532925 as lat2, $2 * 0.0174532925 as lon1, ($4 - $2) * 0.0174532925 as dlon ) q ) q; $$ language sql immutable;
On Thu, May 26, 2011 at 12:05 AM, Merlin Moncure <mmoncure@gmail.com> wrote: > Converted from javascript from here: http://en.wikipedia.org/wiki/Atan2 whoops! meant to say here: http://www.movable-type.co.uk/scripts/latlong.html merlin
Nicely done, Merlin! Hope others with the same problem can find this post. Thanks a lot. -----Original Message----- From: Merlin Moncure [mailto:mmoncure@gmail.com] Sent: May 26, 2011 9:53 AM To: Carlo Stonebanks Cc: pgsql-general@postgresql.org Subject: Re: [GENERAL] Miidpoint between two long/lat points? (earthdistance?) On Thu, May 26, 2011 at 12:05 AM, Merlin Moncure <mmoncure@gmail.com> wrote: > Converted from javascript from here: http://en.wikipedia.org/wiki/Atan2 whoops! meant to say here: http://www.movable-type.co.uk/scripts/latlong.html merlin