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;