交集返回一个MultilineString
,这是一个奇特的词,表示列表LineStrings
。我们可以从每个线串中检索坐标,例如:
import alphashape
from shapely.geometry import LineString
import matplotlib.pyplot as plt
import numpy as np
#replicating your example
fig, ax = plt.subplots()
line_xy = [[350, 100],[0, 120]]
points = np.asarray([(17, 158),(15, 135),(38, 183),(43, 19),(93, 88),(96, 140),(149, 163),(128, 248),(216, 265),(248, 210),(223, 167),(256, 151),(331, 214),(340, 187),(316, 53),(298, 35),(182, 0),(121, 42)])
alpha = 0.99 * alphashape.optimizealpha(points)
hull = alphashape.alphashape(points, alpha)
hull_pts = hull.exterior.coords.xy
p = LineString(line_xy)
ax.plot(*hull_pts, c="green")
ax.scatter(points[:,0], points[:,1], marker="o", color="red")
ax.scatter(*hull_pts, marker="s", color="red")
ax.plot(*p.coords.xy, color='blue')
#retrieving intersection
inters = hull.intersection(p)
#checking for object type to retrieve all intersection coordinates
if inters.type == "LineString":
coords = np.asarray([inters.coords.xy])
elif inters.type == "MultiLineString":
coords = np.asarray([l.coords.xy for l in inters.geoms])
#reshaping array point coordinates into a form that does not make my head hurt
coords = coords.transpose(1, 0, 2).reshape(2, -1)
print(coords)
plt.show()
with coords[:, i]
返回交点的 x-y 值i
.
示例输出:
[[324.67707894 234.24811338 176.4217078 18.88111888]
[101.44702406 106.61439352 109.91875955 118.92107892]]
奇怪的是,shapely
考虑像这样的线点300, 100
在船体内作为交叉点。严格来说,必须检查所有已识别的点是否位于船体多边形内。
alphashape(此处使用 1.3.1)应该更新他们的例程,因为alphashape.alphashape(points, alpha)
生成错误消息ShapelyDeprecationWarning: Iteration over multi-part geometries is deprecated and will be removed in Shapely 2.0. Use the
geoms property to access the constituent parts of a multi-part geometry.