No exact solution exists

There is no isometric map from the sphere to the plane. When you convert lat/lon coordinates from the sphere to x/y coordinates in the plane, you cannot hope that all lengths will be preserved by this operation. You have to accept some kind of deformation. Many different map projections do exist, which can achieve different compromises between preservations of lengths, angles and areas. For smallish parts of earth's surface, transverse Mercator is quite common. You might have heard about UTM. But there are many more.

The formulas you quote compute x/y/z, i.e. a point in 3D space. But even there you'd not get correct distances automatically. The shortest distance between two points on the surface of the sphere would go through that sphere, whereas distances on the earth are mostly geodesic lengths following the surface. So they will be longer.

Approximation for small areas

If the part of the surface of the earth which you want to draw is relatively small, then you can use a very simple approximation. You can simply use the horizontal axis x to denote longitude Ξ», the vertical axis y to denote latitude Ο†. The ratio between these should not be 1:1, though. Instead you should use cos(Ο†0) as the aspect ratio, where Ο†0 denotes a latitude close to the center of your map. Furthermore, to convert from angles (measured in radians) to lengths, you multiply by the radius of the earth (which in this model is assumed to be a sphere).

  • x = r Ξ» cos(Ο†0)
  • y = r Ο†

This is simple equirectangular projection. In most cases, you'll be able to compute cos(Ο†0) only once, which makes subsequent computations of large numbers of points really cheap.

Answer from MvG on Stack Overflow
Top answer
1 of 6
79

No exact solution exists

There is no isometric map from the sphere to the plane. When you convert lat/lon coordinates from the sphere to x/y coordinates in the plane, you cannot hope that all lengths will be preserved by this operation. You have to accept some kind of deformation. Many different map projections do exist, which can achieve different compromises between preservations of lengths, angles and areas. For smallish parts of earth's surface, transverse Mercator is quite common. You might have heard about UTM. But there are many more.

The formulas you quote compute x/y/z, i.e. a point in 3D space. But even there you'd not get correct distances automatically. The shortest distance between two points on the surface of the sphere would go through that sphere, whereas distances on the earth are mostly geodesic lengths following the surface. So they will be longer.

Approximation for small areas

If the part of the surface of the earth which you want to draw is relatively small, then you can use a very simple approximation. You can simply use the horizontal axis x to denote longitude Ξ», the vertical axis y to denote latitude Ο†. The ratio between these should not be 1:1, though. Instead you should use cos(Ο†0) as the aspect ratio, where Ο†0 denotes a latitude close to the center of your map. Furthermore, to convert from angles (measured in radians) to lengths, you multiply by the radius of the earth (which in this model is assumed to be a sphere).

  • x = r Ξ» cos(Ο†0)
  • y = r Ο†

This is simple equirectangular projection. In most cases, you'll be able to compute cos(Ο†0) only once, which makes subsequent computations of large numbers of points really cheap.

2 of 6
22

I want to share with you how I managed the problem. I've used the equirectangular projection just like @MvG said, but this method gives you X and Y positions related to the globe (or the entire map), this means that you get global positions. In my case, I wanted to convert coordinates in a small area (about 500m square), so I related the projection point to another 2 points, getting the global positions and relating to local (on screen) positions, just like this:

First, I choose 2 points (top-left and bottom-right) around the area where I want to project, just like this picture:

Once I have the global reference area in lat and lng, I do the same for screen positions. The objects containing this data are shown below.

//top-left reference point
var p0 = {
    scrX: 23.69,        // Minimum X position on screen
    scrY: -0.5,         // Minimum Y position on screen
    lat: -22.814895,    // Latitude
    lng: -47.072892     // Longitude
}

//bottom-right reference point
var p1 = {
    scrX: 276,          // Maximum X position on screen
    scrY: 178.9,        // Maximum Y position on screen
    lat: -22.816419,    // Latitude
    lng: -47.070563     // Longitude
}

var radius = 6371;      //Earth Radius in Km

//## Now I can calculate the global X and Y for each reference point ##\\

// This function converts lat and lng coordinates to GLOBAL X and Y positions
function latlngToGlobalXY(lat, lng){
    //Calculates x based on cos of average of the latitudes
    let x = radius*lng*Math.cos((p0.lat + p1.lat)/2);
    //Calculates y based on latitude
    let y = radius*lat;
    return {x: x, y: y}
}

