【问题标题】:How to draw polygons in python (giving cordenates) and number its edges?如何在 python 中绘制多边形(给出坐标)并为其边缘编号?
【发布时间】:2023-02-04 06:20:10
【问题描述】:

我有一个 python 中的农林业地块(多边形)列表。每个多边形都表示为纬度/经度。

 polygon1_coords = [
    (11.946898956, -86.109286248),
    (11.947196373, -86.109159886),
    (11.947092456, -86.108962101),
    (11.946897164, -86.10886504),
    (11.946898956, -86.109286248 )]

polygon2_coords = [
    (11.946895372, -86.110055411),
    (11.94718204,  -86.110046254),
    (11.947194581, -86.109172705),
    (11.946900747, -86.109313718),
    (11.946895372, -86.110055411)
    ]

我需要一种方法将这些多边形绘制为图像并在其边缘放置数字。多边形也需要用字母表示。这是我需要的例子

如您所见,我需要能够在同一张图片中表示多个多边形,并分别对其边进行编号。我怎样才能在 python 中做到这一点?

【问题讨论】:

  • 也许学习matplotlib包?

标签: python python-imaging-library gis


【解决方案1】:

Python 中有几个库允许您绘制多边形形状并对其进行注释。下面是一个使用 Matplotlib 库的示例:

import matplotlib.pyplot as plt

polygon = [
    (12.132481, -86.239511),
    (12.132093,-86.239344 ),
    (12.132287, -86.23876),
    (12.132481, -86.239511)
]

x = [p[0] for p in polygon]
y = [p[1] for p in polygon]

fig, ax = plt.subplots()
ax.plot(x, y)

for i, p in enumerate(polygon):
    ax.annotate(f"{i+1}", (p[0], p[1]))

plt.show()

此代码将多边形显示为一系列连接的线段,并用边号注释每个顶点。要用字母表示多边形,只需在 ax.annotate 函数中将 f"{i+1}" 替换为所需的字母即可。

【讨论】:

  • 如何在同一图像中表示多个多边形并分别对其边缘进行编号。我更新了问题
【解决方案2】:

借用之前的答案,但不仅在边缘添加标签,还在多边形内部添加标签,您可以使用shapely's representative_point 为您的标签找到一个位置,请参阅:

import matplotlib.pyplot as plt
from shapely.geometry import Polygon

polygon = [
    (12.132481, -86.239511),
    (12.132093,-86.239344 ),
    (12.132287, -86.23876),
    (12.132481, -86.239511)
]

x = [p[0] for p in polygon]
y = [p[1] for p in polygon]

label_location = Polygon(polygon).representative_point()

fig, ax = plt.subplots()
ax.plot(x, y)

#[:-1] slice because we only want to print 3 edges, not where they meet
for i, p in enumerate(polygon[:-1]):
    ax.annotate(f"{i+1}", (p[0], p[1]))

ax.annotate("B", label_location.coords[:][0])

plt.show()

情节:

【讨论】:

  • 如何在同一图像中表示多个多边形并分别对其边缘进行编号?我更新了问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多