Or how to place a map coordinate into 3D space. This post is a brief walk through of the basics from my 2010 presentation “Where in the world? Intercontinental Ballistic Flash”.

Belgian, Flemish geographer and cartographer Gerardus Mercator, in 1569 presented a cylindrical map projection. The Mercator Projection quickly became the standard map used for nautical purposes. It’s advantages were that the scale is linear in any direction around a point, meaning all angles were preserved (conformal).

Around the equator feature shapes are accurately portrayed but as the poles are approached there is a size and shape distortion. The scale increases as the pole is neared, eventually becoming infinite when the pole is reached.

If you imagine the world as a beach ball. The Mercator Projection is the equivalent of cutting open the ball at each pole and stretching the skin to form a cylinder. Now slice down the cylinder and you have the map.

All map projections distort the shape or size of the Earth’s (or for that matter any other planet’s) surface. The Mercator Projection exaggerates areas far from the equator. Greenland seems as large as Africa, but is in fact 1/14 of the size, Antartica appears to be the largest continent but is in fact only 5th in terms of area.

Despite the obvious distortions, the Mercator Projection makes a great interactive map. Thanks to the conformality it can be zoomed nearly seamlessly.

So here we have a system that converts a 3D scene, the real world, onto a 2D plane, the map. This system can just as easily convert the 2D back to 3D. It is a coordinate system that requires two positional parameters and one constant: the planet radius.

The positional parameters are latitude and longitude. As developers we are used to standard coordinates x and y. Across then up. Horizontal then vertical. With latitude and longitude we have to think differently. Latitude is vertical and longitude is across. Up then across. Latitudes run from pole to pole, while longitudes run around the planet.

There are 180 degrees of latitude from the South Pole to the North pole, and 360 degrees around the planet. Imagine yourself sat at the centre of the Earth with a laser pointer. Point at the Equator then you are at 0 degrees latitude, at the South Pole -90 degrees and at the North Pole 90 degrees. Now point at the Equator again you can turn 180 degrees around the equator to the right (-180 degrees longitude) and to the left (180 degrees longitude).

With these any point on the surface of the planet can be recorded or indeed any position on a map.

So we have a system for calculating a position on the surface of a sphere, a 3D coordinate, but how do we convert latitude, longitude and the planet’s radius into a 3D x,y,z coordinate?

```
```
public static function convertLatLonTo3DPos(lat:Number, lon:Number, radius:Number):Vector3D

{

var v3d:Vector3D = new Vector3D();

//Convert Lat Lon Degrees to Radians

var latRadians:Number = lat * TO_RADIANS;

var lonRadians:Number = lon * TO_RADIANS;

v3d.x = radius * Math.cos(lonRadians) * Math.sin(latRadians);

v3d.z = radius * Math.sin(lonRadians) * Math.sin(latRadians);

v3d.y = -radius * Math.cos(latRadians);

return v3d

}

Here the latitude and longitude are converted into radians ( TO_RADIANS:Number = Math.PI / 180 ). Then the trigonomic functions are applied to the planet radius to return a vector3D x,y and z.

Due to limited bandwidth we had to omit Mercator projection itself from the post. Management.

I shall be expanding on MP in future posts.

For expediency here is a raw projection:

new Point(point.x, Math.log(Math.tan(0.25 * Math.PI + 0.5 * point.y)));

and the raw un-projection:

new Point(point.x, 2 * Math.atan(Math.pow(Math.E, point.y)) – 0.5 * Math.PI);

For more mathematical detail see: http://mathworld.wolfram.com/MercatorProjection.html

Hello!

I use the pure mathematical method to convert Mercator Sphere coordinates to latitude and longitude.

I needed it in JavaScript and implemented it in the following way:

function MercatorToLatLon(mercX, mercY) {

var rMajor = 6378137; //Equatorial Radius, WGS84

var shift = Math.PI * rMajor;

var lon = mercX / shift * 180.0;

var lat = mercY / shift * 180.0;

lat = 180 / Math.PI * (2 * Math.atan(Math.exp(lat * Math.PI / 180.0)) – Math.PI / 2.0);

return { ‘Lon’: lon, ‘Lat’: lat };

}

You can read it up in my blog (http://dotnetfollower.com/wordpress/2011/07/javascript-how-to-convert-mercator-sphere-coordinates-to-latitude-and-longitude/) at greater length.

Thanks!

Hi! this is quite interesting, a while ago I’ve been working in a project (PeopleMundi for playbook and android) and I was facing a situation similar to this, I found similar approaches like convertLatLonTo3DPos, and unfortunatelly they are very slow in term of performance… instead I end up using a provisional stencil buffer which works about 10x times faster by making the reverse 3d projection equation. have a look on it in action http://www.yoambulante.com/en/labs/FixPlanetEarth/ the playbook and android version runs at solid 30fps using this technique, I hope it helps, looking forward to have a beer at FOTB11, cheers!