I have a table in Postgres with lat and long columns. Can I use the ST_Distance_Sphere or ST_Distance_Spheroid methods to get the distance between one point and all other points in the table? Do I first have to convert my existing lat/long to the Point datatype; if so is there any method to do it?
Tell me more
×
Geographic Information Systems Stack Exchange is a question and answer site for
cartographers, geographers and GIS professionals. It's 100% free, no registration required.
|
|
||||
|
|
|
Yes, you can, and the PostGIS documentation page for ST_Distance_Sphere and ST_Distance_Sphereoid both contain good examples. Here's one with lon/lat values plugged in to the ST_Point function:
The result in this example is 508166.687378974 meters. To make the calculation from a given point to other in a "lon_lat" table you would just do the following:
|
|||
|
|
|
In the line with katahdin answer, to calculate the distance to ALL other locations in your table, and assuming you have some kind of id in your rows, you can use something like this:
|
|||
|
|