MySQL 中的几何查询

标签 mysql

SELECT `NAME` FROM world_boundaries 
WHERE Within(GeomFromText('POINT(8.5929098 50.0286713)'), ogc_geom);

为什么此查询应返回 1 个国家/地区,却返回 3 个国家/地区。 它返回: 1.法国 2. 德国(正确之一) 3.美国

我的国家/地区边界数据是否有缺陷,或者这些几何函数是否有一些技巧?

最佳答案

MySQL 还无法检查 Poly 中的点。它将检查您的点是否在 ([minX, maxX], [minY, maxY]) 范围内。另外,我在 MySQL 文档中没有看到 within

Note

Currently, MySQL does not implement these functions according to the specification. Those that are implemented return the same result as the corresponding MBR-based functions. This includes functions in the following list other than Distance() and Related().

These functions may be implemented in future releases with full support for spatial analysis, not just MBR-based support.

From here

我用了point-in-poly ray-tracing算法,如果线路不长,在极地之外该算法可能相当准确。否则,您将需要求解球面三角形。

简单的实现是这样的:

create table point (x int, y int...) /* int values (int(lat * 10^6)) for better performance */
create table poly (x1 int, y1 int, x2 int, y2 int...) /* temporary table for 1 poly only */

选择多边形内点的查询:

select point_id from point, poly
where ((y<y2 and y>=y1) or (y<y1 and y>=y2)) and ((x<x2 and x>=x1) or (x<x1 and x>=x2))
group by point_id
having sum(x1*(y2-y1)*sign(y2-y1) < x*(y2-y1)*sign(y2-y1) - (y-y1)*(x2-x1)) % 2

在 2.5Ghz 的 Pentium 上,在 0.06 秒内对 20,000 个点与 40 条线进行计数。 :)

关于MySQL 中的几何查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1902956/

相关文章:

php连接数据库mysql的简单代码

MySql错误1261 : Row 1 doesn't contain data for all columns

php - 如何从 JSON php 中删除额外的数组

php - 如果数组键值为空,如何自动生成数组键值(来自查询)

php - 将lighttpd服务器移植到Android

php - 如何通过表单标签将数据发送到另一个页面?

mysql - java - Debezium 无法解析 DDL(创建过程)语句 mysql 连接器

MySQL 存储过程总是选择 True

java - MySQL SELECT 在 JDBC 中不起作用

mysql - 更新存在 fk 约束问题的所有行