我在使用 mySQL 空间函数时遇到问题。我的目标是找出 LINESTRING 对象是否穿过 POLYGON 对象。为了确定这一点,我尝试用两个 LINESTRING 对象进行实验以确定它们是否交叉。
SET @ls='LINESTRING (0 0, 1 1, 2 2, 3 3)'; -- original linestring
SET @lp='LINESTRING (0 1, 1 2, 2 3, 3 4)'; -- parallel linestring
SET @lx='LINESTRING (0 3, 1 2, 2 1, 3 0)'; -- crossed linestring
我尝试了多种功能来实现我的目标:
SELECT crosses(GeomFromText(@ls), GeomFromText(@lx)); -- crossing linestrings
returns 0;
SELECT intersects(GeomFromText(@ls), GeomFromText(@lp)); -- parallel linestrings
returns 1;
SELECT overlaps(GeomFromText(@ls), GeomFromText(@lp)); -- parallel linestrings
returns 1;
我知道这是边界比较或某事的问题,但是有没有办法(或函数,或简单的解决方案)如何达到我的目标?另一种可能性是检查 LINESTRING 中的点是否在多边形内,但我想知道是否还有其他方法可以做到这一点?
MySQL Great Circle intersection (do two roads cross?)中提供的解决方案不幸的是,这对我没有帮助..
最佳答案
来自 'Functions That Test Spatial Relationships Between Geometries' 的 MySQL 文档 在 5.6.1 中:
MySQL originally implemented these functions such that they used object bounding rectangles and returned the same result as the corresponding MBR-based functions. As of MySQL 5.6.1, corresponding versions are available that use precise object shapes. These versions are named with an ST_ prefix. For example, Contains() uses object bounding rectangles, whereas ST_Contains() uses object shapes.
As of MySQL 5.6.1, there are also ST_ aliases for existing spatial functions that were already exact. For example, ST_IsEmpty() is an alias for IsEmpty()
例如,您的 intersects
函数返回 true,因为两个形状的最小外接矩形 (MBR) 确实 相交。如果您使用 ST_Intersects
(假设您有 MySQL 5.6.1 或更高版本),那么它将按预期返回 false。这些函数可与点、线和多边形一起使用 - 因此也应该解决您的“线相交多边形”查询(如果需要, ST_Crosses
也存在)。
关于php - MySQL LineString 与 Linestring 或 Polygon 的交集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9617531/