Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find visual center of a polygon

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

like image 655
Rafael Avatar asked Jan 29 '26 23:01

Rafael


1 Answers

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

like image 140
Rafael Avatar answered Feb 01 '26 13:02

Rafael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!