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.
我用了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/