简体   繁体   中英

MariaDB : calculate distance between 2 points in km

I try to get some approximate distance between 2 points in mariaDB.

I use that SQL:

SELECT st_distance(POINT(50.6333,3.0667),p) from p

It outputs results such as:

0
1.9128040446888426
8.103248262271125

It seems mariaDB does not handle SRID. Is there a way to convert these values to km? (looks like multiplying by 110 is quite correct)

My goal is to avoid handling maths such as sin, cos, atan and approximate result is ok for me.

The result returned by st_distance are not kilometer but minutes. For a circumference of the equator of d = 40.075km, the distance between two minutes is d / 360 = 111,319 km.

While the distance between the latitudes is constant, the distance between the longitudes from the equator to the pole caps decreases constantly. According to your example the point from one location must be somewhere in France, where the distance between longitudes is around 70km.

Since you don't want to use the Haversine formula, you can also use the Pythagorean theorem to get a more accurate result:

# Distance between Eiffel tower and Lille

SELECT ST_DISTANCE(GeomFromText("Point(50.6333 3.0669)"), GeomFromText("Point(48.853. 2.348)")) * 111.38;
-> 213.84627307672486

select sqrt(pow((50.63333 - 48.853) * 111.38,2) + pow((3.0669 - 2.348) * 70, 2));
->204.57903071304386

ST_DISTANCE is designed for flat-earth advocates.

ST_DISTANCE_SPHERE is available in InnoDB as of 5.7.

https://dev.mysql.com/doc/refman/5.7/en/spatial-convenience-functions.html#function_st-distance-sphere

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM