12

I have a set of 2d grid points (x,y) that I want to map/project onto a sphere as 3d points (x,y,z).

I realize there will be some warping towards the poles as abs(y) increases but my grid patch will only cover a portion of the sphere near the equator so severe warping will be avoided.

I'm having trouble finding the right equations for that.

user1118321
  • 25,567
  • 4
  • 55
  • 86
milkplus
  • 33,007
  • 7
  • 30
  • 31
  • 1
    I'm not sure what you're asking here...What do the 2D `x` and `y` represent? Are they latitude/longitude, or coordinates on some flat rectangular projection of the sphere? In the last case, what projection are you using? – Rody Oldenhuis Oct 04 '12 at 19:07

3 Answers3

23

Paraphrased from the wikipedia article on Mercator projection:

Given a "mapping sphere" of radius R,
the Mercator projection (x,y) of a given latitude and longitude is:
   x = R * longitude
   y = R * log( tan( (latitude + pi/2)/2 ) )

and the inverse mapping of a given map location (x,y) is:
  longitude = x / R
  latitude = 2 * atan(exp(y/R)) - pi/2

To get the 3D coordinates from the result of the inverse mapping:

Given longitude and latitude on a sphere of radius S,
the 3D coordinates P = (P.x, P.y, P.z) are:
  P.x = S * cos(latitude) * cos(longitude)
  P.y = S * cos(latitude) * sin(longitude)
  P.z = S * sin(latitude)

(Note that the "map radius" and the "3D radius" will almost certainly have different values, so I have used different variable names.)

comingstorm
  • 25,557
  • 3
  • 43
  • 67
  • My latitude is 29.65163. When I try to calculate the y value, I'm getting an error (in Python) because `tan((latitude + pi/2)/2)` is -0.0970531183, and performing log on that value throws a "math domain error" since the value is negative. What am I doing wrong? – Pikamander2 Dec 18 '15 at 13:28
  • You need to convert your latitude from degrees to radians, before adding `pi/2`. This will convert the range `+/- 90 degrees` to a range of `+/- pi/2 radians`, which will not overflow the function range unless you are at the poles (in which case the Mercator projection is singular anyway...) – comingstorm Dec 25 '15 at 05:49
  • Do you know how to get the latitude and longitude from x, y, and z? – PyRulez Aug 12 '18 at 15:24
  • 1
    Latitude is `atan2(z, sqrt(x*x+y*y))`, and longitude is `atan2(y,x)`. Both of these yield radians, which can be used directly in the inverse mapping, but which must be converted to degrees if that's what you want instead. – comingstorm Aug 14 '18 at 03:18
2

I suppose that your (x,y) on the sphere are latitude, longitude.

If so, see http://tutorial.math.lamar.edu/Classes/CalcII/SphericalCoords.aspx.

enter image description here

There:

phi = 90 degree - latitude

theta = longitude

rho = radius of your sphere.

farfareast
  • 2,179
  • 1
  • 23
  • 22
1

I would expect that you could use the inverse of any of a number of globe projections.

Mercator is pretty good around the equator compared to other projections.

Formulas are on the wiki page.
http://en.wikipedia.org/wiki/Mercator_projection

kreativitea
  • 1,741
  • 12
  • 14
  • thanks. i realize that is what i want but i'm having trouble deriving the equations for taking 2d points (x,y) to 3d points (x,y,z) on the sphere. – milkplus Oct 04 '12 at 17:50
  • Ah, the actual formulae. This is a non-trivial mathematical operation, and you might have better luck on http://math.stackexchange.com/. Once you get the formula, you can come back here for help programming it. Also, http://wiki.openstreetmap.org/wiki/Mercator – kreativitea Oct 04 '12 at 18:26