# Centroid of country shape

How can I use RegionCentroid[] to find geometric centroid of the given country shape?

GeoGraphics[{EdgeForm[Black], FaceForm[Black],
Polygon[Entity["Country","Italy"]]}, GeoBackground -> None, PlotRange -> All]


You can use the data directly, asking for the "Location" property

loc1 =
Entity["Country", "Italy"][EntityProperty["Country", "Position"]]
(* GeoPosition[{42.8333, 12.8333}] *)


Or you can use the RegionCentroid functionality on the Polygon - but you have to reverse the coordinates in the end

loc2 =
Entity["Country", "Italy"]["Polygon"] // DiscretizeGraphics //
RegionCentroid // Reverse // GeoPosition
(* GeoPosition[{42.7927, 12.0783}] *)


The two points aren't far from each other,

GeoGraphics[{EdgeForm[Black], FaceForm[Black],
Polygon[Entity["Country", "Italy"]], Red, PointSize@Large,
Point@loc1, Blue, Point@loc2}, GeoBackground -> None,
PlotRange -> All]


• The "Position" property of a Country entity appears to always have integer latitude & longitude (or a fractional part with a small denominator — 5/6 in the case of Italy.) I suspect that it's just a "nice" point that someone at Wolfram picked once upon a time, and doesn't have any special geographical significance. – Michael Seifert Mar 7 '17 at 18:53
• Also, if the country has an odd shape, the centroid can be quite a distance from the location of the country's Position; it can even lie outside of the country's borders, which the Position never does (so far as I've found). See Chile, Norway, Vietnam, and Japan for various example of these behaviors. – Michael Seifert Mar 7 '17 at 19:01
• I don't think this is a trivial problem at all, especially at higher latitudes. It involves computation of centroid over a region on the chosen geoid; just assuming planar geometry for longitude and latitude is typically an acceptable approximation, but hardly a proper solution. – kirma Mar 7 '17 at 19:57
CountryData["Italy", "CenterLocationLink"]

"http://maps.google.com/maps?q=+42.8333,+12.8333&z=6&\"

"q" /. URLParse[
]["Query"] // Interpreter["GeoCoordinates"]

GeoPosition[{42.8333, 12.8333}]


I use ImageMesh[] and it works perfectly..

shape = GeoGraphics[{EdgeForm[Black], FaceForm[Red],
Polygon[Entity["Country", "Italy"]]}, GeoBackground -> None, PlotRange -> All];

centre = Binarize[shape] // ImageMesh // RegionCentroid

{161.979, 241.223}

Show[Binarize[shape] // ImageMesh, Graphics[{Red, PointSize[Large], Point[centre]}]]


Also it works for Japan , (when centroid is outside of the country's borders)

Or

shape3d = GeoElevationData[Entity["Country", "Japan"], Automatic, "Region"];

centre = RegionCentroid[shape3d];

Show[shape3d, Graphics3D[{Red, PointSize[0.03], Point[centre]}]]