Almost every binary predicate method has a counterpart that returns a new geometric object. In addition, the set-theoretic boundary of an object is available as a read-only attribute.
几乎每一个二元谓词方法都有一个对应的方法来返回一个新的几何对象的副本。此外,从集合论的角度,一个几何对象的边界可以作为一个只读的属性得到。
Note |
These methods will always return a geometric object. An intersection of disjoint geometries for example will return an empty GeometryCollection, not None or False . To test for a non-empty result, use the geometry’s is_empty property. 这些方法将总是返回一个几何对象。例如,一个不相交的几何体的交集将返回一个空的GeometryCollection,而不是None或False。要测试一个非空的结果,请使用几何对象的is_empty属性。 |
- object.boundary
Returns a lower dimensional object representing the object’s set-theoretic boundary.
按集合论,返回一个代表该对象边界的更低一维的几何对象。
The boundary of a polygon is a line, the boundary of a line is a collection of points. The boundary of a point is an empty (null) collection.
多边形的边界是一条线,一条线的边界是一系列点的集合。一个点的边界是一个空集。
>>> coords = [((0, 0), (1, 1)), ((-1, 0), (1, 0))]
>>> lines = MultiLineString(coords)
>>> lines.boundary
<shapely.geometry.multipoint.MultiPoint object at 0x...>
>>> pprint(list(lines.boundary))
[<shapely.geometry.point.Point object at 0x...>,
<shapely.geometry.point.Point object at 0x...>,
<shapely.geometry.point.Point object at 0x...>,
<shapely.geometry.point.Point object at 0x...>]
>>> lines.boundary.boundary
<shapely.geometry.collection.GeometryCollection object at 0x...>
>>> lines.boundary.boundary.is_empty
True
See the figures in LineStrings and Collections of Lines for the illustration of lines and their boundaries.
关于线及其边界的说明,请参见LineStrings和Line Collections of Lines中的图表。
- object.centroid
Returns a representation of the object’s geometric centroid (point).
返回几何对象的几何中心点,即质心。
>>> LineString([(0, 0), (1, 1)]).centroid
<shapely.geometry.point.Point object at 0x...>
>>> LineString([(0, 0), (1, 1)]).centroid.wkt
'POINT (0.5000000000000000 0.5000000000000000)'
Note |
The centroid of an object might be one of its points, but this is not guaranteed. 一个对象的质心可能是组成它的一个点,但这并不是必然的。 |
- object.difference(other)
Returns a representation of the points making up this geometric object that do not make up the other object.
返回构成该几何对象的且不构成另一个对象的点的集合。即,相减操作。
>>> a = Point(1, 1).buffer(1.5)
>>> b = Point(2, 1).buffer(1.5)
>>> a.difference(b)
<shapely.geometry.polygon.Polygon object at 0x...>
Note |
The buffer() method is used to produce approximately circular polygons in the examples of this section; it will be explained in detail later in this manual.在本节的例子中,buffer()方法被用来产生近似圆形的多边形;在本手册的后面,将详细解释该方法。 |
(Source code, png, hires.png, pdf)
Note |
Shapely can not represent the difference between an object and a lower dimensional object (such as the difference between a polygon and a line or point) as a single object, and in these cases the difference method returns a copy of the object named self .difference 操作只适用于二维对象,不能用在低维对象上(例如线和点要素),在这些情况下,difference 方法返回一个名为self的对象的副本。 |
- object.intersection(other)
Returns a representation of the intersection of this object with the other geometric object.
返回该对象与另一个几何对象的交集。即,相交操作。
>>> a = Point(1, 1).buffer(1.5)
>>> b = Point(2, 1).buffer(1.5)
>>> a.intersection(b)
<shapely.geometry.polygon.Polygon object at 0x...>
See the figure under symmetric_difference()
below.
见symmetric_difference()下的图。
- object.symmetric_difference(other)
Returns a representation of the points in this object not in the other geometric object, and the points in the other not in this geometric object.
返回属于该对象,但不属于另一个几何对象的元素集合。即,对称差操作。
>>> a = Point(1, 1).buffer(1.5)
>>> b = Point(2, 1).buffer(1.5)
>>> a.symmetric_difference(b)
<shapely.geometry.multipolygon.MultiPolygon object at ...>
(Source code, png, hires.png, pdf)
- object.union(other)
Returns a representation of the union of points from this object and the other geometric object.
返回该对象和另一个几何对象的点集的合集。即,合并操作。
The type of object returned depends on the relationship between the operands. The union of polygons (for example) will be a polygon or a multi-polygon depending on whether they intersect or not.
返回的对象类型取决于运算对象之间的关系。例如,多边形的合并将是一个多边形或一个复合多边形,这取决于它们是否相交。
>>> a = Point(1, 1).buffer(1.5)
>>> b = Point(2, 1).buffer(1.5)
>>> a.union(b)
<shapely.geometry.polygon.Polygon object at 0x...>
The semantics of these operations vary with type of geometric object. For example, compare the boundary of the union of polygons to the union of their boundaries.
这些操作的语义因几何对象的类型而异。例如,比较多边形合并之后的边界和它们的边界的合并结果。
>>> a.union(b).boundary
<shapely.geometry.polygon.LinearRing object at 0x...>
>>> a.boundary.union(b.boundary)
<shapely.geometry.multilinestring.MultiLineString object at 0x...>
(Source code, png, hires.png, pdf)
Note |
union() is an expensive way to find the cumulative union of many objects. See shapely.ops.unary_union() for a more effective method.对于较多对象的合并操作,union()需要较多计算资源。请参阅shapely.ops.unary_union()以获得更有效的方法。 |
Several of these set-theoretic methods can be invoked using overloaded operators:
这些集合论的方法中有几个可以通过运算符来调用。
- intersection can be accessed with and, &
- union can be accessed with or, |
- difference can be accessed with minus, –
- symmetric_difference can be accessed with xor, ^
>>> from shapely import wkt
>>> p1 = wkt.loads('POLYGON((0 0, 1 0, 1 1, 0 1, 0 0))')
>>> p2 = wkt.loads('POLYGON((0.5 0, 1.5 0, 1.5 1, 0.5 1, 0.5 0))')
>>> (p1 & p2).wkt
'POLYGON ((1 0, 0.5 0, 0.5 1, 1 1, 1 0))'
>>> (p1 | p2).wkt
'POLYGON ((0.5 0, 0 0, 0 1, 0.5 1, 1 1, 1.5 1, 1.5 0, 1 0, 0.5 0))'
>>> (p1 - p2).wkt
'POLYGON ((0.5 0, 0 0, 0 1, 0.5 1, 0.5 0))'
>>> (p1 ^ p2).wkt
'MULTIPOLYGON (((0.5 0, 0 0, 0 1, 0.5 1, 0.5 0)), ((1 0, 1 1, 1.5 1, 1.5 0, 1 0)))'