简体   繁体   中英

Mysql Geo Spatial Search returns strange results

I am currently trying to setup a spatial search in Mysql and PHP. For this I use the haversin formula that I found in the following link:

http://www.notaires.fr/sites/default/files/geo_searchjkkjkj_0.pdf

At the moment for test purposes I use the direct query (slowest approach of course) but it acts really strange.

From one of my database entries I copy the latitude and longitude information for and use them for test purposes but I still get an empty result back. So I removed the having statement to look what distance actually comes out as a result for my images and it is 4098.9334608610825 for the database entry where I took the coordinates from.

I am using the exact formula that is shown on slide 8 (of course I adapted the field names and table name to my database) in the powerpoint of the link but I just keep getting these strange results.

Any idea or suggestions how I can solve this? Or maybe there are even much better ways to do the spatial search if so, please let me know (just not sphinx for now because I can't install anything like that on my servers at the moment as I'm not the owner)

在此处输入图片说明

Here you can see the picture with my results. The first result is the database entry where I took my coordinates from.

Thanks in advance.

Try

select (acos(sin(radians(@orig_lat)) * sin(radians(latitude)) + cos(radians(@orig_lat)) * cos(radians(latitude)) * cos(radians(longitude) - radians(@orig_lon))) * 6378) as dist

Think I writed it correctly

Try using a different version of the spherical cosine law formula. It is this for MySQL:

  DEGREES(ACOS(COS(RADIANS(lat1))
             * COS(RADIANS(lat2))
             * COS(RADIANS(long1 - long2))
             + SIN(RADIANS(lat1))
             * SIN(RADIANS(lat2)))) AS distance_in_degrees

You can then convert these degrees to km. Multiply by 111.045 to do that.

If you need a more numerically stable version of the great circle distance computation (you probably don't), try the Vincenty formula:

DEGREES(
  ATAN2(
    SQRT(
    POW(COS(RADIANS(lat2))*SIN(RADIANS(lon2-lon1)),2) +
    POW(COS(RADIANS(lat1))*SIN(RADIANS(lat2)) -
         (SIN(RADIANS(lat1))*COS(RADIANS(lat2)) *
          COS(RADIANS(lon2-lon1))) ,2)),
  SIN(RADIANS(lat1))*SIN(RADIANS(lat2)) +
  COS(RADIANS(lat1))*COS(RADIANS(lat2))*COS(RADIANS(lon2-lon1))))

It, too, returns the distance in degrees.

This material is written up here. http://www.plumislandmedia.net/mysql/haversine-mysql-nearest-loc/

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