Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate the center of gravity with shapely in python?

Tags:

python

shapely

I discovered shapely but I did not find how to calculated the center of gravity of a polygon!

Does someone have the solution?

like image 297
Vianney Bailleux Avatar asked Nov 27 '18 14:11

Vianney Bailleux


1 Answers

If your polygon has a uniform density, its center of mass coincides with its centroid. In shapely, the centroid can be directly calculated as:

from shapely.geometry import Polygon

P = Polygon([[0, 0], [1, 0], [1, 1], [0, 1]])

print(P.centroid)
#POINT (0.5 0.5)
like image 116
ewcz Avatar answered Sep 29 '22 14:09

ewcz