4

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)]
Gianni Spear
  • 7,033
  • 22
  • 82
  • 131
  • Sorry Sheena, i added the question – Gianni Spear Dec 06 '12 at 13:38
  • 2
    Do you have a definition of “inner centroid”? – Chronial Dec 06 '12 at 13:44
  • Dear @Chronial. I am honest to say that i didn't find an official definition of "inner centroid". I am follwoing this example http://www.georeference.org/forum/t37396 – Gianni Spear Dec 06 '12 at 13:47
  • Why do you need it, if you are not quite sure what it is? :) – Chronial Dec 06 '12 at 13:48
  • Not sure if this answers your question, but GRASS's [v.centroids](http://grass.osgeo.org/grass64/manuals/v.centroids.html) does exactly that. Unless you're already working in grass it would be quite cumbersome to wrap their code though. – aquavitae Dec 06 '12 at 13:50
  • Dear @Chronial i am testing a geometry correlation between Two polygons. The formula use the centroid as reference: the approach check if the centroid of polygons A drop inside Polygon B, and vice versa. I wish to use the "inner centroid" in order to avoid problem with not regular shape (mostly in my case) – Gianni Spear Dec 06 '12 at 13:51
  • dear @aquavitae thanks, but i prefer to work outside software (i know GRASS and I like) – Gianni Spear Dec 06 '12 at 13:52
  • @Gianni Not quite sure I understand your goal. But are you looking for any point inside the polygon or one with special attributes? – Chronial Dec 06 '12 at 13:53
  • dear @Chronial the "inner points" correspond to center of mass. there are not unique definition, and i am sorry if i cannot give you the right definition. – Gianni Spear Dec 06 '12 at 13:56
  • @Chronial following this example http://www.georeference.org/doc/transform_centroids.htm – Gianni Spear Dec 06 '12 at 13:58
  • The inner centroids in the given examples are all quite far away from the center of mass and I don’t really understand what the point of the is / how these points relate to centroids at all :/. – Chronial Dec 06 '12 at 14:02
  • @Chronial reading online the "inner centroid" is also define as "center of balance". Some application use the "inner centroid" because for very strange area shapes (ex: horseshoe shapes) will cause the centroid point to be placed outside the area – Gianni Spear Dec 06 '12 at 14:04
  • From looking at this topic I gather that the “inner centroid” is not properly defined in any way, and actually the term itself doesn’t make too much sense. The “center of balance” means the same as “centroid”. “inner centroid” seems to be a proprietary term used by some software. If yo extend your question to explain what you are actually trying to do, somebody might be able to help you. – Chronial Dec 06 '12 at 14:08
  • @Chronial probably here there is an explanation http://gis.stackexchange.com/questions/11200/how-does-arcgis-calculate-the-inside-point-for-feature-to-point – Gianni Spear Dec 06 '12 at 14:12

1 Answers1

10

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".

Gareth Rees
  • 64,967
  • 9
  • 133
  • 163
  • Dear Gareth, Thanks for your replay and suggestions. In this link http://gis.stackexchange.com/questions/11200/how-does-arcgis-calculate-the-inside-point-for-feature-to-point you can read the "inner centroid" algorithm (of ArcGIS software) seem to indicate its a black box. – Gianni Spear Dec 06 '12 at 14:23
  • Dear @Garecth. do you know a build-in module to triangualte a polygons? – Gianni Spear Dec 06 '12 at 14:26
  • There's no geometry code built-in to Python. But the [poly2tri](http://code.google.com/p/poly2tri/) library has a Python interface. – Gareth Rees Dec 06 '12 at 14:29