Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: find a method to calculate the "inner centroid" (X,Y) of a polygon

I have a polygon (converted in a Shapely object). My goal is calculate the "inner centroid" (also known as "point on surface")(return x,y values) and the "centroid" (return x,y values) following the figure example:

enter image description here

from shapely.geometry import Polygon

ref_polygon = Polygon(points)
# get the x and y coordinate of the centroid
ref_polygon.centroid.wkt
'POINT (558768.9293489187300000 6361851.0362532493000000)'

my question is some programmer has already developed a function in Python to calculate the inner centroid or know some module to do this.

Thanks in advance

the points (vertex of the polygon) used are:

points = [(560036.4495758876, 6362071.890493258),
          (560036.4495758876, 6362070.890493258),
          (560036.9495758876, 6362070.890493258),
          (560036.9495758876, 6362070.390493258),
          (560037.4495758876, 6362070.390493258),
          (560037.4495758876, 6362064.890493258),
          (560036.4495758876, 6362064.890493258),
          (560036.4495758876, 6362063.390493258),
          (560035.4495758876, 6362063.390493258),
          (560035.4495758876, 6362062.390493258),
          (560034.9495758876, 6362062.390493258),
          (560034.9495758876, 6362061.390493258),
          (560032.9495758876, 6362061.390493258),
          (560032.9495758876, 6362061.890493258),
          (560030.4495758876, 6362061.890493258),
          (560030.4495758876, 6362061.390493258),
          (560029.9495758876, 6362061.390493258),
          (560029.9495758876, 6362060.390493258),
          (560029.4495758876, 6362060.390493258),
          (560029.4495758876, 6362059.890493258),
          (560028.9495758876, 6362059.890493258),
          (560028.9495758876, 6362059.390493258),
          (560028.4495758876, 6362059.390493258),
          (560028.4495758876, 6362058.890493258),
          (560027.4495758876, 6362058.890493258),
          (560027.4495758876, 6362058.390493258),
          (560026.9495758876, 6362058.390493258),
          (560026.9495758876, 6362057.890493258),
          (560025.4495758876, 6362057.890493258),
          (560025.4495758876, 6362057.390493258),
          (560023.4495758876, 6362057.390493258),
          (560023.4495758876, 6362060.390493258),
          (560023.9495758876, 6362060.390493258),
          (560023.9495758876, 6362061.890493258),
          (560024.4495758876, 6362061.890493258),
          (560024.4495758876, 6362063.390493258),
          (560024.9495758876, 6362063.390493258),
          (560024.9495758876, 6362064.390493258),
          (560025.4495758876, 6362064.390493258),
          (560025.4495758876, 6362065.390493258),
          (560025.9495758876, 6362065.390493258),
          (560025.9495758876, 6362065.890493258),
          (560026.4495758876, 6362065.890493258),
          (560026.4495758876, 6362066.890493258),
          (560026.9495758876, 6362066.890493258),
          (560026.9495758876, 6362068.390493258),
          (560027.4495758876, 6362068.390493258),
          (560027.4495758876, 6362068.890493258),
          (560027.9495758876, 6362068.890493258),
          (560027.9495758876, 6362069.390493258),
          (560028.4495758876, 6362069.390493258),
          (560028.4495758876, 6362069.890493258),
          (560033.4495758876, 6362069.890493258),
          (560033.4495758876, 6362070.390493258),
          (560033.9495758876, 6362070.390493258),
          (560033.9495758876, 6362070.890493258),
          (560034.4495758876, 6362070.890493258),
          (560034.4495758876, 6362071.390493258),
          (560034.9495758876, 6362071.390493258),
          (560034.9495758876, 6362071.890493258),
          (560036.4495758876, 6362071.890493258)]
like image 521
Gianni Spear Avatar asked Jan 15 '23 20:01

Gianni Spear


1 Answers

The term "inner centroid" isn't a well-defined term in computational geometry, but it seems clear from your post that you want to compute a point that is well inside the polygon (with some margin between it and nearby edges), and which is reasonably near to the true centroid.

Here are a couple of ideas you might try:

Algorithm A

  1. Generate all the internal diagonals of the polygon.

  2. For each internal diagonal, consider the midpoint, and give it a score based on how far it is from the nearest edge and how close it is to the centroid.

  3. Choose the midpoint with the highest score.

An internal diagonal of a polygon is a line joining two non-adjacent vertices that lies entirely with the polygon. The set of m internal diagonals of a polygon with n verticies can be generated in O(m + n log log n) using a rather complex algorithm due to Hershberger, or in O(n2) using more straightforward algorithms.

Algorithm B

  1. Triangulate the polygon.

  2. For each triangle in the triangulation, consider the centroid (or maybe the incenter?) of the triangle, and give it a score based on how far it is from the nearest edge and how close it is to the centroid of the polygon.

  3. Choose the triangle center with the highest score.

A simple polygon with n vertices can be triangulated in O(n) using an algorithm based on decomposition into monotone polygons due to Chazelle, or in O(n2) using simpler approaches like "ear clipping".

like image 102
Gareth Rees Avatar answered Jan 29 '23 10:01

Gareth Rees