2

I use TikZ to draw arrows and labels on maps, generated from other applications.

A minimal example with a global mercator projection might look like this:

\documentclass[]{standalone}
\usepackage{tikz}

\begin{document}
    \begin{tikzpicture}
        \node[anchor=south west,inner sep=0] (image) at (0,0) 
            {\includegraphics[width=18cm]{my_mercator_world_map.jpg}};
            \begin{scope}[x={(image.south east)},y={(image.north west)}]
                \draw [->] (0.5,1) -- (0.5421,0.6353);
                \node[draw] at (0.2,0.05) {a}; 
        \end{scope}
    \end{tikzpicture}   
\end{document}

enter image description here

It's easy to normalise map coordinates to TikZ coordinates, but sometimes I have many labels or even TikZ generated polygons. It would be very convenient to give the coordinates as geographical coordinates instead of a number between 0 and 1.

Is there a simple way to normalise TikZ coordinates so that they can be given in e.g. decimal lat-lon if I know the exact extent of the map?

In the case above, I'd like to be able to give coordinates as:

\draw [->] (0,90) -- (7.578,12.177);
\node[draw] at (-108,-81) {a}; 

I also work with local maps, but we can assume that all have a rectangular grid. Map coordinates to any projection would be fantastic, but I guess that is beyond the scope of TikZ.


EDIT

A partly working answer can be found here: tex.stackexchange.com/a/9562/121799

5
  • 1
    Sure: tex.stackexchange.com/a/9562/121799 – user121799 Nov 13 '18 at 1:02
  • Yes. That's the way! As the question is framed differently, I didn't find that answer. – user2821 Nov 13 '18 at 1:07
  • Glad to hear! Is your question answered by that, or do you want to get an official answer, or do you want to get it closed as a duplicate such that people searching for keywords contained in your question get dragged to the original answer? – user121799 Nov 13 '18 at 2:02
  • I'm happy with that answer, but as the linked answer is an answer to a different question, it might be hard to find. Linking this as a duplicate OR a further developed answer to this might be useful for future users. – user2821 Nov 13 '18 at 2:24
  • 1
    Hmmm... the linked answer still use [0..1]x[0..1] as coordinate systems, not [-180..180]x[-90..90]... so an explicit answer could be better, I think! – Rmano Nov 13 '18 at 13:17
4

Based on the linked answer, and using coordinate transformations, you can do this:

\documentclass[tikz]{standalone}
\usetikzlibrary{fpu, calc}
\begin{document}
\begin{tikzpicture}
    \node[anchor=south west,inner sep=0] (image) at (0,0) {\includegraphics[width=0.9\textwidth]{example-image}};
    \begin{scope}[x={($1/360*(image.south east)$)},
        y={($1/180*(image.north west)$)},
        shift={(180,90)},
        ]
        \draw[help lines, red, xstep=45,ystep=30] (-180,-90) grid (180,90);
        \foreach \x in {-180, -135,...,180} { \node [anchor=north] at (\x,-90) {\x}; }
        \foreach \y in {-90,-60,...,90} { \node [anchor=east] at (-180,\y) {\y}; }
    \end{scope}
\end{tikzpicture}
\end{document}

enter image description here

For more complex transformation, you can define your own coordinate system; look at the tikz manual, section 13.2.5 "Defining New Coordinate System" (at pag. 137 in my copy) and at this example.

Then you can use it, like for example:

\documentclass[tikz]{standalone}
\usetikzlibrary{fpu, calc}
\newcommand{\showpoint}[3][5,-15]{
    \path (#2) node[circle, fill=blue, inner sep=1pt]{} coordinate(tmp);
    \draw [thick, blue, <-] (tmp) -- ++(#1) 
        node[fill=white,]{#3};
    }
\begin{document}
\begin{tikzpicture}
    \node[anchor=south west,inner sep=0] (image) at (0,0) {\includegraphics[width=0.9\textwidth]{example-image}};
    \begin{scope}[x={($1/360*(image.south east)$)},
        y={($1/180*(image.north west)$)},
        shift={(180,90)},
        ]
        \draw[help lines, red, xstep=45,ystep=30] (-180,-90) grid (180,90);
        \foreach \x in {-180, -135,...,180} { \node [anchor=north] at (\x,-90) {\x}; }
        \foreach \y in {-90,-60,...,90} { \node [anchor=east] at (-180,\y) {\y}; }
        % use them
            \showpoint{0,90}{North Pole}
            \showpoint{100,90}{still North Pole}
            \showpoint{-100,90}{North Pole, again}
            \showpoint{-3,40}{Madrid}
            \showpoint{-105,40}{Denver}
            \showpoint[5,15]{10,44}{Sarzana}
            %
            \showpoint{0,0}{geolocation list bug}
    \end{scope}
\end{tikzpicture}
\end{document}

enter image description here

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.