Miidpoint between two long/lat points? (earthdistance?)

Started by Carlo Stonebanksalmost 15 years ago7 messagesgeneral
Jump to latest
#1Carlo Stonebanks
stonec.register@sympatico.ca

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.

#2Rick Genter
rick.genter@gmail.com
In reply to: Carlo Stonebanks (#1)
Re: Miidpoint between two long/lat points? (earthdistance?)

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

#3Merlin Moncure
mmoncure@gmail.com
In reply to: Carlo Stonebanks (#1)
Re: Miidpoint between two long/lat points? (earthdistance?)

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;

#4Merlin Moncure
mmoncure@gmail.com
In reply to: Merlin Moncure (#3)
Re: 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

#5Carlo Stonebanks
stonec.register@sympatico.ca
In reply to: Merlin Moncure (#4)
Re: Miidpoint between two long/lat points? (earthdistance?)

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

#6Brent Wood
b.wood@niwa.co.nz
In reply to: Carlo Stonebanks (#5)
Re: Miidpoint between two long/lat points? (earthdistance?)

Why not install PostGIS with full ellipsoidal & projection support & use the azimuth & distance functions available in SQL?

Brent Wood

Brent Wood
DBA/GIS consultant
NIWA, Wellington
New Zealand

Carlo Stonebanks 05/27/11 8:20 AM >>>

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 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

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

Please consider the environment before printing this email.

NIWA is the trading name of the National Institute of Water & Atmospheric Research Ltd.

#7Merlin Moncure
mmoncure@gmail.com
In reply to: Brent Wood (#6)
Re: Miidpoint between two long/lat points? (earthdistance?)

On Thu, May 26, 2011 at 3:39 PM, Brent Wood <b.wood@niwa.co.nz> wrote:

Why not install PostGIS with full ellipsoidal & projection support & use the
azimuth & distance functions available in SQL?

installing postgis is a bit much if all you need to do are simple
distance calculations etc.

merlin