Quirks in Mathematica’s administrative division data for Mexico
If you ask Mathematica for a list of Mexican states via
CountryData["Mexico", "RegionNames"]
you will get a list of strings:
"Aguascalientes", "Baja California", ..., "Zacatecas"}
However, when you try to turn this into a list of objects representing these states via
states = Entity["AdministrativeDivision", {#, "Mexico"}] & /@ CountryData["Mexico", "RegionNames"]
something strange happens. Some items in the list are turned into useful objects, and some are uninterpreted symbols.
For example, Aguascalientes is recognized as an administrative division, but Baja California is not. It recognizes Oaxaca but not Nuevo Leon. The pattern is that states with a space are not recognized. There is an inconsistency in Mathematica: output names do not always match input names. To create the object representing Baja California, you need to pass in the string BajaCalifornia with no space.
Entity["AdministrativeDivision", {"BajaCalifornia", "Mexico"}]
OK, so let's remove spaces before we try to create a list of geographic objects.
names = StringReplace[#, " " -> ""] & /@ CountryData["Mexico", "RegionNames"]
This mostly works, but it trips up on Mexico City. The output name for the region is Ciudad de Mexico, but Mathematica does not recognize CiudaddeMexico as an administrative division. Mathematica does recognize MexicoCity as the name of a city but not as the name of an administrative division.
Changing CiudaddeMexico to MexicoCity in the list of names did not fix the problem. But when I directly edited the list of state objects by replacing the uninterpreted value with the output running
Entity["AdministrativeDivision", {"MexicoCity", "Mexico"}]
by itself everything worked. Then I was able to find a Traveling Salesman tour as in earlier posts (Africa, Americas, Eurasia and Oceania, Canada).
Traveling Salesman tour of MexicoThe tour is
- Baja California
- Baja California Sur
- Sinaloa
- Durango
- Zacatecas
- Aguascalientes
- Nayarit
- Jalisco
- Colima
- Michoacan
- Mexico
- Mexico City
- Morelos
- Guerrero
- Oaxaca
- Chiapas
- Tabasco
- Campeche
- Quintana Roo
- Yucatan
- Veracruz
- Puebla
- Tlaxcala
- Hidalgo
- Queretaro
- Guanajuato
- San Luis Potosi
- Tamaulipas
- Nuevo Leon
- Coahuila
- Chihuahua
- Sonora
The tour is 8,343 kilometers.
The post Quirks in Mathematica's administrative division data for Mexico first appeared on John D. Cook.