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)))
In [5]:
from shapely.geometry import Point
from shapely.ops import cascaded_union
polygons = [Point(i, 0).buffer(0.7) for i in range(5)]
In [6]:
polygons[0].union(polygons[1]).area
Out[6]:
In [7]:
polygons[0].area
Out[7]:
In [8]:
cascaded_union(polygons)
Out[8]:
这个功能在解决多元多边形中特别有用。例如:
In [9]:
from shapely.geometry import MultiPolygon
m = MultiPolygon(polygons)
m.area
Out[9]:
In [10]:
cascaded_union(m).area
Out[10]:
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]:
In [15]:
hits = filter(prepared_polygon.contains, points)
hits
Out[15]:
准备几何实例有以下几种方法: 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]:
In [17]:
import helper; helper.info()