Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

How can I find the bearing between two points in PostGIS?

Please specify in your answer whether or not the method producing a bearing on the spheroid, or a planar bearing.

share|improve this question
add comment

2 Answers 2

up vote 16 down vote accepted

Using ST_Azimuth

Planar bearing can be calculated using ST_Azimuth:

SELECT ST_Azimuth(ST_MakePoint(1,2), 
           ST_MakePoint(3,4))/(2*pi())*360 as degAz,
       ST_Azimuth(ST_MakePoint(3,4), 
           ST_MakePoint(1,2))/(2*pi())*360 As degAzrev

degaz   degazrev
------  ---------
45      225

For spherical azimuth (Quoting potgis-users group):

The PostGIS azimuth function seems to use a simple arctan function to determine azimuth. If you convert your coordinates to a projected coordinate system and then run the query, your results will be much closer to the FCC site's results.
Here is a quick conversion to UTM Zone 31:

select degrees(azimuth(
'POINT(634714.442133176 5802006.052402816)',
'POINT(634731.2410598891 5801981.648284801)'
));

which yields an azimuth of 145.457858825445. Points in the center of the UTM zone, or a more suitable projection would give better results.

Using trigonometric functions and ST_distance_sphere

This is the solution I've chosen when I had to deal with this problems, mainly for legacy reasons (I had a Python function which calculates the azimuth). First, we need to find a function that would tell us the exact distance between two points. Quoting the postgis manual:

ST_distance_sphere(point, point) Returns linear distance in meters between two lat/lon points. Uses a spherical earth and radius of 6370986 meters. Faster than distance_spheroid(), but less accurate. Only implemented for points.

Measure the Longitude and Latitude distance between the points, and use the arctan function to retrieve the angle.

share|improve this answer
    
I'll try to post the python code later, and perhaps make some comparisons between the methods - If I can find that legacy code... –  Adam Matan Aug 3 '10 at 6:22
    
Can you clarify on how you go from a distance to an angle with atan? My trigonmetry is a little hazy... –  fmark Aug 4 '10 at 10:28
add comment

For anyone coming across this question now - PostGIS supports ST_Azimuth on geography (spheroid) as of 2.0.0.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.