# Computing distance between two spherical Mercator points

I am computing the Mercator points according to the top answer at Convert latitude/longitude point to a pixels (x,y) on Mercator projection.

Now that I have two Mercator points on a spherical projection, how do I compute the distance between them?

For some background, what I am attempting to do is implement the answer at How to calculate the optimal zoom-level to display two or more points on a map and am stuck at step #2 where I need to compute the distance.

• You are going in the wrong direction. The best way to calculate distance between arbitrary points is via latitude and longitude. Mercator is useless for distance measurement. May 15, 2019 at 21:19
• Then, what is the right direction given my end goal? Considering I am working on a sphere, are Mercator coordinates really useless for the purpose I need them for? May 15, 2019 at 23:12
• Mercator is useless for distance - the poles are infinitely distant from the Equator. Greenland is not the same area as South America (it's 1/8 the size). Every measurement that leaves the Equator is corrupted by the sine of the latitude. The are sufficient libraries to calculate geodetic distance on the spheroid which are fast enough for most purposes. May 15, 2019 at 23:21
• I do not believe any of those issues matter for my purpose or situation. So, if you know the answer to the question asked, I would appreciate learning what it is. May 16, 2019 at 2:03
• You're better off using the helper functions than writing your own, but for meaningless distance calculation, Pythagoras is still the way to go. May 16, 2019 at 2:21

As mentioned by @mkkenedy in a comment on the question: once you've converted your latitude/longitude coordinates to Mercator coordinates, they are on a Euclidean plane, where you can use the Pythagorean theorem.

Specifically, if your Mercator coordinates are `(x1, y1)` and `(x2, y2)`, the distance is:

``````sqrt((x2-x1)^2 + (y2-y1)^2)
``````
• With the caveat that the computed distance will be wrong (compared to a geodesic calculation or distance in an projection where the poles are not infinitely far from the Equator). Aug 15, 2021 at 23:53

# Concepts: general considerations regarding length measurements in GIS

Before seaching for a way "how to measure correct distances", the conceptual question "how do we define 'correct' disctance" should be answered. It includes, of course, Earth's shape - but which model of it? A sphere (very rough approximation), an ellipsoid of rotation (closer to the reality), the Geoid (quite close, but difficult to handle). What about topography? Should mountains and valleys be considered or not? Or is the distance to be meant for an airplane? Then the vertical distance (cruising altitude - of up to 12.000 meters above ground for commercial flights) should also be considered?

As you see, even conceptually, it's not so easy to say which one is the "real" distance. For practical use, probably the best (in the sense of returning good results while still easy to handle) is using ellipsoidal distance - reducing the Earth's shape to an ellipsoid of rotation (as is the case with most projections used in GIS) and then calculating the distance on this surface.

# Measuring ellipsoidal length with QGIS

If you use a software like QGIS, you can make ellipsoidal length-measurements that return more or less accurate real-world distances even for Mercator-projections.

See the following example for the distance from Berlin to Rome: when set to `Ellipsoidal`, the distance measurement returns 1183.64 km - this is more or less accurate (based on the points projected to the WGS84-ellipsoid). When you check the `Cartesian` checkbox, the distance shows as 1752.9 km: quite a difference! This last value reflect the heavy distortion of the Mercator projection.

Calculation of ellipsoidal distances in QGIS is also possible using the expression `\$length` for a line as this respects the current project’s ellipsoid setting and distance unit settings. First create a line that connects the two points, then apply `\$length`. 