draw colored world/US map in LaTeX

Here's a TikZ picture producing the USA map. I produced it as follows:

  1. I downloaded the svg map from wikipedia
  2. I used Inkscape to translate the svg to tikz code (in verbose mode)
  3. the verbose mode annotated the paths with the abbreviated names of the states so I just copied the names and put them as the style of each path (you can do it with a regex)
  4. I defined a state style and a style for each state.

You can repeat the same steps starting from https://commons.wikimedia.org/wiki/File:BlankMap-World6.svg to get a World map.

Now you can color colorado (or modify many aspects of its rendering) just by redefining the CO style. You can wrap all this in a macro if you want with an optional style parameter that you can use to overwrite the state's styles.

The code is too long for this answer I posted it as a gist at

https://gist.github.com/bordaigorl/fce575813ff943f47505

Update

The Gist above now contains two files: USAmap.tex and main.tex. The first defines a command \USA with an optional argument. The main.tex file shows how to use it:

\documentclass{standalone}
\usepackage{tikz}

\input{USAmap.tex}

\begin{document}

\begin{tikzpicture}
\USA[every state={draw=white, ultra thick, fill=black!10},CA={fill=blue},NY={fill=red}]

\draw[dashed] (TX.center) -- (CA.center) -- (AL.center);
\node at (CO) {CO};
\end{tikzpicture}

\end{document}

generates the following

preview

The main addition is the definition of styles to control the appearance of each state and the creation of (approximate) nodes for each state so you can for example attach labels to states or draw paths etc. The only drawback is that the shape of the node is the bounding box of the path of the state (for simplicity and performance reasons) so the center may lay outside it (see FL for example) but it should be enough to give you an idea of what can be done with this approach.

The styles of the states can also be set globally using tikz as in

