from shapely.geometry import LineString, MultiLineString, Point
coords = [((0, 0), (1, 1)), ((-1, 0), (1, 0))]
lines = MultiLineString(coords)
lines.boundary
获取多元要素的每个部分,在Shapely 2.0以前使用 lines.boundary
,在版本2.0后,使用下面的方法:
len(lines.geoms)
2
for geom in lines.geoms:
print(geom)
LINESTRING (0 0, 1 1) LINESTRING (-1 0, 1 0)
lines.boundary.boundary
lines.boundary.boundary.is_empty
True
object.centroid
返回几何对象的质心的代表。
LineString([(0, 0), (1, 1)]).centroid
LineString([(0, 0), (1, 1)]).centroid.wkt
'POINT (0.5 0.5)'
注意:
一个对象的质心可能是它点中的一个,但是并不保证所有对象的质心都是这样。
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)
dilated
eroded
一个点的默认缓冲区是与99.8%的圆盘面积的多边形补丁。
from shapely.geometry import Point
p = Point(0, 0).buffer(10.0)
len(p.exterior.coords)
65
p.area
313.6548490545941
分辨率为1,缓冲区是一个矩形补丁。例如:
q = Point(0, 0).buffer(10.0, 1)
len(q.exterior.coords)
5
q
q.area
200.0
通过设定距离为0,
buffer
可清除自接触或者自交多边形,例如经典的“领结”。
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
False
bowtie
clean = bowtie.buffer(0)
clean.is_valid
True
clean
len(clean.geoms)
2
list(clean.geoms[0].exterior.coords)
[(0.0, 0.0), (0.0, 2.0), (1.0, 1.0), (0.0, 0.0)]
clean.geoms[0].exterior
list(clean.geoms[0].exterior.coords)
[(0.0, 0.0), (0.0, 2.0), (1.0, 1.0), (0.0, 0.0)]
clean.geoms[0].exterior.coords
<shapely.coords.CoordinateSequence at 0x7f7a742ff860>
list(clean.geoms[0].exterior.coords)
[(0.0, 0.0), (0.0, 2.0), (1.0, 1.0), (0.0, 0.0)]
Point(0, 0).convex_hull
from shapely.geometry import MultiPoint
MultiPoint([(0, 0), (1, 1)]).convex_hull
from shapely.geometry import MultiPoint
MultiPoint([(0, 0), (1, 1), (1, -1)]).convex_hull
from shapely.geometry import Point
Point(0, 0).envelope
from shapely.geometry import MultiPoint
MultiPoint([(0, 0), (1, 1)]).envelope
p = Point(0.0, 0.0)
x = p.buffer(1.0)
x.area
3.136548490545939
len(x.exterior.coords)
65
s = x.simplify(0.05, preserve_topology=False)
s.area
3.061467458920719
len(s.exterior.coords)
17
a = Point(1, 1).buffer(1.5)
b = Point(2, 1).buffer(1.5)
a.difference(b)
a = Point(1, 1).buffer(1.5)
b = Point(2, 1).buffer(1.5)
a.intersection(b)
object.symmetric_difference(other)
返回一个在这个对象不在另一个几何对象的点的代表,
并且其它对象的点不在这个几何对象中。
a = Point(1, 1).buffer(1.5)
b = Point(2, 1).buffer(1.5)
a.symmetric_difference(b)
a = Point(1, 1).buffer(1.5)
b = Point(2, 1).buffer(1.5)
a.union(b)
这些操作的语义随着几何对象类型的不同而变化。例如, 比较合并之后多边形的边界,与多边形边界的合并结果。
a.union(b).boundary
a.boundary.union(b.boundary)
注意:
union()
是一个找到许多对象的累积集合的昂贵的方式。
shapely.ops.cascaded_union()
是一个更有效的方法。