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

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

# 6.5. 合并线性要素¶

## 6.5.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))
>>>     ]
```

## 6.5.2. shapely.ops.linemerge(lines)¶

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

## 6.5.3. 级联合并¶

shapely的级联合并

`shapely.ops.cascaded_union(geoms)`

```>>> from shapely.geometry import Point
>>> 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
```
```>>> cascaded_union(polygons)
```
```/tmp/ipykernel_256442/4190037777.py:1: ShapelyDeprecationWarning: The 'cascaded_union()' function is deprecated. Use 'unary_union()' instead.
```

```>>> from shapely.geometry import MultiPolygon
>>> m = MultiPolygon(polygons)
>>> m.area
```
```7.684543801837549
```
```>>> cascaded_union(m).area
```
```/tmp/ipykernel_256442/1358075078.py:1: ShapelyDeprecationWarning: The 'cascaded_union()' function is deprecated. Use 'unary_union()' instead.
```
```6.610301355116799
```

## 6.5.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 0x7f06fc21ca50>
```
```>>> hits = filter(prepared_polygon.contains, points)
>>> hits
```
```<filter object at 0x7f0721087370>
```

## 6.5.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]'
```