| 1 | = Newbie Question = |
| 2 | |
| 3 | |
| 4 | '''What is the measurement returned by ST_Distance?''' |
| 5 | |
| 6 | |
| 7 | ST_Distance always returns in the units of the projection. |
| 8 | |
| 9 | So if for example you chose SRID 4326 - then your measurement would be in degrees. |
| 10 | |
| 11 | It is best to use a meter based or feet based projection for measurement. |
| 12 | |
| 13 | Unfortunately you can not do that unless you are dealing with regional data such as country, state etc. |
| 14 | |
| 15 | To get meters when using 4326 or other degree projection - use ST_Distance_Sphere or ST_Distance_Spheroid. These unfortunately only work with point geometries. |
| 16 | |
| 17 | Faster but less precise |
| 18 | |
| 19 | |
| 20 | {{{ |
| 21 | SELECT ST_Distance_Sphere( |
| 22 | ST_GeomFromText('POINT(5.0 52.0)', 4326), |
| 23 | ST_GeomFromText('POINT(6.0 56.0)', 4326) ); |
| 24 | |
| 25 | SELECT ST_Distance_Sphere(t1.the_geom, t2.the_geom) |
| 26 | FROM t1 INNER JOIN t2 ON t1.somefield1 = t2.somefield2; |
| 27 | |
| 28 | }}} |
| 29 | |
| 30 | Slower but more precise |
| 31 | |
| 32 | {{{ |
| 33 | SELECT ST_Distance_Spheroid( |
| 34 | ST_GeomFromText('POINT(5.0 52.0)', 4326), |
| 35 | ST_GeomFromText('POINT(6.0 56.0)', 4326), |
| 36 | 'SPHEROID[wiki:UsersWiki"WGS 84",6378137,298.257223563 "WGS 84",6378137,298.257223563]' |
| 37 | ); |
| 38 | |
| 39 | SELECT ST_Distance_Spheroid(t1.the_geom, t2.the_geom, 'SPHEROID[wiki:UsersWiki"WGS 84",6378137,298.257223563 "WGS 84",6378137,298.257223563]') |
| 40 | FROM t1 INNER JOIN t2 ON t1.somefield1 = t2.somefield2; |
| 41 | |
| 42 | }}} |