Archive for the ‘Mercator’ Category

Deconstructing Mercator, Part 2

March 1, 2008

Summarizing last time:  Google’s WKT includes SPHEROID[”…”, 6378137, 0], meaning the earth is a sphere with a radius of 6378137 meters. 6378137 * pi = 20037508, the half-circumference of the earth in meters, and the Mercator max range.  Solve the Mercator equations so that max X = max Y, and you’ll find that happens at +/- 85.051 degrees latitude.

Notice this 85.051 degrees is a “magic number” in Mercator; it dictates a perfect Mercator square for the moon as well as it does the Earth.  Our WKT says Google’s Mercator maps are based on meters.  Yet Wikipedia’s math imagines a world with a radius of 1.  Worse, Charlie rightly points out that Google’s APIs also imagines multiple Mercator worlds with various pixel dimensions!

In short, there is only one square Mercator projection of a sphere.  Datum is the question.  Your local WMS/IMS may need to know the earth is 6378137 meters in radius for conversion from other projections.  One the other hand, it’d be happy with 6378.137 km, 31795.5 furlongs, or 6.7418e-10 lightyears, as long as you can explain the relationship.  {You can tweak your WKT SPHEROID all day, as long as you equally modify your DATUM’s UNIT values.}

More to the point, no matter what your datum, there is only one defacto tile standard: 256×256 tiles, a 256×256 base map, n zoom levels, and a 2×2 zoom between levels.  With tiles, the earth is 128*2^n/pi pixels in radius, end of story.  Any Mercator datum can be turned into another simply by multiplying by the ratio of their diameters.  Making a 6378137 m radius earth fit into 256×256 pixels is one such transformation of datum.

The fun thing, then, is coming up with a better datum.  Google’s solution of pixel datums is certainly elegant.  In many circumstances, however, we are trying to fit tiles to geographic coordinates.  Here, a 360×360 unit Mercator Earth is a nice, clean option. 

Remember that the Mercator projection does not touch longitude except for datum conversion.  Hence, with a 360×360 Mercator Earth, longitude is equal to X already.  Mercator Y can be derived from latitude using the simple { Y = log( tan( lat ) + sec( lat ) ) } equation.

And that’s Mercator.   Fin.

Deconstructing Mercator

March 1, 2008

These days, everybody uses mercator tilesetsCharlie and Morten both put forth excellent explanation of Mercator for us.  These two are both smart, smart guys.  I will attempt to give an unsmart explanation.

If you were to take a globe and count its degrees, you’d see the earth is 360 degrees around, and 180 degrees from pole to pole.  Easy!!  A circle’s circumference is 360 degrees, and also its radius * 2 * pi.  By that logic, 180 degrees = pi * r, and the earth is pi*r tall, and 2*pi*r wide.

Knowing the earth’s radius, then, we can calculate the circumference of the earth (2*pi*r).  The trick is, not everybody is willing to say that the earth is a sphere.  People who know fancy words are more inclined to describe our earth as an oblate spheroid — a somewhat flattened sphere. 

Morten’s WKTs inform us that WGS84 uses a spheroid of SPHEROID["...",6378137, 298.257223563] while Google uses a spheroid of SPHEROID["...", 6378137, 0].  It’s that third parameter thats interesting:  its the flattening factor.  It means WGS84 knows the earth is an oblate spheroid, while Google treats the earth as a perfect sphere.  6378137 meters is the semi-major axes of WGS84’s oblate world.  In Google’s world view, this number is simply the radius of the earth.

Sweet.  We know the radius of the Google’s earth.  We know the half-circumference of the earth, too, pi*r:  20037508.342789 meters.  Check the validMercator range for the Google projection and you’ll see a pattern:  [-20037508.3427892, -20037508.3427892] to [20037508.3427892, 20037508.3427892].  Morten is simply telling us that Google uses the circumference of the earth in meters!!

 But WAIIIIT.   The earth is pi*r tall, and 2*pi*r wide.  Half of those numbers should be half that!!  And they would be, in an unprojected/plate carre world; but Google uses a Mercator projection.  Referring to Wikipedia and our valid range, we can see that Mercator is happy to leave longitude alone.  But also from Wikipedia, “A Mercator map can never fully show the polar areas, since linear scale becomes infinitely high at the poles.”

 WHAT?  Yes, Mercator project has an infinite Y range.  We can’t have infinite bounds!! So how does Google make it look like a square?  Simple:  they chop off the poles. 

For simplicity’s sake, put your mental trig calculator into degree mode, or pretend the world has a radius of 1.  Take Wikipedia’s Mercator projection  { Y = log( tan( lat ) + sec( lat ) ) },  and solve with Y = 180.  You’ll arrive at a value of 85.051 degrees … which should again be familiar from Morten’s blog.  85.051 degrees is a magic latitude that gives Google their square earth.

So there you go:  A rough explanation of Mercator, and an only slightly better explanation of some of the WKT magic number behind hit.  Stay tuned for part two.