```>>> from env_helper import info; info()
```
```页面更新时间： 2024-01-18 10:03:23

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

7.6. 合并线性要素¶

7.6.1. shapely.ops.polygonize(lines)¶

```>>> from shapely.ops import polygonize
>>> lines = [
>>>     ((0, 0), (1, 1)),
>>>     ((0, 0), (0, 1)),
>>>     ((0, 1), (1, 1)),
>>>     ((1, 1), (1, 0)),
>>>     ((1, 0), (0, 0))
>>>     ]
```

7.6.2. shapely.ops.linemerge(lines)¶

```>>> from shapely.ops import linemerge
>>> linemerge(lines)
```
```>>> pprint(list(linemerge(lines)))
```
```Pretty printing has been turned OFF
```

7.6.3. 级联合并¶

`shapely.ops.unary_union(geoms)` 返回一个给定几何对象合并的结果。例如：

```>>> from shapely.geometry import Point
>>> from shapely.ops import unary_union
>>> polygons = [Point(i, 0).buffer(0.7) for i in range(5)]
```
```>>> polygons[0].union(polygons[1]).area
```
```2.8052569090548314
```
```>>> polygons[0].area
```
```1.53690876036751
```
```>>> unary_union(polygons)
```

```>>> from shapely.geometry import MultiPolygon
>>> m = MultiPolygon(polygons)
>>> m.area
```
```7.684543801837549
```
```>>> unary_union(m).area
```
```6.610301355116799
```

7.6.5. 预制的几何操作¶

Shapely几何体能够被加工为一种可以支持高效批次的操作的形式。

`prepared.prep(ob)`

```>>> from shapely.geometry import Point
```
```>>> from shapely.prepared import prep
```
```>>> points = [...] # large list
```
```>>> from shapely.geometry import Point
>>> polygon = Point(0.0, 0.0).buffer(1.0)
>>> prepared_polygon = prep(polygon)
>>> prepared_polygon
```
```<shapely.prepared.PreparedGeometry object at 0x7f817831a5d0>
```
```>>> hits = filter(prepared_polygon.contains, points)
>>> hits
```
```<filter object at 0x7f8190213340>
```

7.6.6. 对象的有效性判断方法¶

`validation.explain_validity(ob)`

```>>> from shapely.geometry import Point
>>> from shapely.geometry import Polygon
>>> coords = [(0, 0), (0, 2), (1, 1), (2, 2), (2, 0), (1, 1), (0, 0)]
>>> p = Polygon(coords)
>>> from shapely.validation import explain_validity
>>> explain_validity(p)
```
```'Ring Self-intersection[1 1]'
```