\tikzset{set state val/.style args={#1/#2}{#1={fill=blue!#2}}}
\tikzset{set state val/.list={CA/100,FL/80,TX/55,NY/20}}

which would interpret the list {CA/100,FL/80,TX/55,NY/20} as couples state/percentage and assign to each mentioned state a shade of blue with intensity proportional to the percentage.


Not a full solution, but perhaps a way to go on with it.

You have to install, if not done already, all map data files into some place, like pst-geo/data of your tex distribution and add the path to the \WorldMap command.

All data files can be obtained from ctan also: http://www.ctan.org/tex-archive/graphics/pstricks/contrib/pst-geo/data

Running with xelatex works, for some reason latex and consequent conversion with dvipdf fails.

The example is taken from the book of Herbert Voss (Example 25-3-1), available from http://www.ctan.org/tex-archive/info/examples/PSTricks_en

\documentclass{article}

\usepackage{pstricks,pst-map2d}

\begin{document}
\psset{unit=3.7,linewidth=.75pt}
\begin{pspicture*}(-6.5,1)(-3,3)
  \WorldMap[rivers=true,city=true,USA=true,maillage=true,path=/usr/local/texlive/2013/texmf-dist/tex/latex/pst-geo/data]
\end{pspicture*}

\end{document}

enter image description here


I know this thread is a bit stale, but I really liked Bordaigorl's answer above. Yet I am lazy, so I wrote a little perl script that translates the Inkscape-output into a nice macro source, along the lines of Bordaigorl's example above.

Steps to do:

  1. Download the SVG-map from Wikipedia. I used the World Map and also the "compact" version, which is a bit more flat and excludes the polar regions. So that is https://commons.wikimedia.org/wiki/File:BlankMap-World6.svg and https://commons.wikimedia.org/wiki/File:BlankMap-World6,_compact.svg.
    1. Open the SVG-file is Inkscape and export it as Tikz. In the export options, enable "verbose" mode, as this puts in all the object names as comments.
    2. Now path it through the perl script I add below, such as cat Worldmap-raw.tex | ./Map2tikz.pl >Worldmap.tex
    3. Include the file in TeX. An example is below as well. Depending on the Inkscape output, the "ocean" may be caught as a path and would end up with a style option [Map/state, Map/xx], which gives an error in the first TeX-run. In this case, open the Workmap.tex file, find the Map/xx text (somewhere around line 8 usually) and remove the whole "[Map/state, Map/SD]" part – or replace by a nice ocean style if you prefer.
    4. Exhibit happiness!

Here the perl-part:

#!/usr/bin/perl

# First written: Mon 2018-05-07 11:37:16 UTC+0200 (CEST) by Hendrik G. Seliger
# Last changes:  Mon 2018-05-07 14:36:54 UTC+0200 (CEST) by Hendrik G. Seliger

# A script to read a map exported by Inkscape as verbose tikz
# and set proper path names so we can highlight countries/states
# by using their identifier

###
$, = ' ';       # set output field separator
$\ = "\n";      # set output record separator

# While reading the file, we create three variables
# Set_Defaults: contains the command to set defaults for all regions
# Set_Commands: contains the code to set up commands to change each regions style
# Create_Map: The outline of the actual map. This changes the tikzpicture into a scope

$Set_Defaults = "\\tikzset{Map/.cd, state/.style={fill, draw=white, ultra thick}";
$Set_Commands = "\\tikzset{every state/.style={Map/state/.style={#1}}";
$Create_Map = "\\newcommand{\\Map}[1][]{";

# The we use a variable to always catch the current path name = state identifier
# We use "xx" to mark an unset state
$Current_State = "xx";

while ( <STDIN> ) {
#   chomp;  # strip record separator
    # Remember current line, as we will change it further down
    $Current_Line = $_;
    # Get the current state/country identifier
    if ( $Current_Line =~ /% (\w\w)$/) {
        $Current_State = $1;
        # Make it upper case
        $Current_State =~ s/(.)/\u$1/g;
        $Set_Defaults .= ", " . $Current_State . "/.style={}";
        $Set_Commands .= ", " . $Current_State . "/.style={Map/" . $Current_State .  "/.style={#1}}";
    }

    # rewrite path commands
    # \path -> \path[Map/state, Map/XX]
    # checking for "path " with space at end keeps the global/ocean background
    # which is exactly what we want
    if ( $Current_Line =~ /\\path /) {
        # Determine current style
        $Style = "[Map/state, Map/" . $Current_State . "] ";
        $Current_Line =~ s/path /path$Style/g;
    }

    # rewrite start if tikzpicture
    # \begin{tikzpicture}[y=0.80pt, x=0.80pt, yscale=-1.000000, xscale=1.000000, inner sep=0pt, outer sep=0pt] -> [y=0.80pt,x=0.80pt,yscale=-1, inner sep=0pt, outer sep=0pt,#1]
    if ($Current_Line =~ /\\begin{tikzpicture}\[(.*)\](.*)$/ ) {
        $Current_Line = "\\begin{scope}[" . $1 .",#1]" . $2 . "\n";
    }

    if ($Current_Line =~ /\\end{tikzpicture}/ ) {
        $Current_Line =~ s/\\end{tikzpicture}//g;
    }

    $Create_Map .= $Current_Line;
}

$Set_Defaults .= '}';
$Set_Commands .= '}';
$Create_Map .= "\\end{scope}}";

print $Set_Defaults;
print $Set_Commands;
print $Create_Map;

##### END OF MAIN, SUBROUTINES NEXT #####

Beware that the perl script does no error checking, so if you twiddle with the Inkscape output, it's not going to fly. And, it also may not work with other map files, e.g. if the country/state areas are not names properly.

BTW, I check on two-letter-identifiers for states/countries only, as this collects countries with several regions, such as MI on the US map, or France on the world map. The latter is a good example of breakdown at the next level, as all the departments are not names, so just exported by Inkscape as e.g. "path2168".

And a sample TeX-file (I use lualatex, as my normal pdflatex exceeds its memory on the world map):

% !TeX TS-program = lualatex
\documentclass{standalone}
\usepackage{tikz}

\input{Worldmap.tex}

\begin{document}

\begin{tikzpicture}
\Map[every state={draw=white, ultra thick, fill=black!10},DE={fill=blue},US={fill=red}]

\end{tikzpicture}

\end{document}

And per request, a screenshot of the resulting map: Screenshot of resulting map from TeXstudio viewer

Maybe this helps the one or the other.