Mercator projection: problem with latitude formula

The answer to the previous question explains how to determine the correct pixel position at a given degree on a Mercator map. For example plug in a given map width in pixels and a given latitude in degrees and it will spit out the correct pixel position. I've tested it with dozens of possible degrees and it always comes back with the right answer. It's awesome.

This is the equation. The 2 variables are mapWidth ($w$) and degreePosition ($d$) and the output is pixelPosition ($p$).

$$p=\frac w{2\pi}\ln\tan\left(\frac\pi4+\frac d2\cdot\frac\pi{180}\right)$$

However I need to solve this equation for degreePosition and my brain has reached the breaking point. After multiple attempts I can not figure out what to do. degreePosition is buried inside a natural log and tangent and I can't figure out how to get it out to one side.

Thanks so much for any help!


\begin{align} \frac w{2\pi} \ln\tan\left(\frac\pi4 + \frac d2\cdot\frac\pi{180}\right) &= p\\ \ln\tan\left(\frac\pi4 + \frac d2\cdot\frac\pi{180}\right) &= \frac{2\pi p}w \\ \tan\left(\frac\pi4 + \frac d2\cdot\frac\pi{180}\right) &= \exp\left(\frac{2\pi p}w\right) \\ \frac\pi4 + \frac d2\cdot\frac\pi{180} &= \arctan\exp\left(\frac{2\pi p}w\right) \\ \frac d2\cdot\frac\pi{180} &= \arctan\exp\left(\frac{2\pi p}w\right) - \frac\pi4 \\ d &= \frac{360}\pi \left(\arctan\exp\left(\frac{2\pi p}w\right) - \frac\pi4\right) \end{align}

  • $\begingroup$ Awesome!!! But I found one mistake when I checked it. 360/pi should be multiplied by pi/4 as well. I fixed that one part and it worked flawlessly. Thanks so much! $\endgroup$ – DR01D Mar 2 '18 at 4:46
  • 1
    $\begingroup$ @DR01D You're right! Silly mistake forgetting the parentheses, but I've fixed that for the benefit of anyone else who happens to look at this. $\endgroup$ – David K Mar 2 '18 at 15:04
  • $\begingroup$ If you hadn't provided the steps I would have never been able to spot that. Also by going through your steps I learned a lot! $\endgroup$ – DR01D Mar 2 '18 at 17:23

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.