>>> from helper import info; info()

页面更新时间： 2020-11-05 21:23:53



# 5.4. 生成新几何对象的方法¶

## 5.4.1. 集合论方法¶

### 边界与中心¶

>>> from shapely.geometry import LineString, MultiLineString, Point
>>> pprint=LineString
>>> coords = [((0, 0), (1, 1)), ((-1, 0), (1, 0))]
>>> lines = MultiLineString(coords)
>>> lines.boundary
>>> pprint(list(lines.boundary))
>>> lines.boundary.boundary
>>> lines.boundary.boundary.is_empty

True


object.centroid

>>> LineString([(0, 0), (1, 1)]).centroid
>>> LineString([(0, 0), (1, 1)]).centroid.wkt

'POINT (0.5 0.5)'


## 5.4.2. 构建新要素的方法¶

Shapely几何对象有一些方法产生新的要素，而不是由集理论得到的对象。

### object.buffer(distance,resolution=16)¶

>>> from shapely.geometry import LineString
>>> line = LineString([(0, 0), (1, 1), (0, 2), (2, 2), (3, 1), (1, 0)])
>>> dilated = line.buffer(0.5)
>>> eroded = dilated.buffer(-0.3)

>>> dilated

>>> eroded


>>> from shapely.geometry import Point
>>> p = Point(0, 0).buffer(10.0)
>>> len(p.exterior.coords)

66

>>> p.area

313.6548490545939


>>> q = Point(0, 0).buffer(10.0, 1)
>>> len(q.exterior.coords)

5

>>> q

>>> q.area

200.0


>>> from shapely.geometry import Polygon
>>> coords = [(0, 0), (0, 2), (1, 1), (2, 2), (2, 0), (1, 1), (0, 0)]
>>> bowtie = Polygon(coords)
>>> bowtie.is_valid

False

>>> bowtie

>>> clean = bowtie.buffer(0)
>>> clean.is_valid

True

>>> clean

>>> len(clean)

2

>>> list(clean[0].exterior.coords)

[(0.0, 0.0), (0.0, 2.0), (1.0, 1.0), (0.0, 0.0)]

>>> clean[0].exterior

>>> list(clean[0].exterior.coords)

[(0.0, 0.0), (0.0, 2.0), (1.0, 1.0), (0.0, 0.0)]

>>> clean[0].exterior.coords

<shapely.coords.CoordinateSequence at 0x7f77c1c83da0>

>>> list(clean[1].exterior.coords)

[(1.0, 1.0), (2.0, 2.0), (2.0, 0.0), (1.0, 1.0)]


### object.convex_hull¶

>>> Point(0, 0).convex_hull

>>> from shapely.geometry import MultiPoint
>>> MultiPoint([(0, 0), (1, 1)]).convex_hull

>>> from shapely.geometry import MultiPoint
>>> MultiPoint([(0, 0), (1, 1), (1, -1)]).convex_hull


### object.envelope¶

>>> from shapely.geometry import Point
>>> Point(0, 0).envelope

>>> from shapely.geometry import MultiPoint
>>> MultiPoint([(0, 0), (1, 1)]).envelope


### object.simplify(tolerance,preserve_topology=True)¶

>>> p = Point(0.0, 0.0)
>>> x = p.buffer(1.0)
>>> x.area

3.1365484905459384

>>> len(x.exterior.coords)

66

>>> s = x.simplify(0.05, preserve_topology=False)
>>> s.area

3.0614674589207187

>>> len(s.exterior.coords)

17


## 5.4.3. 相减操作¶

object.difference(other)

>>> a = Point(1, 1).buffer(1.5)
>>> b = Point(2, 1).buffer(1.5)
>>> a.difference(b)


Shapely不能代表一个对象和作为单个对象的低维数对象的不同 （例如一个多边形与一条线或一个点的差异） 在这些例子中，不同的方法返回 一个叫做‘self’的对象的副本。

## 5.4.4. 相交操作¶

object.intersection(other) ， 返回一个对象与另一个几何对象交集的代表。

>>> a = Point(1, 1).buffer(1.5)
>>> b = Point(2, 1).buffer(1.5)
>>> a.intersection(b)


object.symmetric\_difference(other) 返回一个在这个对象不在另一个几何对象的点的代表， 并且其它对象的点不在这个几何对象中。

>>> a = Point(1, 1).buffer(1.5)
>>> b = Point(2, 1).buffer(1.5)
>>> a.symmetric_difference(b)


## 5.4.5. object.union(other)¶

>>> a = Point(1, 1).buffer(1.5)
>>> b = Point(2, 1).buffer(1.5)
>>> a.union(b)


>>> a.union(b).boundary

>>> a.boundary.union(b.boundary)


union() 是一个找到许多对象的累积集合的昂贵的方式。 shapely.ops.cascaded_union() 是一个更有效的方法。