In [1]:
from shapely.geometry import LineString, MultiLineString, Point
pprint=LineString
coords = [((0, 0), (1, 1)), ((-1, 0), (1, 0))]
lines = MultiLineString(coords)
lines.boundary
pprint(list(lines.boundary))
lines.boundary.boundary
lines.boundary.boundary.is_empty
Out[1]:
object.centroid
返回几何对象的质心的代表。
In [2]:
LineString([(0, 0), (1, 1)]).centroid
LineString([(0, 0), (1, 1)]).centroid.wkt
Out[2]:
In [3]:
a = Point(1, 1).buffer(1.5)
b = Point(2, 1).buffer(1.5)
a.difference(b)
Out[3]:
In [4]:
a = Point(1, 1).buffer(1.5)
b = Point(2, 1).buffer(1.5)
a.intersection(b)
Out[4]:
object.symmetric\_difference(other)
返回一个在这个对象不在另一个几何对象的点的代表,
并且其它对象的点不在这个几何对象中。
In [5]:
a = Point(1, 1).buffer(1.5)
b = Point(2, 1).buffer(1.5)
a.symmetric_difference(b)
Out[5]:
In [6]:
a = Point(1, 1).buffer(1.5)
b = Point(2, 1).buffer(1.5)
a.union(b)
Out[6]:
这些操作的语义随着几何对象类型的不同而变化。例如, 比较合并之后多边形的边界,与多边形边界的合并结果。
In [7]:
a.union(b).boundary
Out[7]:
In [8]:
a.boundary.union(b.boundary)
Out[8]:
注意:
union()
是一个找到许多对象的累积集合的昂贵的方式。
shapely.ops.cascaded_union()
是一个更有效的方法。
In [9]:
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)
In [10]:
dilated
Out[10]:
In [11]:
eroded
Out[11]:
图6.2:一条线的扩张和一个多边形的收缩。 新对象用蓝线表示。
一个点的默认缓冲区是与99.8%的圆盘面积的多边形补丁。
In [12]:
from shapely.geometry import Point
p = Point(0, 0).buffer(10.0)
len(p.exterior.coords)
Out[12]:
In [13]:
p.area
Out[13]:
分辨率为1,缓冲区是一个矩形补丁。例如:
In [14]:
q = Point(0, 0).buffer(10.0, 1)
len(q.exterior.coords)
Out[14]:
In [15]:
q
Out[15]:
In [16]:
q.area
Out[16]:
通过设定距离为0, buffer() 可清除自接触或者自交多边形,例如经典的“领结”。
In [17]:
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
Out[17]:
In [18]:
bowtie
Out[18]:
In [19]:
clean = bowtie.buffer(0)
clean.is_valid
Out[19]:
In [20]:
clean
Out[20]:
In [21]:
len(clean)
Out[21]:
In [22]:
list(clean[0].exterior.coords)
Out[22]:
In [23]:
clean[0].exterior
Out[23]:
In [24]:
list(clean[0].exterior.coords)
Out[24]:
In [25]:
clean[0].exterior.coords
Out[25]:
In [26]:
list(clean[1].exterior.coords)
Out[26]:
In [27]:
Point(0, 0).convex_hull
Out[27]:
In [28]:
from shapely.geometry import MultiPoint
MultiPoint([(0, 0), (1, 1)]).convex_hull
Out[28]:
In [29]:
from shapely.geometry import MultiPoint
MultiPoint([(0, 0), (1, 1), (1, -1)]).convex_hull
Out[29]:
In [30]:
from shapely.geometry import Point
Point(0, 0).envelope
Out[30]:
In [31]:
from shapely.geometry import MultiPoint
MultiPoint([(0, 0), (1, 1)]).envelope
Out[31]:
In [32]:
p = Point(0.0, 0.0)
x = p.buffer(1.0)
x.area
Out[32]:
In [33]:
len(x.exterior.coords)
Out[33]:
In [34]:
s = x.simplify(0.05, preserve_topology=False)
s.area
Out[34]:
In [35]:
len(s.exterior.coords)
Out[35]:
In [36]:
import helper; helper.info()