[英]Mysql Geo Spatial Search returns strange results
I am currently trying to setup a spatial search in Mysql and PHP. 我目前正在尝试在Mysql和PHP中设置空间搜索。 For this I use the haversin formula that I found in the following link:
为此,我使用在以下链接中找到的Haversin公式:
http://www.notaires.fr/sites/default/files/geo_searchjkkjkj_0.pdf 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.
因此,我删除了hading语句,以查看实际结果是我的图像出现了什么距离,而从中获取坐标的数据库条目为4098.9334608610825。
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. 我使用的是幻灯片的幻灯片中幻灯片8上显示的确切公式(当然,我将字段名和表名调整为我的数据库),但我一直在得到这些奇怪的结果。
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:
这是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. 然后,您可以将这些度数转换为km。 Multiply by 111.045 to do that.
乘以111.045即可。
If you need a more numerically stable version of the great circle distance computation (you probably don't), try the Vincenty formula: 如果您需要更大的圆距离计算的数字稳定版本(您可能不需要),请尝试使用Vincenty公式:
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/
http://www.plumislandmedia.net/mysql/haversine-mysql-nearest-loc/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.