简体   繁体   English

检查LinearRing是否包含点在Shapely中始终返回False

[英]Checking if LinearRing contains a point always returns False in Shapely

This is variation of Shapely contains(point) always gives False , not a duplicate 这是Shapely contains(point)的变体, 总是给出False ,而不是重复

I want to determine if a point inside a certain figure created of many points, but to start simple I do this test, which fails. 我想确定某个图形中的某个点是否由许多点创建,但是为了简单起见,我进行了此测试,但失败了。 I define a 2x2 square, and the first point should be inside, the second outside, but both calls return False 我定义了一个2x2的正方形,第一个点应该在内部,第二个点应该在内部,但是两个调用都返回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)

The output is 输出是

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

So obviously I'm doing something (simple?) wrong. 所以很明显我在做错事(简单?)。

According to the Shapely documentation there are three fundamental types of geometric objects: points, curves, and surfaces. 根据Shapely文档 ,几何对象有三种基本类型:点,曲线和曲面。 The LinearRing is one of the implementations of the curve type. LinearRing是曲线类型的实现之一。

Citing the docs: 引用文档:

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. 曲线的拓扑尺寸为1。

And here is what it says about object.contains(other) method: 这是关于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 . 如果没有其他点位于对象的外部并且其他内部的至少一个点位于对象的内部,则返回True

So, in your case the points Point(1, 1) , Point(3, 3) don't lie on the curve LinearRing([[0, 0], [2, 0], [2, 2], [0, 2]]) but outside it, in the exterior, hence your tests return False . 因此,在您的情况下,点Point(1, 1)Point(3, 3)不在曲线LinearRing([[0, 0], [2, 0], [2, 2], [0, 2]])但在外部,在外部,因此您的测试返回False

In order to check if the points are enclosed by the LinearRing , in your simple case, you could construct polygons from those rings and do the same check: 为了检查点是否被LinearRing ,在您的简单情况下,您可以从这些环构造多边形并执行相同的检查:

>>> Polygon(ring).contains(Point(1, 1))
True

>>> Polygon(ring).contains(Point(3, 3))
False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM