Re: Worldmap mercator projection - Latitude to Y calculation.
Paul <[email protected]> Sat, 17 Jan 2026 12:11:23 -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/17/2026 3:11 AM, R.Wieser wrote:
> Paul
>
>> Your coordinates for NYC, where did you get those coords ???
>
> If you mean 39, -77 its probably from me.
>
> Looking back in my docs I see I have an image of a globus wit the lat/long
> lines on it, mentioning 39, -77. The only problem is that it mentions
> Washington DC, and not New York. I've got no idea how I mixed those two up.
> My apologies.
>
>> 40 42 46 -74 0 22 New York City value (Wikipedia)
>>
>> 40.71277778 -74.00611111 DMS to D via
>> https://www.latlong.net/degrees-minutes-seconds-to-decimal-degrees
>>
>> X=0.294427 Y=0.375981 merc.exe (the CoPilot program)
>
> Is there any chance you can (re)post the used formule (just to be sure I'm
> using the correct one) and some intermediate results (especially the ones
> fed into the tan() an log() functions) so I can verify what the formule is
> doing here (and spot my mistake(s)) ?
>
> Regards,
> Rudy Wieser
>
The CoPilot response shows its equation here. And this is what
it used to write the C code for me.
[Picture] Use Download Original for best result (CoPilot output)
https://i.postimg.cc/SQLZbfvY/mercator-equation.gif
Whereas the Wikipedia reference for the same thing is here.
The USGS equation hides the 2PI part inside its R and it neglects
to include coordinate correction for plotting purposes. The USGS
form of the equation shows the "shape of the transform" and is
not the equation a practitioner needs to implement. The Copilot
equation delivers a unit square, with the center of the
map presumably being 0,0 for lat,long. The unit square itself
has a different origin, like the upper left corner. In any case,
using your calibrated eyeball, you will be able to figure out
what additional math is require to fit your plotting scheme.
https://en.wikipedia.org/wiki/Mercator_projection
Mathematics [half way down the page or so...]
A simple expression for the spherical form of the Mercator projection is:[26]
[26] https://pubs.usgs.gov/pp/1453/report.pdf (PDF page 228, stamped as "page 218")
The USGS document on its page, does not note the
agreed 85.05113 degree cutoff for latitudes (so the "globe"
is not too messy). The cutoff is encoded in the C code that CoPilot gave me.
***************************************************************************
#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 (plus/minus 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(), the C log10 function is an 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;
}
***************************************************************************
.\merc
Enter Latitude in degrees: 40.71277778
Enter Longitude in degrees: -74.00611111
Your screen data point is at X=0.294427 Y=0.375981
And then I scaled up my graphic to 10000 x 10000
and plotted (2944, 3760). I wrote a piece of code
I call pbmplot.exe and it starts with an empty
square and adds the datapoints I extracted from the
Panoply output. The pbm graphics file format is
dead simple, and the graphics data (one hundred million
ASCII characters) is output with one printf("%s")
type statement :-) The format doesn't even need
<cr><lf> during the data phase, which I find both
amusing and practical. I bet Notepad would not
like that file :-) But GIMP opened that file just fine.
Paul