Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Real (Great Circle) distance in PostGIS with lat/long SRID?

Tags:

gis

postgis

I'm using a lat/long SRID in my PostGIS database (-4326). I would like to find the nearest points to a given point in an efficient manner. I tried doing an

ORDER BY    ST_Distance(point, ST_GeomFromText(?,-4326))

which gives me ok results in the lower 48 states, but up in Alaska it gives me garbage. Is there a way to do real distance calculations in PostGIS, or am I going to have to give a reasonable sized buffer and then calculate the great circle distances and sort the results in the code afterwards?

like image 257
Paul Tomblin Avatar asked Sep 23 '08 17:09

Paul Tomblin


2 Answers

You are looking for ST_distance_sphere(point,point) or st_distance_spheroid(point,point).

See:

http://postgis.refractions.net/documentation/manual-1.3/ch06.html#distance_sphere http://postgis.refractions.net/documentation/manual-1.3/ch06.html#distance_spheroid

This is normally referred to a geodesic or geodetic distance... while the two terms have slightly different meanings, they tend to be used interchangably.

Alternatively, you can project the data and use the standard st_distance function... this is only practical over short distances (using UTM or state plane) or if all distances are relative to a one or two points (equidistant projections).

like image 69
James Schek Avatar answered Sep 18 '22 10:09

James Schek


PostGIS 1.5 handles true globe distances using lat longs and meters. It is aware that lat/long is angular in nature and has a 360 degree line

like image 26
TheSteve0 Avatar answered Sep 22 '22 10:09

TheSteve0