具有Matplotlib楔形的Shapely多边形的交集

扎克·乔伯特

在这种情况下,我将绘制matplotlib.patches.Wedge对象以及缓冲shapely.geometry.LineString对象。我需要计算这两个对象的重叠区域。但是,Wedgematplotlib.wedges对象,不能与Shapely的.intersection()方法一起使用

我怎样才能做到这一点?这是一些代码:

from shapely.geometry import LineString
from matplotlib.patches import Wedge
from matplotlib import pyplot as plt
from descartes.patch import PolygonPatch

width = 5
radius = 1
rich = 1

circle_patch = Wedge((0, 0), radius+3,
                     0, 360, 3)

fig, ax = plt.subplots()

ax.add_patch(circle_patch)

ax.plot(0, 0, 'xr')
plt.autoscale()

coords = [
    [0, 0],
    [0, 1],
    [0, 2],
    [1, 2],
    [2, 2]
]

stick = LineString(coords)

stick_patch = PolygonPatch(stick.buffer(0.5))

ax.add_patch(stick_patch)

x, y = stick.xy
ax.plot(x, y, 'r-', zorder=1)

plt.show()

area = stick.buffer(0.5).intersection(circle_patch).area

在此处输入图片说明

PS必须是环形,而不是圆形

乔治

最简单的解决方案是不使用Matplotlib补丁,而首先使用Shapely构造楔形多边形:

import matplotlib.pyplot as plt
from descartes.patch import PolygonPatch
from shapely.geometry import LineString, Point

outer_circle = Point(0, 0).buffer(4)
inner_circle = Point(0, 0).buffer(1)
wedge = outer_circle.difference(inner_circle)

stick = LineString([(0, 0), (0, 2), (2, 2)])
buffered_stick = stick.buffer(0.5)

intersection = buffered_stick.intersection(wedge)

wedge_patch = PolygonPatch(wedge)
stick_patch = PolygonPatch(buffered_stick, alpha=0.5, hatch='/')
intersection_patch = PolygonPatch(intersection, alpha=0.5, hatch='.')

fig, ax = plt.subplots()
ax.add_patch(wedge_patch)
ax.add_patch(stick_patch)
ax.add_patch(intersection_patch)
plt.autoscale()

在此处输入图片说明


If, for some reason, this is not possible, and you have to work with the Matplotlib's Wedge, then I can think of two ways to get its intersection area with Shapely's polygon. In both of them, I convert the patches to Shapely polygons first. You probably can't get intersection area using only Matplotlib.

1) Using .get_path() method on the Matplotlib's patch from which you can extract vertices as a NumPy array and convert it to a Shapely polygon using asPolygon:

import matplotlib.pyplot as plt
from descartes.patch import PolygonPatch
from matplotlib.patches import Wedge
from shapely.geometry import asPolygon, LineString

wedge_patch = Wedge(center=(0, 0), 
                    r=4,
                    theta1=0, 
                    theta2=360, 
                    width=3)
stick = LineString([(0, 0), (0, 2), (2, 2)])
buffered_stick = stick.buffer(0.5)

wedge_path = wedge_patch.get_path()
wedge_polygon = asPolygon(wedge_path.vertices).buffer(0)
intersection = buffered_stick.intersection(wedge_polygon)

stick_patch = PolygonPatch(buffered_stick, alpha=0.5, hatch='/')
intersection_patch = PolygonPatch(intersection, alpha=0.5, hatch='.')

fig, ax = plt.subplots()
ax.add_patch(wedge_patch)
ax.add_patch(stick_patch)
ax.add_patch(intersection_patch)
plt.autoscale()

Note the buffer(0) which I apply to the wedge polygon. This is a common trick in Shapely to make a valid polygon out of an invalid. In your answer you do something similar when removing zeros from ring_coords.

2)通过访问Wedge属性centerrwidth,并使用它们来重新创建多边形:

import matplotlib.pyplot as plt
from descartes.patch import PolygonPatch
from matplotlib.patches import Wedge
from shapely.geometry import LineString, Point

wedge_patch = Wedge(center=(0, 0), 
                    r=4,
                    theta1=0, 
                    theta2=360, 
                    width=3)
stick = LineString([(0, 0), (0, 2), (2, 2)])
buffered_stick = stick.buffer(0.5)

outer_circle = Point(wedge_patch.center).buffer(wedge_patch.r)
inner_circle = Point(wedge_patch.center).buffer(wedge_patch.r - wedge_patch.width)
wedge_polygon = outer_circle.difference(inner_circle)
intersection = buffered_stick.intersection(wedge_polygon)

stick_patch = PolygonPatch(buffered_stick, alpha=0.5, hatch='/')
intersection_patch = PolygonPatch(intersection, alpha=0.5, hatch='.')

fig, ax = plt.subplots()
ax.add_patch(wedge_patch)
ax.add_patch(stick_patch)
ax.add_patch(intersection_patch)
plt.autoscale()

所有解决方案均提供相同的视觉输出。

并且所有方法都给出大致相同的区域:

>>> intersection.area
3.3774012986988513  # 1st case
3.3823210603713694  # 2nd case and the original without Matplotlib

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章