title: How to convert spherical coordinates to equirectangular projection coordinates?
body: <h1>Simplified question</h1>
  <p>How do you convert a <a href="https://en.wikipedia.org/wiki/Spherical_coordinate_system" rel="noreferrer">spherical coordinate</a> (θ, φ) into a position (x, y) on an <a href="https://en.wikipedia.org/wiki/Equirectangular_projection" rel="noreferrer">equirectangular projection</a> (also called 'geographic projection')?</p>
  <p>In which:</p>
  <ul>
  <li>x is longitude, the horizontal position, from -180 to 180 degrees.</li>
  <li>y is latitude, the vertical position, from -90 to 90 degrees.</li>
  <li>θ is theta, the horizontal angle in degrees, a vector from (0,0,0) to a point on the surface of a sphere.</li>
  <li>φ is phi, the vertical angle in degrees, a vector from (0,0,0) to a point on the surface of a sphere.</li>
  </ul>
  <hr />
  <p><em>Below you find the original question, back when I did not understand the problem well, but I think that it is still good for showing what is a practical application of this solution.</em></p>
  <h1>Context</h1>
  <p>Edit: <em>The original question title was: How to transform a photo at a given angle to become a part of a panorama photo?</em></p>
  <p>Can anybody help me with which steps I should take if I want to transform a photo taken at any given angle in such a way that I can place the resulting (distorted/transformed) image at the corresponding specific location on an equirectangular projection, cube map, or any panorama photo projection?</p>
  <p>Whichever projection is easiest to do is good enough, because there are plenty of resources on how to convert between different projections. I just don't know how to do the step from an actual photo to such a projection.</p>
  <p>It is safe to assume that the camera will stay at a fixed location, and can rotate in any direction from there. The data that I think is required to do this, is probably something like this:</p>
  <ul>
  <li>Horizontal angle of physical camera <code>[-180, +180]</code> (e.g. +140deg).</li>
  <li>Vertical angle of physical camera <code>[-90, +90]</code> (e.g. -30deg).</li>
  <li>Resolution of photo <code>w x h</code> (e.g. 1280x720 pixels).</li>
  <li>Horizontal angle of photo (e.g. 70deg).</li>
  <li>Vertical angle of photo (e.g. 40deg).</li>
  <li>Lens correction a, b, c parameters (see below).</li>
  </ul>
  <p>I have this data, and I guess the first step is to do lens correction so that all lines that should be straight are in fact straight. And this can be done using <code>imagemagick</code>'s <a href="http://www.imagemagick.org/Usage/distorts/#barrel" rel="noreferrer" title="Barrel Distortion">Barrel Distortion</a>, in which you only need to fill in three parameters: a, b, and c. The transformation that is applied to the image to correct this is straightforward.</p>
  <p>I am stuck on the next step. Either I do not fully understand it, or search engines are not helping me, because most results are about converting between already given projections or use advanced applications to stitch photos intelligently together. These results did not help me with answering my question.</p>
  <p>EDIT: I figured that maybe a figure will help explaining it better :)</p>
  <p>The problem is that a given photo <strong>Red</strong> cannot be placed into the equirectangular projection without a transformation. The figure below illustrates the problem.</p>
  <p><a href="https://i.stack.imgur.com/b0IkT.png" rel="noreferrer"><img src="https://i.stack.imgur.com/b0IkT.png" alt="Equirectangular projection with a &quot;normal&quot; photo" /></a></p>
  <p>So, I have <strong>Red</strong>, and I need to transform it into <strong>Green</strong>. <strong>Blue</strong> shows the difference in transformation, but this depends on the horizontal/vertical angle.</p>