Shapely 2.0.0 中文文档

  1. 主页
  2. 文档
  3. Shapely 2.0.0 中文文档
  4. User Manual
  5. Predicates and Relationships(谓词和关系)
  6. Unary Predicates(一元谓词——属性)

Unary Predicates(一元谓词——属性)

Standard unary predicates are implemented as read-only property attributes. An example will be shown for each.

一元谓词作为只读属性实现。我们将通过每一个例子展示。

  • object.has_z

Returns True if the feature has not only x and y, but also z coordinates for 3D (or so-called, 2.5D) geometries.

如果要素不仅有x和y,而且还有3D(或所谓的2.5D)几何形状的z坐标,则返回True。

>>> Point(0, 0).has_z
False
>>> Point(0, 0, 0).has_z
True
  • object.is_ccw

Returns True if coordinates are in counter-clockwise order (bounding a region with positive signed area). This method applies to LinearRing objects only.

如果坐标按逆时针顺序排列,则返回True(以正面标记的面积来框定区域)。该方法只适用于LinearRing对象。

New in version 1.2.10.

>>> LinearRing([(1,0), (1,1), (0,0)]).is_ccw
True

A ring with an undesired orientation can be reversed like this:

非预期方向的环可以通过以下代码进行翻转操作:

>>> ring = LinearRing([(0,0), (1,1), (1,0)])
>>> ring.is_ccw
False
>>> ring2 = LinearRing(list(ring.coords)[::-1])
>>> ring2.is_ccw
True
  • object.is_empty

Returns True if the feature’s interior and boundary (in point set terms) coincide with the empty set.

如果要素的内部和边界为空集,则返回True。

>>> Point().is_empty
True
>>> Point(0, 0).is_empty
False
Note:With the help of the operator module’s attrgetter() function, unary predicates such as is_empty can be easily used as predicates for the built in filter().使用python内置模块operator中的attrgetter()函数,一元谓词is_empty 能够用作python内置函数filter()的谓词,来对列表中的对象进行批量判断和过滤。
>>> from operator import attrgetter
>>> empties = filter(attrgetter('is_empty'), [Point(), Point(0, 0)])
>>> len(list(empties))
1
  • object.is_ring

Returns True if the feature is a closed and simple LineString. A closed feature’s boundary coincides with the empty set.

如果要素是一个闭合的、简单的LineString,返回True。一个封闭的要素的边界与空集重合。

>>> LineString([(0, 0), (1, 1), (1, -1)]).is_ring
False
>>> LinearRing([(0, 0), (1, 1), (1, -1)]).is_ring
True

This property is applicable to LineString and LinearRing instances, but meaningless for others.

该属性适用于LineString和LinearRing实例,但对其他实例无意义。

  • object.is_simple

Returns True if the feature does not cross itself.

如果该要素不自相交,则返回True。

Note:The simplicity test is meaningful only for LineStrings and LinearRings.该属性只对LineStrings和LinearRings有意义。
>>> LineString([(0, 0), (1, 1), (1, -1), (0, 1)]).is_simple
False

Operations on non-simple LineStrings are fully supported by Shapely.

Shapely完全支持对非简单LineStrings的操作。

  • object.is_valid

Returns True if a feature is “valid” in the sense of [1].

如果要素是”有效 “的,则返回True。

Note:The validity test is meaningful only for Polygons and MultiPolygons. True is always returned for other types of geometries.有效性测试只对多边形Polygons 和复合多边形MultiPolygons有意义。对于其他类型的几何要素,总是返回True。

A valid Polygon may not possess any overlapping exterior or interior rings. A valid MultiPolygon may not collect any overlapping polygons. Operations on invalid features may fail.

一个有效的多边形Polygon 不得拥有任何重叠的外部或内部环。一个有效的复合多边形MultiPolygon不会与任何多边形重叠。对无效要素进行的操作可能会失败。

>>> MultiPolygon([Point(0, 0).buffer(2.0), Point(1, 1).buffer(2.0)]).is_valid
False

The two points above are close enough that the polygons resulting from the buffer operations (explained in a following section) overlap.

上述两点足够接近,以至于由缓冲操作(在下一节中解释)产生的多边形会重叠。

Note:The is_valid predicate can be used to write a validating decorator that could ensure that only valid objects are returned from a constructor function.is_valid谓词可以用来编写一个验证装饰器,可以确保只有有效的对象从构造函数返回。
from functools import wraps
def validate(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        ob = func(*args, **kwargs)
        if not ob.is_valid:
            raise TopologicalError(
                "Given arguments do not determine a valid geometric object")
        return ob
    return wrapper
>>> @validate  
... def ring(coordinates):
...     return LinearRing(coordinates)

... 
>>> coords = [(0, 0), (1, 1), (1, -1), (0, 1)]
>>> ring(coords)  
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 7, in wrapper
shapely.geos.TopologicalError: Given arguments do not determine a valid geometric object
标签 ,
这篇文章对您有用吗?

我们要如何帮助您?

欢迎留下您的宝贵建议

Please enter your comment!
Please enter your name here