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))
]
from shapely.ops import linemerge
linemerge(lines)
pprint(list(linemerge(lines)))
Pretty printing has been turned OFF
from shapely.geometry import Point
from shapely.ops import unary_union
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
unary_union(polygons)
这个功能在解决多元多边形中特别有用。例如:
from shapely.geometry import MultiPolygon
m = MultiPolygon(polygons)
m.area
7.684543801837549
unary_union(m).area
6.610301355116799
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 0x7fee7c2f0470>
hits = filter(prepared_polygon.contains, points)
hits
<filter object at 0x7fee5c241f60>
准备几何实例有以下几种方法: contains
, contains_properly
, covers
,
与 intersects
。在非准备几何对象中
作为副本使用。
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]'