Function for finding latitude given the Y position on a Mercator world map?
August 17, 2010 1:20 AM   Subscribe

Function for finding latitude given the Y position on a Mercator world map?

Looking for something like

function y2lat(y, mapHeight) { [some magic] return lat }

I found this: function y2lat(a) { return 180/Math.PI * (2 * Math.atan(Math.exp(a*Math.PI/180)) - Math.PI/2); } on this page but I can't see how it works without knowing the map height, unless the input value is scaled to something?
posted by gwint to Computers & Internet (8 answers total)
 
A Mercator map's height is not bound - the poles are at +-infinity. You'd need to know already a latitude at a given y on the map and use that to figure out how much you need to scale it by.
posted by edd at 1:31 AM on August 17, 2010


Response by poster: I see. So if I could determine, say, the y value for the equator, how would I then scale for other y values since the projection for latitude isn't linear?
posted by gwint at 6:53 AM on August 17, 2010


Well the equator doesn't help - I think it would be reasonable to assume the equator is in the centre of the image. You need somewhere off the equator (the further the better, I imagine).

Lets say we count the equator as y=0. When y=0, y2lat(a) above returns 0, so that's all fine.

If we knew that y=+400 was at latitude 45 degrees:
lat2y(45) = 50.5
and therefore we need to use y2lat(y*50.5/400) to convert.

That's from the middle of the picture, so if the picture was 1000 pixels high, the top and bottom would be at +-500 (give or take a pixel for the equator), and would be at latitude
y2lat(500*50.5/400) = +- 53.2 degrees, for example.
posted by edd at 7:56 AM on August 17, 2010


Response by poster: Hmm, I'm still a little confused. Here's the map I'm thinking about using. The height is 477. If we say that the equator is roughly at 238, and London is at 127 (=51 deg 30 min N), and Capetown is at 302 (=33 deg 55 min S) how do I use that information to find additional latitudes at arbitrary points, say New York at y=153? I thought you can't use a simple scaling multiplier since the distance between latitudes grows at you go towards the poles?
posted by gwint at 8:22 AM on August 17, 2010


Best answer: If London is at 127 pixels from the top, that's 111 pixels above the equator.
For that location, lat2y(51.5) = 60.28
So we need to use y2lat(d*60.28/111) where d is the distance above the equator in pixels.

For example, Capetown has a d of -64, and y2lat(-64*60.28/111) = -32.8 - just under 33 deg S - which is a little bit off where it should be but we're probably not pixel perfect.

New York is 82 pixels above the equator, which suggests a latitude of y2lat(82*60.28/111)=40.63 deg N - again not perfect but it's within a pixel or two.

The thing we need to do is use the right scaling multiplier before using y2lat, rather than scaling y2lat after.
posted by edd at 8:42 AM on August 17, 2010


This MathWorld page has the necessary formulas.
posted by Pork-Chop Express at 11:33 AM on August 17, 2010


Sorry, just ignore me. I overlooked the link in your question.
posted by Pork-Chop Express at 1:24 PM on August 17, 2010


Response by poster: Many thanks for helping me through edd.
posted by gwint at 1:30 PM on August 17, 2010


« Older I want to start a cult, but for music   |   What's up with my son? Newer »
This thread is closed to new comments.