5
$\begingroup$

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:

This is administrative level 1 for Myanmar. It is next coarsest after the main country.

This is administrative level 2 for Myanmar.

This is administrative level 3 for Myanmar. It is the finest map with many small boundaries.

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

Myanmar Provincial Relations Table

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.

$\endgroup$
2
  • 1
    $\begingroup$ 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. $\endgroup$ Commented Apr 14 at 12:37
  • $\begingroup$ @azerbajdzan Thank you. I will check that out. You are probably right. $\endgroup$
    – Teg Louis
    Commented Apr 14 at 12:56

1 Answer 1

1
+200
$\begingroup$
divisions = 
  EntityValue[
   Entity["AdministrativeDivision", {EntityProperty[
       "AdministrativeDivision", "ParentRegion"] -> 
      Entity["Country", "Myanmar"]}], "Entities"];
rule = Entity["AdministrativeDivision", {x_, "Myanmar"}] :> x;


Cases[GeoNearest["AdministrativeDivision1", #], 
     Entity["AdministrativeDivision", {_, "Myanmar"}]] & /@ 
   divisions /. rule;
Graph[Sort /@ 
   DeleteCases[
    Flatten[Thread /@ Thread[UndirectedEdge[divisions /. rule, %]]], 
    UndirectedEdge[x_, x_]] // DeleteDuplicates, 
 VertexLabels -> Automatic]
Grid[Prepend[#[[2]], #[[1]]] & /@ 
  Transpose[{Prepend[VertexList[%], ""], 
    Prepend[Normal[AdjacencyMatrix[%]], VertexList[%]]}], 
 Frame -> All]

enter image description here

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["AdministrativeDivision", {EntityProperty[
       "AdministrativeDivision", "ParentRegion"] -> 
      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"}

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$
    – Teg Louis
    Commented Apr 14 at 16:03
  • 1
    $\begingroup$ @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. $\endgroup$ Commented Apr 14 at 16:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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