wiki:UsersWikiNewbieMeasurementInMeters

Version 2 (modified by pracine, 16 years ago) ( diff )

Newbie Question

How to get distance measurement in meters?

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.

If you have point data you can simply use: st_distance_sphere which always returns in meters but only works for points.

If you have more complex geometries such as lines and polygons, you need to transform 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).

e.g.

SELECT road.rd_name, ht.ht_name, ST_Distance(ST_Transform(roads.the_geom,2163) , ST_Transform(hotel.the_geom,2163))  as dist_meters
FROM road, hotel
WHERE <some criteria here>

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

plpgsqlfunctions

So you would use something like:

SELECT utmzone(ST_Centroid(the_geom)) as srid

to get the right UTM zone SRID.

Note: See TracWiki for help on using the wiki.