```>>> from env_helper import info; info()
```
```页面更新时间： 2023-06-24 11:31:07

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

# 7.2. Shapely 中几何要素的操作¶

## 7.2.1. 一般的属性与方法¶

`object.area` 返回对象的面积

`object.bounds` 返回一个元组`(minx,miny,maxx,maxy)`，即对象的界限。

`object.length` 返回对象的长度。

`object.geom_type` 返回一个字符串，指定对象的几何类型，按照OpenGIS的格式进行。

```>>> from shapely.geometry import Point
>>> Point(0,0).geom_type
```
```'Point'
```

`object.distance(other)` 返回到其他类型几何对象之间的浮点型最短距离。

```>>> Point(0,0).distance(Point(1,1))
```
```1.4142135623730951
```

`object.representative_point()` 返回一个在几何对象内的易得到的计算点。

```>>> donut = Point(0, 0).buffer(2.0).difference(Point(0, 0).buffer(1.0))
>>> donut.centroid.wkt
```
```'POINT (0 0)'
```
```>>> donut.representative_point().wkt
```
```'POINT (1.4975923633360986 0.0490085701647802)'
```

## 7.2.2. 点¶

`Point` 的构造函数采用坐标位置值或点元组参数。

```>>> from shapely.geometry import Point
>>> point = Point(0.0, 0.0)
>>> point.area
```
```0.0
```
```>>> point.length
```
```0.0
```
```>>> point.bounds
```
```(0.0, 0.0, 0.0, 0.0)
```

```>>> list(point.coords)
```
```[(0.0, 0.0)]
```
```>>> point.x
```
```0.0
```
```>>> point.y
```
```0.0
```

```>>> Point(point)
```

## 7.2.3. 线¶

```class LineString( coordinates)
```

`LineString`的构造采用线性序列，一般采用2个或者多个`(x,y[,z])`点元组。

```>>> from shapely.geometry import LineString
>>> line = LineString([(0, 0), (1, 1)])
>>> print(line.area)
```
```0.0
```
```>>> print(line.length)
```
```1.4142135623730951
```
```>>> print(line.bounds)
```
```(0.0, 0.0, 1.0, 1.0)
```
```>>> len(line.coords)
```
```2
```
```>>> list(line.coords)
```
```[(0.0, 0.0), (1.0, 1.0)]
```
```>>> LineString(line)
```

`Point`的实例序列不是一个有效的构造函数的参数。线由点来描述， 但不是由点的实例组成。

## 7.2.4. 面¶

```class Polygon(exterior[,interiors=None])
```

```>>> from shapely.geometry import Polygon
>>>
>>> ext = [(0, 0), (0, 2), (2, 2), (2, 0), (0, 0)]
>>> int = [(1, 0), (0.5, 0.5), (1, 1), (1.5, 0.5), (1, 0)][::-1]
>>> polygon = Polygon(ext, [int])
```
```>>> polygon
```
```>>> polygon.is_valid
```
```True
```
```>>> ext = [(0, 0), (0, 2), (2, 2), (2, 0), (0, 0)]
>>> int = [(1, 0), (0, 1), (0.5, 1.5), (1.5, 0.5), (1, 0)][::-1]
```
```>>> from shapely.geometry import Polygon
>>> polygon = Polygon(ext, [int])
```
```>>> polygon
```
```>>> polygon.is_valid
```
```False
```
```>>>
>>> ext = [(0, 0), (0, 2), (2, 2), (2, 0), (0, 0)]
>>> int = [(0.5, 0), (1.5, 0), (1.5, 1), (0.5, 1), (0.5, 0)]
>>> polygon = Polygon(ext, [int])
```
```>>> polygon
```
```>>> polygon.is_valid
```
```False
```

```>>> from shapely.geometry import  Polygon
>>> from shapely.geometry import LinearRing
>>> polygon = Polygon([(0, 0), (1, 1), (1, 0)])
```
```>>> polygon
```
```>>> polygon.area
>>> polygon.length
>>> polygon.bounds
>>> list(polygon.exterior.coords)
>>> list(polygon.interiors)
>>> coords = [(0, 0), (1, 1), (1, 0)]
>>> r = LinearRing(coords)
```
```>>> r
```
```>>> s = Polygon(r)
>>> s.area
```
```0.5
```
```>>> s
```
```>>> t = Polygon(s.buffer(1.0).exterior, [r])
>>> t.area
```
```6.5507620529190325
```
```>>> t
```

```shapely.geometry.box(minx,miny,maxx,maxy,ccw=True)
```

```>>> from shapely.geometry import Polygon, box
>>> b = box(0.0, 0.0, 1.0, 1.0)
>>> list(b.exterior.coords)
```
```[(1.0, 0.0), (1.0, 1.0), (0.0, 1.0), (0.0, 0.0), (1.0, 0.0)]
```

```shapely.geometry.polygon.orient(polygon,sign=1.0)
```

## 7.2.6. 线环(LinearRings)¶

`LinearRing` 的构造需要一个有序序列 `(x, y[, z])` 的点元组。

Shapely不会阻止这种环的创建，但是当他们运行的时候可能会产生异常。

## 7.2.7. 集合(Collections)¶

Shapely的一些操作可能会出现多相几何对象的集合导致影响一些Shapely操作。

```>>> from shapely.geometry import LineString
>>> a = LineString([(0, 0), (1,1), (1,2), (2,2)])
>>> b = LineString([(0, 0), (1,1), (2,1), (2,2)])
>>> x = a.intersection(b)
>>> from pprint import pprint
>>> len(x)
```
```/tmp/ipykernel_65127/1232873787.py:6: ShapelyDeprecationWarning: __len__ for multi-part geometries is deprecated and will be removed in Shapely 2.0. Check the length of the geoms property instead to get the  number of parts of a multi-part geometry.
len(x)```
```2
```
```>>> type(x)
```
```shapely.geometry.collection.GeometryCollection
```

a)是指一条绿色和黄色的线沿着一条线和一个点相交；

b)是指交点（蓝色）是一个包含一条线和一个点的集合。

```>>> from pprint import pprint
>>> pprint(list(x))
>>> from shapely.geometry import MultiPoint
>>> m = MultiPoint([(0, 0), (1, 1), (1,2), (2,2)])
>>> m[:1].wkt
>>> m[3:].wkt
>>> m[4:].wkt
```
```[<shapely.geometry.linestring.LineString object at 0x7f1c28166890>,
<shapely.geometry.point.Point object at 0x7f1c28154f50>]
```
```/tmp/ipykernel_65127/3428894420.py:2: ShapelyDeprecationWarning: Iteration over multi-part geometries is deprecated and will be removed in Shapely 2.0. Use the geoms property to access the constituent parts of a multi-part geometry.
pprint(list(x))
/tmp/ipykernel_65127/3428894420.py:2: ShapelyDeprecationWarning: __len__ for multi-part geometries is deprecated and will be removed in Shapely 2.0. Check the length of the geoms property instead to get the  number of parts of a multi-part geometry.
pprint(list(x))
/tmp/ipykernel_65127/3428894420.py:5: ShapelyDeprecationWarning: __getitem__ for multi-part geometries is deprecated and will be removed in Shapely 2.0. Use the geoms property to access the constituent parts of a multi-part geometry.
m[:1].wkt
/tmp/ipykernel_65127/3428894420.py:6: ShapelyDeprecationWarning: __getitem__ for multi-part geometries is deprecated and will be removed in Shapely 2.0. Use the geoms property to access the constituent parts of a multi-part geometry.
m[3:].wkt
/tmp/ipykernel_65127/3428894420.py:7: ShapelyDeprecationWarning: __getitem__ for multi-part geometries is deprecated and will be removed in Shapely 2.0. Use the geoms property to access the constituent parts of a multi-part geometry.
m[4:].wkt```
```'GEOMETRYCOLLECTION EMPTY'
```

### Collections of Points¶

```class MultiPoint( points)
```

```>>> from shapely.geometry import MultiPoint
>>> points = MultiPoint([(0.0, 0.0), (1.0, 1.0)])
>>> points.area
>>> points.length
>>> points.bounds
```
```(0.0, 0.0, 1.0, 1.0)
```

```>>> import pprint
>>> pprint.pprint(list(points.geoms))
>>> pprint.pprint(list(points))
```
```[<shapely.geometry.point.Point object at 0x7f1c11d4f990>,
<shapely.geometry.point.Point object at 0x7f1c2cc00ed0>]
[<shapely.geometry.point.Point object at 0x7f1c11df7ed0>,
<shapely.geometry.point.Point object at 0x7f1c11dda1d0>]
```
```/tmp/ipykernel_65127/253965190.py:3: ShapelyDeprecationWarning: Iteration over multi-part geometries is deprecated and will be removed in Shapely 2.0. Use the geoms property to access the constituent parts of a multi-part geometry.
pprint.pprint(list(points))
/tmp/ipykernel_65127/253965190.py:3: ShapelyDeprecationWarning: __len__ for multi-part geometries is deprecated and will be removed in Shapely 2.0. Check the length of the geoms property instead to get the  number of parts of a multi-part geometry.
pprint.pprint(list(points))```

```MultiPoint([Point(0, 0), Point(1, 1)])
```

### Collections of Lines（多元线）¶

```class MultiLineString( lines)
```

```>>> from shapely.geometry import MultiLineString
>>> coords = [((0, 0), (1, 1)), ((-1, 0), (1, 0))]
>>> lines = MultiLineString(coords)
>>> lines.area
>>> lines.length
>>> lines.bounds
>>> len(lines.geoms)
```
```2
```

```>>> pprint.pprint(list(lines.geoms))
>>> pprint.pprint(list(lines))
```
```[<shapely.geometry.linestring.LineString object at 0x7f1c11dba990>,
<shapely.geometry.linestring.LineString object at 0x7f1c11de8090>]
[<shapely.geometry.linestring.LineString object at 0x7f1c11df7e50>,
<shapely.geometry.linestring.LineString object at 0x7f1c11debb50>]
```
```/tmp/ipykernel_65127/654406357.py:2: ShapelyDeprecationWarning: Iteration over multi-part geometries is deprecated and will be removed in Shapely 2.0. Use the geoms property to access the constituent parts of a multi-part geometry.
pprint.pprint(list(lines))
/tmp/ipykernel_65127/654406357.py:2: ShapelyDeprecationWarning: __len__ for multi-part geometries is deprecated and will be removed in Shapely 2.0. Check the length of the geoms property instead to get the  number of parts of a multi-part geometry.
pprint.pprint(list(lines))```

```>>> MultiLineString(lines)
>>> MultiLineString(lines.geoms)
```

### Collection of Polygons（多元多边形）¶

```class MultiPolygon( polygons)
```

```>>> polygon = [(0, 0), (1,1), (1,2), (2,2),(0,0)]
>>> s = [(10, 0), (21,1), (31,2), (24,2),(10,0)]
>>> t = [(0, 50), (1,21), (1,22), (32,2),(0,50)]
>>> from shapely.geometry import Polygon
>>> p_a, s_a, t_a = [Polygon(x) for x in  [polygon, s, t]]
>>> from shapely.geometry import MultiPolygon
>>> polygons = MultiPolygon([p_a, s_a, t_a])
>>> len(polygons.geoms)
>>>
>>> len(polygons)
>>>
>>> polygons.bounds
```
```/tmp/ipykernel_65127/223419790.py:10: ShapelyDeprecationWarning: __len__ for multi-part geometries is deprecated and will be removed in Shapely 2.0. Check the length of the geoms property instead to get the  number of parts of a multi-part geometry.
len(polygons)```
```(0.0, 0.0, 32.0, 50.0)
```
```>>> p_a
```
```>>> s_a
```
```>>> t_a
```
```>>> polygons
```
```>>> uu = polygons.geoms
```
```>>> uu[0]
```

## 7.2.8. 其他问题¶

### 空要素¶

```>>> from shapely.geometry import LineString
>>> line = LineString()
>>> line.is_empty
>>> line.length
>>> line.bounds
>>> line.coords
```
```<shapely.coords.CoordinateSequence at 0x7f1c11db3a50>
```

```>>> line.coords = [(0, 0), (1, 1)]
>>> line.is_empty
>>> line.length
>>> line.bounds
```
```(0.0, 0.0, 1.0, 1.0)
```