PolygonCenter

The visual centre of a polygon: the point inside it that is farthest from its edges, which is where a label or a picture looks centred ("pole of inaccessibility"). Unlike the area centroid or the middle of the bounding box it is always inside the shape, and it sits in the fattest part of it: for a horseshoe or a country with a long arm the centroid can lie outside or in a thin stretch, while this lands in the body. It also gives the radius of the biggest circle that fits inside the shape around that point.

With several rings (see DrawnPolygon) the shape is what they enclose under the even-odd rule, so a hole is a place the centre keeps away from, and the centre of a shape of separate parts is in the fattest of them.

It is the polylabel algorithm (Mapbox): the bounding box is cut into cells, and only those that could still hold a point better than the best found so far are cut again.

The work is done on a flat map of the shape, with longitude shrunk by the cosine of the shape's middle latitude, so a degree of longitude and a degree of latitude are about the same length there. That is close for any shape of ordinary size; for one that crosses the antimeridian (its longitudes would have to be unwrapped first) or reaches a pole it is only an approximation.

Types

Link copied to clipboard
data class VisualCenter(val position: GeoPosition, val radiusMeters: Double)

A position and the radiusMeters of the biggest circle around it that stays inside the polygon.

Functions

Link copied to clipboard
fun visualCenter(ring: List<GeoPosition>, relativePrecision: Double = 0.001): PolygonCenter.VisualCenter

The visual centre of the polygon with outline ring (the last point is not repeated). It is found to within relativePrecision of the polygon's size (the default, 0.1%, is far finer than a label or picture can show). A ring of fewer than 3 points has no inside: the first point, radius 0.

Link copied to clipboard
fun visualCenterOfRings(rings: List<List<GeoPosition>>, relativePrecision: Double = 0.001): PolygonCenter.VisualCenter

The visual centre of the shape the rings enclose together (even-odd: a ring inside another is a hole). The first ring is the one used when there is nothing to find (no ring has 3 points, or the shape has no area). Rings of fewer than 3 points are ignored.