Re: Worldmap mercator projection - Latitude to Y calculation. - last try
Paul <[email protected]> Sat, 24 Jan 2026 12:20:17 -0500
| Newsgroups | alt.comp.os.windows-xp,alt.windows7.general,alt.comp.os.windows-10 |
|---|---|
| Organization | A noiseless patient Spider |
| Message-ID | <[email protected]> |
On Sat, 1/24/2026 9:16 AM, J. P. Gilliver wrote:
> On 2026/1/24 8:28:44, R.Wieser wrote:
>> John,
>>
>>> There are three stages to this
>> ...
>>> 1. The mathematical conversion from latitude (and longitude) to the
>>> position on the plane map *relative to its centre*
>>
>> Nope. There is no rule that that center must be taken as the origin. Take
>> the formule Paul provided for instance. It returns a result in the range of
>> 0 to 1 .
>
> It makes it easier to understand what's going on if done in these three
> stages. And assuming we're talking of the Web Mercator image as shown at
> https://upload.wikimedia.org/wikipedia/commons/e/ec/Web_maps_Mercator_projection_SW.jpg,
> 0 degrees latitude and longitude _is_ at its centre.
>
>>
>>> 2. The _scaling_ required, depending on the size of the map image.
>>> Which again will depend on the size of the image (and may be different
>>> for X and Y).
>>
>> Worse : Mercator maps go from +85 to -85 latitude. That must be part of the
>
> (Or 85.051129.)
>
>> formule (I've found several maps where the south-pole is cut off, and a bit
>> of the north too. iow, useless without further information).
>
> No, just because the map is cut off at those latitudes, that figure does
> NOT have to appear in the formula. The poles _have_ to be cut off,
> otherwise the map would be infinitely tall, and very distorted at the poles.
>
>>
>> But again no. The result of the formule *is* the scaling (in your and my
>> usage ranging from +1 to -1, in Pauls formule case, from 0 to +1). You just
>> apply it on whatever size Mercator-style map you have handy.
>
> The above image, according to my browser, is 2068 by 2060 pixels.
>
>>
>>> I think _most_ of those contributing to this discussion know that,
>>> but have not been making it very clear which bits of their formula(e)
>>> do what.
>>
>> I do not need to know what all the parts of a car do, as long as I can drive
>> it. The same goes for these two formules. Latitude goes in, something I
>> can apply comes out.
>>
> True, if that really is all you want. As a scientist/engineer/just
> enquiring mind, I don't like to blindly use a formula without knowing
> what it does - or perhaps _why_.
>
> Yes, you can drive a car without knowing what each bit does. But knowing
> at least some of them will improve your longevity (wear and tear on the
> mechanisms), fuel economy, performance ... as you drive.
>
>>
>> Though the whole problem isn't that nobody understood what you said there,
>> but that nobody was willing to compare the (intermediate) results I posted
>> with what they got themselves, allowing me to locate where I made my
>> mistake(s?).
>
> That is indeed one of many problems.
>
> It would be good to see where the following points come out on e. g. the
> above image, using any formula (longitude given first):
> 0, 0
> +/- 180, 85
> +/-180, -85
> and some known place, such as London or New York.
>
>>
>> Not when I asked for it in my first post, and not when I rather explicitily
>> asked for it a few days back. :-(
>>
>> Regards,
>> Rudy Wieser
>>
>>
> I guess if you just want a formula that works, and _aren't_ bothered
> about the three steps - the mathematical conversion from angle to linear
> dimension, the scaling, and the offset - then we're very different
> minds. Which is of course fine; if we were all the same, it'd be a
> boring world.
>
As an engineer, I run a range of values through the equation, to get
a feel for both the rate-of-change of my (broken-or-working) function
as well as the absolute displacement. When we design logic blocks that
cannot be changed, in silicon, we waste a whole month on a test bench to
test all boundaries for mis-behavior. If trig functions were involved,
that just magnifies the amount of work, because then there are lots
of ways to break a thing.
The code I got, has a "clamp" function that sets any value outside 85.051129
back to exactly 85.051129 so that the function cannot produce any
infinities by accident.
#include <stdio.h>
#include <math.h>
/* gcc -lm -o merc.exe merc.c */
int main(void) {
double lat, lon;
const double PI = 3.14159265358979323846;
while (1) {
printf("\nEnter Latitude in degrees: ");
if (scanf("%lf", &lat) != 1) break;
printf("Enter Longitude in degrees: ");
if (scanf("%lf", &lon) != 1) break;
/* Clamp latitude to Mercator limits (approximately 85.05113 degrees) */
if (lat > 85.05113) lat = 85.05113;
if (lat < -85.05113) lat = -85.05113;
/* Convert longitude to X in [0,1] */
double x = (lon + 180.0) / 360.0;
/* Convert latitude to Y in [0,1] using Mercator projection */
/* the C log function is an ln() below, the C log10 function would be a log() */
double rad = lat * PI / 180.0;
double y = 0.5 - (log(tan(PI/4.0 + rad/2.0)) / (2.0 * PI));
printf("Your screen data point is at X=%f Y=%f\n", x, y);
}
return 0;
}
My conclusion from examining the pattern, is
(0,0) is on the upper left of the unit square
and Y is going down the page. That makes -80 near the
bottom.
Enter Latitude in degrees: -80
Enter Longitude in degrees: 0
Your screen data point is at X=0.500000 Y=0.887741
Enter Latitude in degrees: -50
Enter Longitude in degrees: 0
Your screen data point is at X=0.500000 Y=0.660855
Enter Latitude in degrees: -30
Enter Longitude in degrees: 0
Your screen data point is at X=0.500000 Y=0.587425
Enter Latitude in degrees: 0
Enter Longitude in degrees: 0
Your screen data point is at X=0.500000 Y=0.500000 <=== (0,0) is at (0.5,0.5)
Enter Latitude in degrees: 30
Enter Longitude in degrees: 0
Your screen data point is at X=0.500000 Y=0.412575
Enter Latitude in degrees: 50
Enter Longitude in degrees: 0
Your screen data point is at X=0.500000 Y=0.339145
Enter Latitude in degrees: 80
Enter Longitude in degrees: 0
Your screen data point is at X=0.500000 Y=0.112259
Paul