The following calculations have been sourced from the Navit Project source code, released under the GNU General Public License version 2.

To convert latitude and longitude into UTM X and Y coordinates, we can simply perform two calculations (lat is latitude and lon is longitude):

x = lon × 6371000.0 × pi ÷ 180
y = ln(tan((pi ÷ 4) + (lat × pi ÷ 360))) × 6371000.0

In C / C++, this can be written as

#ifndef M_PI
M_PI = 3.141592653589793238462643
#endif

#ifndef M_PI_4
M_PI_4 = (M_PI / 4.0)
#endif

x = lon * 6371000.0 * M_PI / 180;
y =log(tan(M_PI_4 + (lat * M_PI / 360))) * 6371000.0;

  1. maryam says:

    I can not understand the y. Could you explain or put a reference?
    Thank you very much.

  2. Tim says:

    Hi maryam,

    The Y value corresponds to the number of meters above/below the equator. The X value corresponds to the east-west position. This is explained pretty well in this wikipedia article.

  3. Daoud says:

    Greetings,

    Kindly note that I really interested on this subject and I greatly want to get help from you.

    first, I think you used “lon” & “lat” in opposite way (lon should be used in calculating y and vice-versa).
    Second, You should specify more brackets in order to give priorities for the operations that should be calculated before others. you should include every multiplication/division operation in separate brackets () so the user and the program would understand how to calculate it.

    Third, I tested these 2 methods using MS Excel 2007 and it came with very wrong answers.

    Please it is recommended you should update these information…

    Thanks in advance.

  4. Glen says:

    This method works well enough. It does not correct for flattening at the poles, but for most tings, it will suffice.

    If you are trying to do this in a spreadsheet you have to remember that in C/C++, angular units are in radians. The provided code adjust for that, BUT, In a spreadheet you may have problems if you are using degrees as the angular measurement unit. Also, In C.C++, the log() function returns the natural log, not log base 10. This too will be different in a spreadsheet if you are not careful.