检查 LinearRing 是否包含一个点在 Shapely 中总是 returns False
Checking if LinearRing contains a point always returns False in Shapely
这是 的变体,不是重复的
我想确定某个图形中的一个点是否由多个点组成,但为了简单起见,我做了这个测试,但失败了。我定义了一个 2x2 的正方形,第一个点应该在里面,第二个点在外面,但是都调用 return False
import unittest
from shapely.geometry import LineString,Point,LinearRing
class TestTools(unittest.TestCase):
def test_isInside(self):
points = [
[0,0],
[2,0],
[2,2],
[0,2]
]
ring=LinearRing(points)
print(ring)
print(Point(1,2))
self.assertEqual(ring.contains(Point(1,1)),True)
self.assertEqual(ring.contains(Point(3,3)),False)
输出是
LINEARRING (0 0, 2 0, 2 2, 0 2, 0 0)
POINT (1 2)
======================================================================
FAIL: test_isInside (__main__.TestTools)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_tools.py", line 17, in test_isInside
self.assertEqual(ring.contains(Point(1,1)),True)
AssertionError: False != True
很明显我做错了什么(简单?)。
根据 Shapely documentation 存在三种基本类型的几何对象:点、曲线和曲面。 LinearRing
是曲线类型的实现之一。
引用文档:
A Curve has an interior set consisting of the infinitely many points along its length (imagine a Point dragged in space), a boundary set consisting of its two end points, and an exterior set of all other points. A Curve has a topological dimension of 1.
这是关于 object.contains(other)
方法的内容:
Returns True
if no points of other lie in the exterior of the object and at least one point of the interior of other lies in the interior of object.
因此,在您的情况下,点 Point(1, 1)
、Point(3, 3)
不在曲线 LinearRing([[0, 0], [2, 0], [2, 2], [0, 2]])
上,而是在曲线之外,在外部,因此您的测试 return False
.
为了检查这些点是否被 LinearRing
包围,在您的简单情况下,您可以从这些环构造 polygons 并进行相同的检查:
>>> Polygon(ring).contains(Point(1, 1))
True
>>> Polygon(ring).contains(Point(3, 3))
False
这是
我想确定某个图形中的一个点是否由多个点组成,但为了简单起见,我做了这个测试,但失败了。我定义了一个 2x2 的正方形,第一个点应该在里面,第二个点在外面,但是都调用 return False
import unittest
from shapely.geometry import LineString,Point,LinearRing
class TestTools(unittest.TestCase):
def test_isInside(self):
points = [
[0,0],
[2,0],
[2,2],
[0,2]
]
ring=LinearRing(points)
print(ring)
print(Point(1,2))
self.assertEqual(ring.contains(Point(1,1)),True)
self.assertEqual(ring.contains(Point(3,3)),False)
输出是
LINEARRING (0 0, 2 0, 2 2, 0 2, 0 0)
POINT (1 2)
======================================================================
FAIL: test_isInside (__main__.TestTools)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_tools.py", line 17, in test_isInside
self.assertEqual(ring.contains(Point(1,1)),True)
AssertionError: False != True
很明显我做错了什么(简单?)。
根据 Shapely documentation 存在三种基本类型的几何对象:点、曲线和曲面。 LinearRing
是曲线类型的实现之一。
引用文档:
A Curve has an interior set consisting of the infinitely many points along its length (imagine a Point dragged in space), a boundary set consisting of its two end points, and an exterior set of all other points. A Curve has a topological dimension of 1.
这是关于 object.contains(other)
方法的内容:
Returns
True
if no points of other lie in the exterior of the object and at least one point of the interior of other lies in the interior of object.
因此,在您的情况下,点 Point(1, 1)
、Point(3, 3)
不在曲线 LinearRing([[0, 0], [2, 0], [2, 2], [0, 2]])
上,而是在曲线之外,在外部,因此您的测试 return False
.
为了检查这些点是否被 LinearRing
包围,在您的简单情况下,您可以从这些环构造 polygons 并进行相同的检查:
>>> Polygon(ring).contains(Point(1, 1))
True
>>> Polygon(ring).contains(Point(3, 3))
False