I want to make an adjacency matrix for the world/each country at a specified administrative level. This is very easy to do for countries in Mathematica, but is not straightforward for different administrative divisions.

Here are examples of different administrative levels for Myanmar:

Here is the adjacency matrix with naming columns for Myanmar at the first (not zeroth) administrative level:

produced by the code:

   MyanmarProvinceRelations =
"Sagaing"<->"Magway","Sagaing"<->"Chin","Shan"<->"Kayah","Shan"<->"Mandalay",
"Shan"<->"Kayin","Kayin"<->"Bago","Kayin"<->"Mon","Mon"<->"Tanintharyi","Bago"<->"Yangon",
g = Graph[MyanmarProvinceRelations];
(* Get the ordered list of vertex names *)
vertices = VertexList[g];


I might have missed some relationships. It is difficult to keep track of manually.

Here is how to get the nearest neighbor graph (not Adjacency graph, but it is okay for my purposes.):

allCountriesFromContinents = Flatten[CountryData /@ {"SouthAmerica", "NorthAmerica", "Europe", "Asia", "Africa", "Oceania"}];
nearestNeighborGraph=NearestNeighborGraph[allCountriesFromContinents,
VertexShapeFunction -> "Name",ImageSize->Full];
countryNames = VertexList[nearestNeighborGraph];
countryNamesStrings = EntityValue[countryNames, "Name"];
grid = Grid[countryAdjacencyTable, Frame -> All]


But for example when I try to do the NearestNeighborGraph[] for a region, like for Arizona or Myanmar doing:

divisions =
EntityValue[
Entity["Country", "UnitedStates"]}], "Entities"];
nearestNeighborGraph=NearestNeighborGraph[arizonaStuff,  VertexShapeFunction -> "Name"]


I get the error:

The default distance function does not give a real numeric distance when applied to the point pair

And I cannot figure out how to fix this.

• I do not think that any data about countries in Mathematica are reliable. Maybe about USA and other developed countries but not about states like Myanmar. Commented Apr 14 at 12:37
• @azerbajdzan Thank you. I will check that out. You are probably right. Commented Apr 14 at 12:56

divisions =
EntityValue[
Entity["Country", "Myanmar"]}], "Entities"];
rule = Entity["AdministrativeDivision", {x_, "Myanmar"}] :> x;

divisions /. rule;
Graph[Sort /@
DeleteCases[
UndirectedEdge[x_, x_]] // DeleteDuplicates,
VertexLabels -> Automatic]
Grid[Prepend[#[[2]], #[[1]]] & /@
Transpose[{Prepend[VertexList[%], ""],
Frame -> All]


Two remarks:

First. Who could ever come up with such a cumbersome syntax to get administrative divisions of a country (Myanmar)? This nobody can remember.

EntityValue[
Entity["Country", "Myanmar"]}], "Entities"]


Second. There is inconsistency with the above code and CountryData in naming one region - first code names it Yangon the second Rangoon.

CountryData["Myanmar", "Regions"]
divisions /. rule


{"Ayeyarwady", "Bago", "Chin", "Kachin", "Kayah", "Kayin", "Magway", "Mandalay", "Mon", "Rakhine", "Sagaing", "Shan", "Tanintharyi", "Yangon"}

{"Ayeyarwady", "Bago", "Chin", "Kachin", "Kayah", "Kayin", "Magway", "Mandalay", "Mon", "Rakhine", "Rangoon", "Sagaing", "Shan", "Tanintharyi"}

• I got the syntax from the documentation. And I 100% the likely required syntax is cumbersome. I am amazed you or anyone was able to solve it. Commented Apr 14 at 16:03
• @TegLouis I got it from documentation too. The remark was meant that the Mathematica's authors of the functions regarding country data made them cumbersome, not you. Commented Apr 14 at 16:09