1
$\begingroup$

Needed to draw a salesman territory map, multicolor like a schoolhouse USA states map, with the Postal Abbreviation location at the state capital. Wished to do it with GeoGraphics and curated geography information already in modern Mathematica systems.

USA state map

$\endgroup$
1
  • $\begingroup$ That's neat! Is there some meaning behind the colours chosen for each state? $\endgroup$
    – MassDefect
    May 21 '19 at 18:38
3
$\begingroup$

You can do this quite smoothly in a few lines of code:

GeoGraphics[
 {GeoStyling[Opacity@1, EdgeForm[Gray], 
     FaceForm[RandomChoice@{LightGreen, LightBlue, LightRed, Yellow}]],
    Polygon@#,
    Text[Style[#["StateAbbreviation"], 
      FontSize -> 7], #["CapitalCity"]["Position"]]} & /@ 
  EntityList[
   EntityClass["AdministrativeDivision", "ContinentalUSStates"]], 
 ImageSize -> Full]

enter image description here

$\endgroup$
4
  • $\begingroup$ outstanding, so lovely, so much functionality from so little code $\endgroup$ May 21 '19 at 20:50
  • $\begingroup$ @Manuel--Moe--G Glad you like it :) Do you understand what's going on in the code, or would you like some more explanation? $\endgroup$
    – Carl Lange
    May 21 '19 at 20:57
  • $\begingroup$ Using help on the keywords, I should be OK, thanks so much! $\endgroup$ May 21 '19 at 21:02
  • $\begingroup$ No problem! Feel free to ask if something is unclear. $\endgroup$
    – Carl Lange
    May 21 '19 at 21:36
1
$\begingroup$

Below has some extra code for a shorthand for Reap into a list, also a pattern for datasets with a logical primary key. May have gone overboard with the Normal, as I am not yet completely comfortable with Association and Dataset.

reapList::usage="gracefully collect all Sow expressions into list, same shape even if empty";
SetAttributes[reapList,HoldAll];
reapList[r__]:=Flatten[Reap[r,{_}][[2]],2]

allUSAStatesList={
    "stateAbbreviation"->#[EntityProperty["AdministrativeDivision","StateAbbreviation"]],
    "name"->StringSplit[#[EntityProperty["AdministrativeDivision","Name"]],", "][[1]],
    "entityMA"->#,
    "captialCityCoordinates"->#[EntityProperty["AdministrativeDivision","CapitalCity"]][EntityProperty["City","Coordinates"]],
    "polygon"->Polygon[#]
   }&/@EntityValue[Entity["Country","UnitedStates"],"AdministrativeDivisions"];
"";

allUSAStatesDataset=Dataset[Association[#]&/@allUSAStatesList];
allUSAStates[s_String]:=First@Select[allUSAStatesDataset,#stateAbbreviation===s&];
allUSAStates[k_String,col_String]:=allUSAStates[k][col];
allUSAStates[All]:=Association[#->allUSAStates[#]&/@(Sort@Normal@allUSAStatesDataset[All,"stateAbbreviation"])];
allUSAStates[All,col_String]:=Association[#->allUSAStates[#][col]&/@(Sort@Normal@allUSAStatesDataset[All,"stateAbbreviation"])]

f01[statekey_String->a_]:=(
  Sow@GeoStyling[EdgeForm[Gray],RandomChoice@{LightGreen,LightBlue,LightRed,Yellow}];
  Sow@a["polygon"];
 )

f02[statekey_String->a_]:=(
  Sow@GeoMarker[a["captialCityCoordinates"],Text@Style[a["stateAbbreviation"],Black]]
 )

GeoGraphics@With[
  {lower48=DeleteCases[Normal@allUSAStates[All],("HI"|"AK")->_]},
  reapList[
   f01/@lower48;
   f02/@lower48
  ]
 ]

Output is above in question.

$\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.