// Calculate global X and Y for top-left reference point
p0.pos = latlngToGlobalXY(p0.lat, p0.lng);
// Calculate global X and Y for bottom-right reference point
p1.pos = latlngToGlobalXY(p1.lat, p1.lng);

/*
* This gives me the X and Y in relation to map for the 2 reference points.
* Now we have the global AND screen areas and then we can relate both for the projection point.
*/

// This function converts lat and lng coordinates to SCREEN X and Y positions
function latlngToScreenXY(lat, lng){
    //Calculate global X and Y for projection point
    let pos = latlngToGlobalXY(lat, lng);
    //Calculate the percentage of Global X position in relation to total global width
    pos.perX = ((pos.x-p0.pos.x)/(p1.pos.x - p0.pos.x));
    //Calculate the percentage of Global Y position in relation to total global height
    pos.perY = ((pos.y-p0.pos.y)/(p1.pos.y - p0.pos.y));

    //Returns the screen position based on reference points
    return {
        x: p0.scrX + (p1.scrX - p0.scrX)*pos.perX,
        y: p0.scrY + (p1.scrY - p0.scrY)*pos.perY
    }
}

//# The usage is like this #\\

var pos = latlngToScreenXY(-22.815319, -47.071718);
$point = $("#point-to-project");
$point.css("left", pos.x+"em");
$point.css("top", pos.y+"em");

As you can see, I made this in javascript, but the calculations can be translated to any language.

P.S. I'm applying the converted positions to an HTML element whose id is "point-to-project". To use this piece of code on your project, you shall create this element (styled as position absolute) or change the "usage" block.

