```>>> from env_helper import info; info()
```
```页面更新时间： 2023-04-15 20:03:05

Linux发行版本: Debian GNU/Linux 12 (bookworm)
操作系统内核: Linux-6.1.0-7-amd64-x86_64-with-glibc2.36
Python版本: 3.11.2
```

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

7.5.1. 几何对象的边界与中心¶

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

```>>> len(lines.geoms)
```
```2
```
```>>> for geom in lines.geoms:
>>>     print(geom)
```
```LINESTRING (0 0, 1 1)
LINESTRING (-1 0, 1 0)
```
```>>> 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)'
```

7.5.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)
```
```65
```
```>>> p.area
```
```313.6548490545941
```

```>>> 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.geoms)
```
```2
```
```>>> list(clean.geoms[0].exterior.coords)
```
```[(0.0, 0.0), (0.0, 2.0), (1.0, 1.0), (0.0, 0.0)]
```
```>>> clean.geoms[0].exterior
```
```>>> list(clean.geoms[0].exterior.coords)
```
```[(0.0, 0.0), (0.0, 2.0), (1.0, 1.0), (0.0, 0.0)]
```
```>>> clean.geoms[0].exterior.coords
```
```<shapely.coords.CoordinateSequence at 0x7fcffed64950>
```
```>>> list(clean.geoms[0].exterior.coords)
```
```[(0.0, 0.0), (0.0, 2.0), (1.0, 1.0), (0.0, 0.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.1365484905459398
```
```>>> len(x.exterior.coords)
```
```65
```
```>>> s = x.simplify(0.05, preserve_topology=False)
>>> s.area
```
```3.061467458920719
```
```>>> len(s.exterior.coords)
```
```17
```

7.5.3. 二元操作¶

相减操作¶

`object.difference(other)`

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

相交操作¶

`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)
```

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()` 是一个更有效的方法。