| 1 | = Newbie Question = |
| 2 | |
| 3 | '''How to get distance measurement in meters?''' |
| 4 | |
| 5 | ST_Distance returns measurement in the spatial ref system units. If you have data in long lat your measurements will be in degrees which is not terribly helpful. |
| 6 | |
| 7 | If you have point data you can simply use: st_distance_sphere which always returns in meters |
| 8 | but only works for points. |
| 9 | |
| 10 | If you have more complex geometries such as lines and polygons, you need to transform |
| 11 | your data into a meter-based projection so you can get measurement in meters. If for example you are in the U.S, SRID 2163 US National Atlas Equal Area works pretty good for measurement. It is not as accurate as UTM or State Plane, but its pretty good and covers all of US I believe and probably works for some of Canada as well (not sure about non-continental e.g. Hawaii, Alaska). |
| 12 | |
| 13 | e.g. |
| 14 | |
| 15 | |
| 16 | {{{ |
| 17 | SELECT road.rd_name, ht.ht_name, ST_Distance(ST_Transform(roads.the_geom,2163) , ST_Transform(hotel.the_geom,2163)) as dist_meters |
| 18 | FROM road, hotel |
| 19 | WHERE <some criteria here> |
| 20 | |
| 21 | }}} |
| 22 | |
| 23 | |
| 24 | If you are somewhere else and your locations are within that projection, you can use that. Look at the utmzone function in the wiki that will help you determing the right UTM zone SRID for your data |
| 25 | |
| 26 | [wiki:UsersWikiplpgsqlfunctions plpgsqlfunctions] |
| 27 | |
| 28 | So you would use something liek |
| 29 | SELECT utmzone(ST_Centroid(the_geom)) as srid |
| 30 | |
| 31 | to get the right UTM zone SRID. |