我如何计算多角形(而不是多边形)的多角形的质心

发布于 2025-02-05 23:40:36 字数 246 浏览 3 评论 0原文

我知道可以根据多边形来计算多边形的质心

from shapely.geometry import Polygon
coordinate_list = [[1,2], [2,3], [5,5]]
output = Polygon(coordinate_list).centroid

。 0],[0,1],[1,0]]]

有办法做到这一点。 Shapely似乎具有一个多角类,但与多边形类别的运行不相同。

I understand that the centroid of a polygon may be calculated from

from shapely.geometry import Polygon
coordinate_list = [[1,2], [2,3], [5,5]]
output = Polygon(coordinate_list).centroid

However, my coordinate_list is a multiple polygons, e.g. my coordinate_list = [[[1,2], [2,3], [5,5]], [[0,0], [0,1], [1,0]]]

Is there way to do this. Shapely appears to have a multipolygon class but it does not operate the same as the Polygon class.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉风有信 2025-02-12 23:40:36

您可以使用MultipolyGon()。centroid,只是您不能将coordion_list直接传递给多层构造函数:

/../采用一系列外环和孔清单元组/../
/../还接受多边形实例的无序序列/../
https://shapely.readthedocs.io.io/en/stable/手册

# Based on Multipolygon sample,
# https://shapely.readthedocs.io/en/stable/code/multipolygon.py

from matplotlib import pyplot
from shapely.geometry import Polygon, MultiPolygon
from descartes.patch import PolygonPatch

# from https://github.com/shapely/shapely/blob/main/docs/code/figures.py
from figures import BLUE, BLACK, SIZE, set_limits, plot_coords, color_isvalid

fig = pyplot.figure(1, figsize=SIZE, dpi=90)
ax = fig.add_subplot(121)
set_limits(ax, -1, 6, -1, 6)

coordinate_list = [[[1,2], [2,3], [5,5]], [[0,0], [0,1], [1,0]]]

# "constructor takes a sequence of exterior ring and hole list tuples" - 
# https://shapely.readthedocs.io/en/stable/manual.html#collections-of-polygons
multi = MultiPolygon([(coordinate_list[0], []), (coordinate_list[1], [])])

# "the constructor also accepts an unordered sequence of Polygon instances"
#multi = MultiPolygon([Polygon(coordinate_list[0]),Polygon(coordinate_list[1])])

plot_coords(ax, multi.centroid, color=BLACK)

for polygon in multi.geoms:
    plot_coords(ax, polygon.exterior)
    patch = PolygonPatch(polygon, facecolor=BLUE, edgecolor=BLUE, alpha=0.5, zorder=2)
    ax.add_patch(patch) 

“

You can use MultiPolygon().centroid, it's just that you can't pass that coordinate_list directly to MultiPolygon constructor as it:

/../ takes a sequence of exterior ring and hole list tuples /../
/../ also accepts an unordered sequence of Polygon instances /../
https://shapely.readthedocs.io/en/stable/manual.html#collections-of-polygons

# Based on Multipolygon sample,
# https://shapely.readthedocs.io/en/stable/code/multipolygon.py

from matplotlib import pyplot
from shapely.geometry import Polygon, MultiPolygon
from descartes.patch import PolygonPatch

# from https://github.com/shapely/shapely/blob/main/docs/code/figures.py
from figures import BLUE, BLACK, SIZE, set_limits, plot_coords, color_isvalid

fig = pyplot.figure(1, figsize=SIZE, dpi=90)
ax = fig.add_subplot(121)
set_limits(ax, -1, 6, -1, 6)

coordinate_list = [[[1,2], [2,3], [5,5]], [[0,0], [0,1], [1,0]]]

# "constructor takes a sequence of exterior ring and hole list tuples" - 
# https://shapely.readthedocs.io/en/stable/manual.html#collections-of-polygons
multi = MultiPolygon([(coordinate_list[0], []), (coordinate_list[1], [])])

# "the constructor also accepts an unordered sequence of Polygon instances"
#multi = MultiPolygon([Polygon(coordinate_list[0]),Polygon(coordinate_list[1])])

plot_coords(ax, multi.centroid, color=BLACK)

for polygon in multi.geoms:
    plot_coords(ax, polygon.exterior)
    patch = PolygonPatch(polygon, facecolor=BLUE, edgecolor=BLUE, alpha=0.5, zorder=2)
    ax.add_patch(patch) 

Multipolygon with centroid

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文