## 简单的点、线与多边形

### 点

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

In [1]:
```from shapely.geometry import Point
point = Point(0.0, 0.0)
point.area
```
Out[1]:
`0.0`
In [2]:
```point.length
```
Out[2]:
`0.0`
In [3]:
```point.bounds
```
Out[3]:
`(0.0, 0.0, 0.0, 0.0)`

In [4]:
```list(point.coords)
```
Out[4]:
`[(0.0, 0.0)]`
In [5]:
```point.x
```
Out[5]:
`0.0`
In [6]:
```point.y
```
Out[6]:
`0.0`

In [7]:
```Point(point)
```
Out[7]:

### 线

``````class LineString( coordinates)

``````

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

In [8]:
```from shapely.geometry import LineString
line = LineString([(0, 0), (1, 1)])
print(line.area)
```
```0.0
```
In [9]:
```print(line.length)
```
```1.4142135623730951
```
In [10]:
```print(line.bounds)
```
```(0.0, 0.0, 1.0, 1.0)
```
In [11]:
```len(line.coords)
```
Out[11]:
`2`
In [12]:
```list(line.coords)
```
Out[12]:
`[(0.0, 0.0), (1.0, 1.0)]`
In [13]:
```LineString(line)
```
Out[13]:

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

### 面

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

``````

In [14]:
```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])
```
In [15]:
```polygon
```
Out[15]:
In [16]:
```polygon.is_valid
```
Out[16]:
`True`
In [17]:
```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]
```
In [18]:
```from shapely.geometry import Polygon
polygon = Polygon(ext, [int])
```
In [19]:
```polygon
```
Out[19]:
In [20]:
```polygon.is_valid
```
Out[20]:
`False`
In [21]:
```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])
```
In [22]:
```polygon
```
Out[22]:
In [23]:
```polygon.is_valid
```
Out[23]:
`False`
In [ ]:
```
```

In [24]:
```from shapely.geometry import  Polygon
from shapely.geometry import LinearRing
polygon = Polygon([(0, 0), (1, 1), (1, 0)])
```
In [25]:
```polygon
```
Out[25]:
In [ ]:
```
```
In [26]:
```polygon.area
polygon.length
polygon.bounds
list(polygon.exterior.coords)
list(polygon.interiors)
coords = [(0, 0), (1, 1), (1, 0)]
r = LinearRing(coords)
```
In [27]:
```r
```
Out[27]:
In [ ]:
```
```
In [28]:
```s = Polygon(r)
s.area
```
Out[28]:
`0.5`
In [29]:
```s
```
Out[29]:
In [ ]:
```
```
In [30]:
```t = Polygon(s.buffer(1.0).exterior, [r])
t.area
```
Out[30]:
`6.5507620529190325`
In [31]:
```t
```
Out[31]:

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

``````

In [32]:
```from shapely.geometry import Polygon, box
b = box(0.0, 0.0, 1.0, 1.0)
list(b.exterior.coords)
```
Out[32]:
`[(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)

``````

In [ ]:
```
```

### 线环(LinearRings)

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

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

In [33]:
```from shapely.geometry import Polygon, LinearRing
# import LinearRing
ring = LinearRing([(0, 0), (1, 1), (1, 0)])
ring.area
ring.length
ring.bounds
len(ring.coords)
list(ring.coords)
LinearRing(ring)
```
Out[33]:

In [ ]:
```
```

### 集合(Collections)

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

In [34]:
```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)
```
Out[34]:
`2`
In [35]:
```type(x)
```
Out[35]:
`shapely.geometry.collection.GeometryCollection`

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

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

In [36]:
```# 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
```
Out[36]:
`'GEOMETRYCOLLECTION EMPTY'`

#### Collections of Points

``````class MultiPoint( points)

``````

In [37]:
```from shapely.geometry import MultiPoint
points = MultiPoint([(0.0, 0.0), (1.0, 1.0)])
points.area
points.length
points.bounds
```
Out[37]:
`(0.0, 0.0, 1.0, 1.0)`

In [38]:
```import pprint
pprint.pprint(list(points.geoms))
pprint.pprint(list(points))
```
```[<shapely.geometry.point.Point object at 0x7fd0c3858c18>,
<shapely.geometry.point.Point object at 0x7fd0c3858d68>]
[<shapely.geometry.point.Point object at 0x7fd0c3858470>,
<shapely.geometry.point.Point object at 0x7fd0c3858d68>]
```

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

``````

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

``````class MultiLineString( lines)

``````

In [39]:
```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)
```
Out[39]:
`2`

In [40]:
```pprint.pprint(list(lines.geoms))
pprint.pprint(list(lines))
```
```[<shapely.geometry.linestring.LineString object at 0x7fd0c3858d68>,
<shapely.geometry.linestring.LineString object at 0x7fd0c3858b00>]
[<shapely.geometry.linestring.LineString object at 0x7fd0c3857630>,
<shapely.geometry.linestring.LineString object at 0x7fd0c38575f8>]
```

In [41]:
```MultiLineString(lines)
MultiLineString(lines.geoms)
```
Out[41]:

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

``````class MultiPolygon( polygons)

``````

In [42]:
```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
```
Out[42]:
`(0.0, 0.0, 32.0, 50.0)`
In [43]:
```p_a
```
Out[43]:
In [44]:
```s_a
```
Out[44]:
In [45]:
```t_a
```
Out[45]:
In [46]:
```polygons
```
Out[46]:
In [47]:
```uu = polygons.geoms
```
In [48]:
```uu[0]
```
Out[48]:

In [ ]:
```
```

### 其他问题

#### 空要素

In [49]:
```from shapely.geometry import LineString
line = LineString()
line.is_empty
line.length
line.bounds
line.coords
# The coordinates of a empty feature can be set, 可以设置一个空值要素的坐标，
# after which the geometry is no longer empty.在这之后几何坐标不再是空的。
line.coords = [(0, 0), (1, 1)]
line.is_empty
line.length
line.bounds
```
Out[49]:
`(0.0, 0.0, 1.0, 1.0)`

### 一般的属性与方法

#### shapely一般的属性与方法

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

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

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

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

In [50]:
```from shapely.geometry import Point
Point(0,0).geom_type
```
Out[50]:
`'Point'`

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

In [51]:
```Point(0,0).distance(Point(1,1))
```
Out[51]:
`1.4142135623730951`

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

In [52]:
```donut = Point(0, 0).buffer(2.0).difference(Point(0, 0).buffer(1.0))
donut.centroid.wkt
```
Out[52]:
`'POINT (-2.26143213395106e-17 -3.945215842382012e-17)'`
In [53]:
```donut.representative_point().wkt
```
Out[53]:
`'POINT (-1.5 4.119267568565299e-15)'`

#### 对点状Shapefile进行缓冲操作

In [1]:
```import helper; helper.info()
```
```页面更新时间： 2019-02-28 18:42:52

Python: 3.5.3
```
In [ ]:
```
```