MySQL GIS / 空间扩展 - 融化我的大脑
我是 MySQL 中的 GIS 新手,它正在融化我的大脑!
我创建了一个表“地点”,如下所示:
CREATE TABLE `places` (
`id` int(6) unsigned zerofill NOT NULL auto_increment,
`business_name` varchar(100) NOT NULL,
`street_postcode` varchar(10) NOT NULL,
`longitude` decimal(22,20) NOT NULL,
`latitude` decimal(22,20) NOT NULL,
`coord` point NOT NULL,
UNIQUE KEY `id` (`id`),
) ENGINE=InnoDB AUTO_INCREMENT=25080 DEFAULT CHARSET=utf8
id、business_name 和 street_postcode 插入表中
,然后用 PHP 中的经度和纬度坐标更新表,如下所示:
UPDATE `places` SET `longitude` = '".$longitude."', `latitude` = '".$latitude."', `coord` = GeomFromText('POINT(".$coord.")') WHERE `id` = '".$row->id."' LIMIT 1
这一切似乎都很顺利,但我陷入困境当尝试找到距离 X 坐标最近的位置时。如何找到距 X 经度和纬度最近的 10 个地点?
这似乎不起作用:
SELECT business_name, street_postcode, ROUND(GLength(LineStringFromWKB(LineString(AsBinary(coord), AsBinary('51.49437081 -0.2275573')))))
AS distance FROM places ORDER BY distance ASC LIMIT 10;
预先感谢!
I'm new to GIS in MySQL and it's melting my brain!
I've created a table "places" as so:
CREATE TABLE `places` (
`id` int(6) unsigned zerofill NOT NULL auto_increment,
`business_name` varchar(100) NOT NULL,
`street_postcode` varchar(10) NOT NULL,
`longitude` decimal(22,20) NOT NULL,
`latitude` decimal(22,20) NOT NULL,
`coord` point NOT NULL,
UNIQUE KEY `id` (`id`),
) ENGINE=InnoDB AUTO_INCREMENT=25080 DEFAULT CHARSET=utf8
id, business_name and street_postcode where inserted into the table
I then updated the table with longitude and latitude coordinates in PHP as so:
UPDATE `places` SET `longitude` = '".$longitude."', `latitude` = '".$latitude."', `coord` = GeomFromText('POINT(".$coord.")') WHERE `id` = '".$row->id."' LIMIT 1
This all seems to have gone well, but I'm stuck when trying to find the nearest place to X coordinates. How do I find the 10 nearest places to X longitude and latitude?
This does not seem to work:
SELECT business_name, street_postcode, ROUND(GLength(LineStringFromWKB(LineString(AsBinary(coord), AsBinary('51.49437081 -0.2275573')))))
AS distance FROM places ORDER BY distance ASC LIMIT 10;
Eternal thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为 MySQL 中并未真正实现空间函数。他们删除了所有代码,但没有实现所有方法。
我建议使用 PostGIS 或 SpatiaLite。
That is because the spatial functions are not really implmented in MySQL. They stubbed out all the code but have not implemented all the methods.
I recommend using PostGIS or SpatiaLite.