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 =
{"Kachin"<->"Sagaing","Kachin"<->"Shan","Sagaing"<->"Shan","Sagaing"<->"Manadalay","Magway"<->"Manadalay","Magway"<->"Bago",
"Sagaing"<->"Magway","Sagaing"<->"Chin","Shan"<->"Kayah","Shan"<->"Mandalay",
"Shan"<->"Kayin","Kayin"<->"Bago","Kayin"<->"Mon","Mon"<->"Tanintharyi","Bago"<->"Yangon",
"Yangon"<->"Ayeyarwady","Bago"<->"Ayeyarwady","Magway"<->"Rakhine","Rakhine"<->"Chin","Magway"<->"Chin"};
g = Graph[MyanmarProvinceRelations];
am = AdjacencyMatrix[g];
(* Get the ordered list of vertex names *)
vertices = VertexList[g];
(* Display the adjacency matrix with table headings *)
TableForm[am, TableHeadings -> {vertices, vertices}]
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"];
countryAdjacencyMatrix = AdjacencyMatrix[nearestNeighborGraph];
countryAdjacencyTable = Prepend[MapThread[Prepend, {Normal[countryAdjacencyMatrix], countryNamesStrings}], Prepend[countryNamesStrings, ""]];
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["AdministrativeDivision", {EntityProperty[
"AdministrativeDivision", "ParentRegion"] ->
Entity["Country", "UnitedStates"]}], "Entities"];
arizonaStuff=AdministrativeDivisionData[divisions[[3]],"Subdivisions"];
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.