# [postgis-users] Calculating Z angle or pitch from 2 3D locations

David Rowley dgrowleyml at gmail.com
Sat Jul 13 18:36:33 PDT 2013

```I'm wondering if there is anyway with PostGIS 2.0 to calculate the pitch
which I must travel to get to another location on a WGS84 spheroid.

Basically I want to calculate where the tops of mountains are from my
current location using a table which contains the lat/long/elevation of the
summits.

Here is my current formula which is part of a PostgreSQL function.

-- The pitch calculation is not perfect. First we calculate the angle
to the location on
-- a flat plane using the trig function atan. Note that we can't use
ST_3DDistance here to
-- calculate the variation in height of the two greographical points
as we need to know if the
-- point that we're heading to is lower than our current point.
ST_3DDistance only tells us the
-- veritcal distance and will always be positive. If we're up high
and looking down we need a
-- negative pitch...
degrees(atan((osm.alt - elevation) / ST_Distance(osm.latlong,loc)))

-- Now we subtract an estimate of the degrees around the sphere the
object is.
-- Note this is assumes the world is a perfect sphere with a 6370986
-- Here we find a percentage of how far over the sphere the point is
then divide
-- that by the estimate of the circumference of earth to get the
degrees.
- (ST_Distance(osm.latlong,loc) / (6370986 * pi() * 2) * 360) AS
pitch

Osm.alt is the elvevation of the point we want to look at.
Osm.latlong is the geography column of the point we're interested in.
Loc is our current location in geography
Elevation is our current elevation.

Is there a way to do this with more accuracy (using WGS84 data) with
PostGIS?

Any help on this would be really appreciated.

David

```