Standard binary predicates are implemented as methods. These predicates evaluate topological, set-theoretic relationships. In a few cases the results may not be what one might expect starting from different assumptions. All take another geometric object as argument and return True
or False
.
标准的二元谓词通过方法实现。这些谓词评估拓扑与集合论的关系。在少数情况下,结果可能不是人们所期望的那样。所有的方法都以另一个几何对象作为参数,并返回Ture或False。
Returns True
if the two objects are of the same geometric type, and the coordinates of the two objects match precisely.
如果两个对象的几何类型相同,并且两个对象的坐标精确匹配,则返回True。
Returns True
if the set-theoretic boundary, interior, and exterior of the object coincide with those of the other.
如果对象的集合边界、内部和外部与另一个对象的边界、内部和外部重合,则返回True。
The coordinates passed to the object constructors are of these sets, and determine them, but are not the entirety of the sets. This is a potential “gotcha” for new users. Equivalent lines, for example, can be constructed differently.
传递给对象构造函数的坐标有以下几种,但不是全部的集合。这对新用户来说是一个潜在的 “麻烦”。例如,相同的线,可以用不同的方式构造。
>>> a = LineString([(0, 0), (1, 1)])
>>> b = LineString([(0, 0), (0.5, 0.5), (1, 1)])
>>> c = LineString([(0, 0), (0, 0), (1, 1)])
>>> a.equals(b)
True
>>> a == b
False
>>> b.equals(c)
True
>>> b == c
False
Returns True
if the object is within a specified tolerance.
如果对象在所有点上都近似等于另一个对象,并达到指定的小数位精度,则返回True。
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。
This predicate applies to all types, and is inverse to within()
. The expression a.contains(b) == b.within(a)
always evaluates to True
.
这种谓词适用于所有类型,并且与within()相反。表达式a.contains(b) == b.within(a)总是评定为Ture。
>>> coords = [(0, 0), (1, 1)]
>>> LineString(coords).contains(Point(0.5, 0.5))
True
>>> Point(0.5, 0.5).within(LineString(coords))
True
A line’s endpoints are part of its boundary and are therefore not contained.
一条线的端点是其边界的一部分,因此不包含在内。
>>> LineString(coords).contains(Point(1.0, 1.0))
False
Note:Binary predicates can be used directly as predicates for filter() or itertools.ifilter(). 二元谓词可以直接作为filter()或itertools.ifilter()的谓词使用。
>>> line = LineString(coords)
>>> contained = list(filter(line.contains, [Point(), Point(0.5, 0.5)]))
>>> len(contained)
1
>>> contained
[<POINT (0.5 0.5)>]
Returns True
if every point of other is a point on the interior or boundary of object. This is similar to object.contains(other)
except that this does not require any interior points of other to lie in the interior of object.
如果对象的内部包含另外一个对象的边界和内部,则返回True。这与object.contains(other)相似,只是其没有对内部点的要求。
Returns True
if every point of object is a point on the interior or boundary of other. This is equivalent to other.covers(object)
.
如果对象的每一个点都是其他对象的内部或边界上的一个点,则返回真。其等价于other.covers(object)。
New in version 1.8.
Returns True
if the interior of the object intersects the interior of the other but does not contain it, and the dimension of the intersection is less than the dimension of the one or the other.
如果该对象的内部与另一对象的内部相交但不包含该对象,并且相交处的维数小于其本身或另一个对象的维度,则返回True。
>>> LineString(coords).crosses(LineString([(0, 1), (1, 0)]))
True
A line does not cross a point that it contains.
一条线不跨越它所包含的点。
>>> LineString(coords).crosses(Point(0.5, 0.5))
False
Returns True
if the boundary and interior of the object do not intersect at all with those of the other.
如果该对象的边界和内部与其他对象不相交(按集合论,没有任何同样的元素),则返回True。
>>> Point(0, 0).disjoint(Point(1, 1))
True
This predicate applies to all types and is the inverse of intersects()
.
这种谓词适用于所有类型,可以视为intersects()的逆向。
Returns True
if the boundary or interior of the object intersect in any way with those of the other.
如果该对象的边界或内部以任何方式与另一对象的边界或内部相交,则返回True。
In other words, geometric objects intersect if they have any boundary or interior point in common.
换句话说,如果几何对象有任何边界或内部的共同点,它们就会相交。
Returns True
if the geometries have more than one but not all points in common, have the same dimension, and the intersection of the interiors of the geometries has the same dimension as the geometries themselves.
如果几何要素有一个以上但不是所有的共同点,具有相同的维数,并且几何要素内部的交点与几何要素本身具有相同的维数,则返回True。
也就是两个对象的intersects()为Ture,但是相互之间的within()为False,则返回Ture。
Returns True
if the objects have at least one point in common and their interiors do not intersect with any part of the other.
如果对象的边界仅与另一个对象的边界相交,并且不与另一个对象的其他部分相交,则返回True。
Overlapping features do not therefore touch, another potential “gotcha”. For example, the following lines touch at (1, 1)
, but do not overlap.
重叠(Overlapping )的要素不会接触(touch),这是另一个潜在的 “麻烦”。例如,下面的线条在(1,1)处接触,但不重叠。
>>> a = LineString([(0, 0), (1, 1)])
>>> b = LineString([(1, 1), (2, 2)])
>>> a.touches(b)
True
Returns True
if the object’s boundary and interior intersect only with the interior of the other (not its boundary or exterior).
如果对象的边界和内部仅与另一个对象的内部相交(而不是其边界或外部),则返回True。
This applies to all types and is the inverse of contains()
.
适用于所有类型,是contains()的逆运算。
Used in a sorted()
key, within()
makes it easy to spatially sort objects. Let’s say we have 4 stereotypic features: a point that is contained by a polygon which is itself contained by another polygon, and a free spirited point contained by none
在sorted()键中使用,within()使对象的空间排序变得容易。假设我们有4个要素:一个被一个多边形所包含的点,而这个多边形本身又被另一个多边形所包含,以及一个没有被包含的自由点。
>>> a = Point(2, 2)
>>> b = Polygon([[1, 1], [1, 3], [3, 3], [3, 1]])
>>> c = Polygon([[0, 0], [0, 4], [4, 4], [4, 0]])
>>> d = Point(-1, -1)
and that copies of these are collected into a list
并且这些副本被收集到一个列表中
>>> features = [c, a, d, b, c]
that we’d prefer to have ordered as [d, c, c, b, a]
in reverse containment order. As explained in the Python Sorting HowTo, we can define a key function that operates on each list element and returns a value for comparison. Our key function will be a wrapper class that implements __lt__()
using Shapely’s binary within()
predicate.
我们倾向于将其排序为 [d, c, c, b, a] 的反向包含顺序。正如在 Python Sorting HowTo 中所解释的,我们可以定义一个键函数,对每个列表元素进行操作,并返回一个值进行比较。我们的键函数将是一个包装类,它使用 Shapely 的二进制 within() 谓词实现 。
>>> class Within:
... def __init__(self, o):
... self.o = o
... def __lt__(self, other):
... return self.o.within(other.o)
As the howto says, the less than comparison is guaranteed to be used in sorting. That’s what we’ll rely on to spatially sort. Trying it out on features d and c, we see that it works.
正如Python Sorting HowTo 中所解释的,小于的比较是保证在排序中使用的。这就是我们要依靠的空间排序。在特征d和c上试了一下,我们发现它是有效的。
>>> Within(d) < Within(c)
False
It also works on the list of features, producing the order we want.
它也在功能列表上工作,产生我们想要的顺序。
>>> [d, c, c, b, a] == sorted(features, key=Within, reverse=True)
True