🌐
NOAA
ngs.noaa.gov β€Ί NCAT
NGS Coordinate Conversion and Transformation Tool (NCAT)
NGS Coordinate Conversion and ... easily convert between different coordinate systems and/or transform between different reference frames and/or datums, in a single step. For coordinate conversion, NCAT allows conversion between lat/long/height, SPC, UTM, XYZ, and USNG systems. NCAT currently uses NADCON* to perform three-dimensional (latitude, longitude, ellipsoid ...
Discussions

Converting latitude and longitude to XY coordinates
I am doing a JavaScript application that requires the movement of a certain element in a real world map, in frames. For each frame, I have the following positions in latitude and longitude for the More on gis.stackexchange.com
🌐 gis.stackexchange.com
March 11, 2020
excel - Formula for X & Y Co-ordinates to Latitude and Longitude? - Stack Overflow
Do you want to convert decimal degrees (DD) into DMS? ... To answer my own question, the formulas are explained well here intmath.com/vectors/3d-earth-geometry.php so x and y (and z) are just straight-line distances measured with cartesian coordinates using a plane through the equator. I think the question does make sense now - if you can project down onto this plane knowing the latitude and longitude ... More on stackoverflow.com
🌐 stackoverflow.com
map projections - Conversion of coordinates (longitude ; latitude) to (X;Y) - Mathematics Stack Exchange
We have an old mapping system we are needing to convert some data to and from. We need to convert from Lng/lat to XY and from XY to Lng/Lat. We can convert from Lng/Lat to XY Using the following... More on math.stackexchange.com
🌐 math.stackexchange.com
June 4, 2019
python - How to convert latitude and longitude to x and y grid system? - Stack Overflow
I have file with latitude and longitude values, that i want to convert the x and y in km using Python. I want to measure the distance from each point. for instance I make the first points of latitu... More on stackoverflow.com
🌐 stackoverflow.com
🌐
EarthPoint
earthpoint.us β€Ί convert.aspx
Convert Coordinates
Convert Latitude/Longitude to UTM, UPS, MGRS, GARS, Maidenhead, GEOREF, State Plane, and back.
🌐
Tool-Online
tool-online.com β€Ί en β€Ί coordinate-converter.php
Free online coordinates converter
Following users' requests, i added the option to convert to and from Geocentric cartesian coordinates (X, Y, Z). Here's how: - Long, Lat, h -> X, Y, Z: Select WGS84 left and right: WGS84_XYZ (geocentric) under 'International' menu; - X, Y, Z -> Long, Lat, h: Select left WGS84_XYZ (geocentric) ...
🌐
Medium
medium.com β€Ί @manishsingh7163 β€Ί converting-from-latitude-longitude-to-cartesian-coordinates-9ddf30841c45
Converting from (latitude,longitude) to Cartesian coordinates | by Manish Singh | Medium
March 16, 2024 - In the Cartesian coordinate system: - The x-axis originates at (0,0) for longitude and latitude, meaning longitude 0 aligns with the equator. - The y-axis starts at (0,90), denoting latitude.
🌐
Sciencing
sciencing.com β€Ί convert-xy-coordinates-longitude-latitude-8449009.html
How To Convert XY Coordinates To Longitude And Latitude - Sciencing
March 13, 2025 - Assume the value 6371 kilometers to the variable R, which is the approximate radius of Earth. Calculate latitude and longitude using the formulas latitude = asin (Z/R) and longitude = atan2 (Y,X).
Find elsewhere
Top answer
1 of 1
4

You need the bearing, which is the angle from point 1 point 2 on the surface of the globe. You can get that from turf.js, a powerful GIS calculation library.

Then you need a distance formula, to get the distance on the surface of the globe between the two points. Again, turf.js has a distance function.

Once you have the bearing and the distance, you've basically turned this into a 2D cartesian coordinates problem. Now you need to pick your units of x and y. What is a unit of x / y? 1 meter? 1 km? Lets say we that 1 unit of x or y is equal to 1 meter. If you find that point 2 is 35m away from point 1, with a bearing of 90 degrees, you know that the xy coordinates of point 2 would be (0,35), assuming `(0,0) for point 1. If point 2 is 1.4km away from point 1, with a bearing of 32 degrees, point 2's xy coords can be calculated with some simple trig: ( 1400 * cos(32) , 1400 * sin(32) ).

So a js function might look like this:

function getCoordsOfNextPoint( firstPoint, nextPoint, prevXY = {x: 0, y: 0} ) {

  const bearing = turf.bearing(firstPoint, nextPoint);
  const distance = turf.distance(firstPoint, nextPoint, {units: 'kilometers'});

  const xy = {
    x:  prevXY.x + distance * 1000 * Math.cos(bearing * Math.PI / 180),
    y:  prevXY.y + distance * 1000 * Math.sin(bearing * Math.PI / 180)
  }

  return xy

}

const myFirstPoint = [-75.343, 39.984]
const mySecondPoint = [-75.534, 39.123]
const secondPointXYCoords = getCoordsOfNextPoint(myFirstPoint, mySecondPoint)

You may want to tweak the units or parameters. Also watch out for the conventions on the units of what the bearing function returns, that can trip you up. (I know its tripped me up in the past). Turf is super powerful, definitely worth looking into.

Edit: stringing points together

To get from point 0 to point 1, you won't need the extra parameter prevXY. But every point after that will take the point returned by the function used for the 2 points before it. For example:

// Assuming x0y0 = { x: 0, y: 0}

const x1y1 = secondPointXYCoords = getCoordsOfNextPoint(myFirstPoint, mySecondPoint)
const x2y2 = secondPointXYCoords = getCoordsOfNextPoint(myFirstPoint, mySecondPoint, x1y1)
const x3y3 = vsecondPointXYCoords = getCoordsOfNextPoint(myFirstPoint, mySecondPoint, x2y2)

And you could write a loop to read your latlng points from an array and write to another array of xy points, which will basically translate your latlng points to xy points.

Hopefully that gets your started.

Top answer
1 of 2
3

You have so many parentheses in your latitude formula that it’s hard to see what goes with what.

Let $\phi = \mathrm{LatY} \times \frac\pi{180},$ that is, if LatY is the latitude in degrees then $\phi$ is the latitude in radians. Let $h$ be mapHeight and let $w$ be mapWidth. Then your formula for $y$ becomes this in mathematical notation: $$ y = \frac h2 - \frac{w \ln\left(\tan\left(\frac\pi4 + \frac\phi2\right)\right) }{2 \pi} $$

This is similar to the formula found at http://mathworld.wolfram.com/MercatorProjection.html except for the scaling and translation factors (which you want in order to fit the output on the display).

Solving the equation for $\phi$ (still in radians), $$ \phi = 2 \arctan\left(\exp\left(\frac{2\pi}{w} \left(\frac h2 - y \right) \right)\right) - \frac\pi2. $$ Multiply by $\frac{180}{\pi}$ to get the answer in degrees.

Again it’s hard to be sure due to the profusion of parentheses, but the attempted formula seems to be equivalent to the mathematical equation

$$ \mathrm{lat} = \frac{\exp\left(-\frac{\left(Y - \frac h2 \right)}{w} \times 2 \pi \right) - \tan\left(\frac\pi4\right) \times 2}{\pi / 180}, $$

which is clearly quite different. The fact that $\tan\left(\frac\pi4\right)$ (which is just equal to $1$) occurs in there should be a red flag indicating that something was done in the wrong order.

2 of 2
1

Can't spot the issue with the final equation but reverse engineering the y = equation will work. Doing that you end up with the following:

lat = ((Math.arctan(Math.exp((((mapHeight / 2) -y) / mapWidth) * (2 * Math.PI))) - (Math.PI / 4))*2)/(Math.PI/180)
Top answer
1 of 6
13

The following gets you pretty close (answer in km). If you need to be better than this, you have to work harder at the math - for example by following some of the links given.

import math
dx = (lon1-lon2)*40000*math.cos((lat1+lat2)*math.pi/360)/360
dy = (lat1-lat2)*40000/360

Variable names should be pretty obvious. This gives you

dx = 66.299 km (your link gives 66.577)
dy = 2.222 km (link gives 2.225)

Once you pick coordinates (for example, lon1, lat1) as your origin, it should be easy to see how to compute all the other XY coordinates.

Note - the factor 40,000 is the circumference of the earth in km (measured across the poles). This gets you close. If you look at the source of the link you provided (you have to dig around a bit to find the javascript which is in a separate file) you find that they use a more complex equation:

function METERS_DEGLON(x)
{  
   with (Math)
   {
      var d2r=DEG_TO_RADIANS(x);
      return((111415.13 * cos(d2r))- (94.55 * cos(3.0*d2r)) + (0.12 * cos(5.0*d2r)));
   }
}

function METERS_DEGLAT(x)
{
   with (Math)
   {
      var d2r=DEG_TO_RADIANS(x);
      return(111132.09 - (566.05 * cos(2.0*d2r))+ (1.20 * cos(4.0*d2r)) - (0.002 * cos(6.0*d2r)));
   }
}

It looks to me like they are actually taking account of the fact that the earth is not exactly a sphere... but even so when you are making the assumption you can treat a bit of the earth as a plane you are going to have some errors. I'm sure with their formulas the errors are smaller...

2 of 6
4

UTM projections are in meters. So you could use something like the utm lib at this link:

https://pypi.python.org/pypi/utm

Googling python lat lon to UTM will point to several options.

UTM zones are 6 degrees of longitude wide and start from 0 at the prime meridian. The origin of each UTM zone is on the equator (x-axis) with the y-axis at the western most degree of longitude. This makes the grid positive to the north and east. You could calculate your distance from these results. Values are most accurate in the middle of the UTM zone.

You also should know what datum your original lat lon values are based on and use the same datum in your conversion.

🌐
Reddit
reddit.com β€Ί r/gis β€Ί converting latlong to a local xy coordinate system
r/gis on Reddit: Converting latlong to a local XY coordinate system
August 9, 2022 -

Hello all.
I'm a programmer who has just about zero experience with GIS. I am currently working on a project that contains a local map of a fairly small area.
This map needs to be populated with points of interest relevant to the customer.
Because of some licensing issue I can't make use of any of the common map api's that you would normally use for this. I will need to convert Lat/Long to the XY coordinate system of the unreal engine.

To do this I have been given a map image, the Lat/Long coord's for the topleft and bottomright corner of the image, and a list of lat/long coords for the points of interest.

Top left lat/long: 52.04176, 4.66225
Bottom right lat/long: 51.99592, 4.76227
Top left in Unreal: x=0.0, y=0.0
Bottom right in Unreal: x=800.0 y=600.0

I thought this would be easily done by just using the range, but I kinda forgot you have to account for the curvature of the earth so it doesn't work.

How do I convert this?

[edit]Used Solution

For flat screen projection you want to convert your coords to something called "Web Mercator"
Because of float rounding in Unreal I need to do the conversions before importing. I used excel to convert it.

X = [Earth's radius scaled to your map(6371 will do)] * long * cos(lat for center of your map)
Y = [Earth's radius scaled to your map] * lat * -1
Figure out the range of your coordinates and use that to normalize the import to 0-1, multiply by size of your map to get unreal coordinates.

[/edit]

Top answer
1 of 1
1

As has already been noted in comments, the real solution to your problem is to research map projections, choose an appropriate projection, and use that projection to convert latitude and longitude to points on your display. However, for small areas of the earth's surface, a simple "flat earth" approximation might be sufficient.

The major problem with your code is that the length of a degree of latitude is not the same as the length of a degree of longitude, except at the equator. If you move one degree north, you move the same distance regardless of your location: 1/360th of the length of a great circle. But if you move one degree west, you have moved a shorter distance depending on your latitude, because you are moving on a "small circle" instead of a great circle. If you are near the north pole then the radius of this small circle is much less than the radius of a great circle. The only exception is if you are at the equator, and then the "small circle" is actually a great circle. So how to compensate?

If your latitude is $\phi$, where $\phi = 0^\circ$ at the equator and $\phi = 90^\circ$ at the north pole, then the radius of the small circle through your location is $R \cos \phi$, where $R$ is the radius of the earth. The radius of a great circle is $R$. If you move one degree north then you have moved a distance of $2 \pi R / 360$, whereas if you move one degree west you have moved a distance of $(2 \pi R \cos \phi)/360$. In your code, a pixel corresponds to some measure of distance, so "degrees per pixel" is inversely proportional to distance; but the distance per degree is different in the X and Y directions, as explained above. So to compensate, you should divide the degrees per pixel in the X direction by $\cos \phi$. Note that the cosine function in most programming languages requires an angle in radians, so you may need to convert degrees to radians before computing the cosine.

🌐
MathWorks
mathworks.com β€Ί automated driving toolbox β€Ί automated driving algorithms β€Ί geographic and hd maps
latlon2local - Convert geographic coordinates to local Cartesian coordinates - MATLAB
alt = 10; % 10 meters is an approximate altitude in Boston, MA origin = [d.latitude(1), d.longitude(1), alt]; Convert the route from geographic coordinates to Cartesian coordinates, x and y.
🌐
Latitude and Longitude Finder
latlong.net β€Ί home β€Ί geo tools
Lat Long to UTM Converter
This is an effective and fast online Lat Long to UTM converter. It can be used to make the stated conversions at any time and any place. Type the latitude and longitude values to convert from lat long coordinate system into UTM (Universal Transverse Mercator) coordinate system.
Top answer
1 of 4
12

Update

If utm coordinates are suitable for your purposes, then note that in lieu of pyproj, the utm package is now available and is simpler to use. Just be aware of lng, lat ordering vs. easting, northing ordering for the respective from_latlon and to_latlon functions, per the docs.

The below conversion example would become:

import utm
x, y = utm.from_latlon(input_lat, input_lon)

Original

Use pyproj for converting your lng, lat pairs to a projected coordinate system. In other words you need to convert from your geographic coordinate system (most likely EPSG code 4326) to a local projected coordinate system, e.g. a local UTM zone or regional system such as the British National Grid (EPSG code 27700).

import pyproj as proj

# setup your projections
crs_wgs = proj.Proj(init='epsg:4326') # assuming you're using WGS84 geographic
crs_bng = proj.Proj(init='epsg:27700') # use a locally appropriate projected CRS

# then cast your geographic coordinate pair to the projected system
x, y = proj.transform(crs_wgs, crs_bng, input_lon, input_lat)

Note that pyproj.transform() also works on numpy arrays, so you can therefore transform your lon, lat arrays to x, y arrays. You can then use numpy's built-in array methods to normalise your values.

For example:

import numpy as np
x = (x - x.min()) / (x.max() - x.min())
y = (y - y.min()) / (y.max() - y.min())

However, if you are using sklearn already, then you may as well use sklearn.preprocessing.normalize.

2 of 4
4

It depends on what metric you need to preserve. One obvious approach is to project it to some appropriate flat coordinate system. To do this, you can use the pyproj library. Then, simply rescale your coordinates to be in the range [0, 1]. However, this doesn't perfectly preserve spatial relationships because longitude and latitude are on a sphere (or an ellipsoid or geoid), and x,y coordinates are on a flat surface. You should read about different map projections to see what you may be losing. A good reference is Map Projections: A Working Manual. If your positions only cover a small patch of the Earth, the errors may be acceptable to you.

A second approach is to convert the longitude and latitude to (x, y, z) coordinates in 3d space. If you approximate the earth's surface as a sphere, then the formulae should be

x = cos(lat) * cos(lon)
y = cos(lat) * sin(lon)
z = sin(lat)

where lon and lat are in radians. In this case however, z=-1 is the south pole, z=1 is the north pole, x=1 and x=-1 are the prime meridian, and the dateline, and y=1 and y=-1 are 90 degrees East and 90 degrees West. Again, you can rescale so that coordinates are [0,1] rather than [-1,1].

🌐
EPSG.io
epsg.io β€Ί transform
Transform coordinates - GPS online converter
Transform your coordinates online easily with epsg.io