3

Follow up from this question: gCentroid shifting centroid towards concentration of points, but with a trickier shape.

Using this SpatialPointsDataFrame I calculated the center using gCentroid, but it places it outside the shape.

# c. = [dput'd data from link]
poly <- SpatialPolygons(Srl = list(Polygons( srl = list(Polygon(coords = coordinates(c.))), ID = 1)))
plot(poly)
axis(1)
axis(2)
points(rgeos::gCentroid(poly)) 

enter image description here

How can I return coordinates for a center point that falls inside the specified shape?

As pointed out in the comments for these sort of shapes, the center falls outside the boundaries. What I am looking for I think is better described as the "center of mass", the point within a polygon that is farthest from boundaries.

enter image description here

Henrik
  • 65,555
  • 14
  • 143
  • 159
Rafael
  • 3,096
  • 1
  • 23
  • 61
  • 1
    It's been a while since I was in school but I believe that only convex polygons are guaranteed to have a centroid inside the shape. – svenhalvorson Jan 25 '19 at 21:23
  • 1
    Think of a torus (a region with two concentric rings as the boundary). OF COURSE the centroid is outside the region. If you want something inside the region, you will have to define what you are looking for. – G5W Jan 25 '19 at 21:24
  • A very closely related question about finding the "visual center" of an irregular polygon has been asked here: https://stackoverflow.com/questions/1203135/what-is-the-fastest-way-to-find-the-visual-center-of-an-irregularly-shaped-pol/65409262#65409262 – mjkvaak Jun 28 '23 at 08:52

1 Answers1

3

polylabelR::poi (Pole of Inaccessibility (Visual Center) of a Polygon) seems to do the trick. Related approach: https://blog.mapbox.com/a-new-algorithm-for-finding-a-visual-center-of-a-polygon-7c77e6492fbc

poly <- SpatialPolygons(Srl = list(Polygons( srl = list(Polygon(coords = coordinates(c.))), ID = 1)))
plot(poly)
axis(1)
axis(2)
points(rgeos::gCentroid(c.)) 

p <- polylabelr::poi(xx$long, xx$lat, precision = 0.01)
points(p)

enter image description here

Henrik
  • 65,555
  • 14
  • 143
  • 159
Rafael
  • 3,096
  • 1
  • 23
  • 61