Title: Lat/Lng to Web Mercator Projection Issues
Tags: qt;geolocation
Question: I've got some simple code in a Qt application which converts from a latitude/longitude coordinate to a web mercator x/y position:
// Radius of the Earth in metres
const double EARTH_RADIUS = 20037508.34;
// Convert from a LL to a QPointF
QPointF GeoPoint75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0toMercator() {
double x = this-&gt;longitude() * EARTH_RADIUS / 180.0;
double y = log(tan((90.0 + this-&gt;latitude()) * PI / 360.0)) / (PI / 180.0);
y = y * EARTH_RADIUS / 180.0;
return QPointF(x, y);
// Convert from a QPointF to a LL
GeoPoint GeoPoint75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0romMercator(const QPointF &amp;pt) {
double lon = (pt.x() / EARTH_RADIUS) * 180.0;
double lat = (pt.y() / EARTH_RADIUS) * 180.0;
lat = 180.0 / PI * (2 * atan(exp(lat * PI / 180.0)) - PI / 2.0);
return GeoPoint(lat, lon);
I'm wanting to get the geographic position of a number of objects which are in metres distance away from a geographic origin, however either my lack of understanding or source code are not correct.
Consider the following:
GeoPoint pt1(54.253230,  -3.006460);
QPointF m1 = pt1.toMercator();
qDebug() << m1;
// QPointF(-334678,7.21826e+06)
// Now I want to add a distance onto the mercator coordinates, i.e. 50 metres
m1.rx() += 50.0;
qDebug() << m1;
// QPointF(-334628,7.21826e+06)
// Take this back to a LL
GeoPoint pt1a = GeoPoint75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0romMercator(m1);
qDebug() << pt1a.toString();
// "54.25323°, -3.00601°"
If I plot the two LL coordinates into Google Earth, they are not 50m apart as expected, they are about 29.3m apart.
I'm perplexed!
Comment: Distance between projected points is not same as distance between geographical points. It looks like you add 50m to projected point and expect it will still 50m on spheroid? I don't think it's a valid assumption.

Comment: It's about different coordinate systems. You can read more here: https://en.wikipedia.org/wiki/Geographical_distance or other sources - there are plenty of them.

Comment: @vahancho thanks for your comment. Given that I'm adding 50m to the projected point and then "reprojecting" it back to lat/long, that should take into account the 50m difference? Is that not correct? If that isn't correct then what does the web mercator projection actually represent? Also, apologies I'm not understanding all this...



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2017:00:09/28/17', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<qt><geolocation>', 'Title': 'Lat/Lng to Web Mercator Projection Issues', 'ViewCount': 287, 'id': 46470866, 'user_AboutMe': '', 'user_AccountId': 2687092.0, 'user_DisplayName': 'weblar83', 'user_Reputation': 651.0, 'user_Views': 65.0, 'user_id': 2321263.0}}