计算椭圆之间相交的面积 - python
calculate area intersected between ellipses - python
我的目标是计算两个椭圆之间相交的面积。下面计算两个固定坐标之间的面积,但我希望使用一个椭圆的移动坐标迭代此过程,同时保持第二个椭圆固定。
使用下面的方法,ellipse1
将保持不变,而 ellipse2
将使用 df
中 X
和 Y
的坐标。
预期的输出将有望 return 每个时间点的区域四舍五入到小数点后 2 位作为单独的列标记为 'Area'
。
df = pd.DataFrame({
'Time' : [1,2,3,4,5],
'X' : [3.0,4.5,5.0,10.0,3.0],
'Y' : [1.0,0.0,-1.0,-2.0,-3.0],
})
from matplotlib import pyplot as plt
from shapely.geometry.point import Point
from shapely import affinity
from matplotlib.patches import Polygon
import numpy as np
def create_ellipse(center, lengths, angle = 0):
"""
create a shapely ellipse. adapted from
https://gis.stackexchange.com/a/243462
"""
circ = Point(center).buffer(1)
ell = affinity.scale(circ, int(lengths[0]), int(lengths[1]))
ellr = affinity.rotate(ell, angle)
return ellr
fig,ax = plt.subplots(figsize = (6,6))
ax.set_xlim([-20,20])
ax.set_ylim([-20,20])
ellipse1 = create_ellipse((0,0),(5,10),0)
verts1 = np.array(ellipse1.exterior.coords.xy)
patch1 = Polygon(verts1.T,color = 'red', alpha = 0.5)
ax.add_patch(patch1)
ellipse2 = create_ellipse((3.0,1.0),(5,5),0)
verts2 = np.array(ellipse2.exterior.coords.xy)
patch2 = Polygon(verts2.T,color = 'blue', alpha = 0.5)
ax.add_patch(patch2)
##compute intersect
intersect = ellipse1.intersection(ellipse2)
verts3 = np.array(intersect.exterior.coords.xy)
patch3 = Polygon(verts3.T, facecolor = 'none', edgecolor = 'black')
ax.add_patch(patch3)
##compute intersected area
print(intersect.area)
预期输出:
Time X Y Area
0 1 3.0 1.0 56.51
1 2 4.5 0.0 46.99
2 3 5.0 -1.0 36.75
3 4 10.0 -2.0 0.00
4 5 3.0 -3.0 54.03
如果我能清楚地理解您的意思,您就快成功了;您所需要的只是在数据框中使用一种循环。即使由于 apply 这可能不是很有效(更好的方法可能是跳到 geopandas,但我认为这里的方法对你来说更容易理解),这也可以解决问题:
ellipse1 = create_ellipse((0,0),(5,10),0)
df['ellipse2'] = df[['X', 'Y']].apply(
lambda xy:create_ellipse([*xy],(5,5),0),
axis=1
)
df['Area'] = df['ellipse2'].apply(lambda x: ellipse1.intersection(x).area)
df.drop('ellipse2', axis=1, inplace=True)
print(df)
Returns
Time X Y Area
0 1 3.0 1.0 56.518719
1 2 4.5 0.0 41.997447
2 3 5.0 -1.0 36.746078
3 4 10.0 -2.0 0.000000
4 5 3.0 -3.0 54.029912
我的目标是计算两个椭圆之间相交的面积。下面计算两个固定坐标之间的面积,但我希望使用一个椭圆的移动坐标迭代此过程,同时保持第二个椭圆固定。
使用下面的方法,ellipse1
将保持不变,而 ellipse2
将使用 df
中 X
和 Y
的坐标。
预期的输出将有望 return 每个时间点的区域四舍五入到小数点后 2 位作为单独的列标记为 'Area'
。
df = pd.DataFrame({
'Time' : [1,2,3,4,5],
'X' : [3.0,4.5,5.0,10.0,3.0],
'Y' : [1.0,0.0,-1.0,-2.0,-3.0],
})
from matplotlib import pyplot as plt
from shapely.geometry.point import Point
from shapely import affinity
from matplotlib.patches import Polygon
import numpy as np
def create_ellipse(center, lengths, angle = 0):
"""
create a shapely ellipse. adapted from
https://gis.stackexchange.com/a/243462
"""
circ = Point(center).buffer(1)
ell = affinity.scale(circ, int(lengths[0]), int(lengths[1]))
ellr = affinity.rotate(ell, angle)
return ellr
fig,ax = plt.subplots(figsize = (6,6))
ax.set_xlim([-20,20])
ax.set_ylim([-20,20])
ellipse1 = create_ellipse((0,0),(5,10),0)
verts1 = np.array(ellipse1.exterior.coords.xy)
patch1 = Polygon(verts1.T,color = 'red', alpha = 0.5)
ax.add_patch(patch1)
ellipse2 = create_ellipse((3.0,1.0),(5,5),0)
verts2 = np.array(ellipse2.exterior.coords.xy)
patch2 = Polygon(verts2.T,color = 'blue', alpha = 0.5)
ax.add_patch(patch2)
##compute intersect
intersect = ellipse1.intersection(ellipse2)
verts3 = np.array(intersect.exterior.coords.xy)
patch3 = Polygon(verts3.T, facecolor = 'none', edgecolor = 'black')
ax.add_patch(patch3)
##compute intersected area
print(intersect.area)
预期输出:
Time X Y Area
0 1 3.0 1.0 56.51
1 2 4.5 0.0 46.99
2 3 5.0 -1.0 36.75
3 4 10.0 -2.0 0.00
4 5 3.0 -3.0 54.03
如果我能清楚地理解您的意思,您就快成功了;您所需要的只是在数据框中使用一种循环。即使由于 apply 这可能不是很有效(更好的方法可能是跳到 geopandas,但我认为这里的方法对你来说更容易理解),这也可以解决问题:
ellipse1 = create_ellipse((0,0),(5,10),0)
df['ellipse2'] = df[['X', 'Y']].apply(
lambda xy:create_ellipse([*xy],(5,5),0),
axis=1
)
df['Area'] = df['ellipse2'].apply(lambda x: ellipse1.intersection(x).area)
df.drop('ellipse2', axis=1, inplace=True)
print(df)
Returns
Time X Y Area
0 1 3.0 1.0 56.518719
1 2 4.5 0.0 41.997447
2 3 5.0 -1.0 36.746078
3 4 10.0 -2.0 0.000000
4 5 3.0 -3.0 54.029912