In [2]:
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))
]
In [3]:
from shapely.ops import linemerge
linemerge(lines)
Out[3]:
In [4]:
pprint(list(linemerge(lines)))
Pretty printing has been turned OFF
In [5]:
from shapely.geometry import Point
from shapely.ops import unary_union
polygons = [Point(i, 0).buffer(0.7) for i in range(5)]
In [6]:
polygons[0].union(polygons[1]).area
Out[6]:
2.8052569090548314
In [7]:
polygons[0].area
Out[7]:
1.53690876036751
In [8]:
unary_union(polygons)
Out[8]:
这个功能在解决多元多边形中特别有用。例如:
In [9]:
from shapely.geometry import MultiPolygon
m = MultiPolygon(polygons)
m.area
Out[9]:
7.684543801837549
In [10]:
unary_union(m).area
Out[10]:
6.610301355116799
In [11]:
from shapely.geometry import Point
In [12]:
from shapely.prepared import prep
In [13]:
points = [...] # large list
In [14]:
from shapely.geometry import Point
polygon = Point(0.0, 0.0).buffer(1.0)
prepared_polygon = prep(polygon)
prepared_polygon
Out[14]:
<shapely.prepared.PreparedGeometry object at 0x7fee7c2f0470>
In [15]:
hits = filter(prepared_polygon.contains, points)
hits
Out[15]:
<filter object at 0x7fee5c241f60>
准备几何实例有以下几种方法: contains, contains_properly, covers, 与 intersects 。在非准备几何对象中 作为副本使用。
In [16]:
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)
Out[16]:
'Ring Self-intersection[1 1]'