4
$\begingroup$

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]

enter image description here

$\endgroup$
4
$\begingroup$

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]

Mathematica graphics

|improve this answer|||||
$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Michael Seifert Mar 7 '17 at 18:53
  • 2
    $\begingroup$ 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. $\endgroup$ – Michael Seifert Mar 7 '17 at 19:01
  • 2
    $\begingroup$ 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. $\endgroup$ – kirma Mar 7 '17 at 19:57
4
$\begingroup$
CountryData["Italy", "CenterLocationLink"]
"http://maps.google.com/maps?q=+42.8333,+12.8333&z=6&\"
"q" /. URLParse[
    First @ CountryData["Italy", "CenterLocationLink"]
]["Query"] // Interpreter["GeoCoordinates"]
GeoPosition[{42.8333, 12.8333}]
|improve this answer|||||
$\endgroup$
0
$\begingroup$

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]}]]

enter image description here

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

enter image description here

Or

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

centre = RegionCentroid[shape3d];

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

enter image description here

|improve this answer|||||
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.