2

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.

Taras
  • 32,823
  • 4
  • 66
  • 137
ericg
  • 121
  • 1
  • 3
  • 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. – Vince May 15 '19 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? – ericg May 15 '19 at 23:12
  • 3
    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. – Vince May 15 '19 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. – ericg May 16 '19 at 02:03
  • 1
    You're better off using the helper functions than writing your own, but for meaningless distance calculation, Pythagoras is still the way to go. – Vince May 16 '19 at 02:21
  • What helper functions? Wasn't sure if Pythagoras applied to the situation. But, if that is the answer to the question, please provide it as the actual answer and I will mark it as such. – ericg May 16 '19 at 02:23
  • The helper functions documented in the other answers of your linked questions. – Vince May 16 '19 at 02:44
  • I have looked at those, but they do not seem to be of much help. I would appreciate it if you could be more detailed in how to use Pythagoras to answer the question. Thank you. – ericg May 16 '19 at 12:03
  • I do not understand your problem enough to answer it. You seem to be making a mountain out of the molehill of calculating delta-X and delta-Y. – Vince May 16 '19 at 12:18
  • ok. I will continue to try to figure out how to compute the distance between two spherical mercator points. Perhaps someone else will know the answer. – ericg May 16 '19 at 12:22
  • Use the Pythagorean theorem. Once you have Mercator coordinates, they're in a plane, so you can use planar equations. Vince was assuming you wanted "real world" distance. Mercator is terrible for finding "real world" distances. – mkennedy May 16 '19 at 17:24

2 Answers2

2

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)
tinlyx
  • 11,057
  • 18
  • 71
  • 119
digartner
  • 21
  • 2
  • 1
    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). – Vince Aug 15 '21 at 23:53
1

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 rotational ellipsoid/spheroid (closer to the reality), the Geoid (quite close, but difficult to handle). What about topography/relief? 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 - or just the vertical distance?

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 a rotational ellipsoid (as is the case with most projections used in GIS) and then calculating the distance on the surface of this three dimensional model (spheroid).

See also here for the concept of ground distance, used in survey.

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, almost 50% longer! This last value reflects the heavy distortion of the Mercator projection and thus returns no useful measurement.

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.

enter image description here

Babel
  • 71,072
  • 14
  • 78
  • 208