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