1

I'm trying to convert spherical coordinates (namely latitude and longitude from a GPS device) into Cartesian coordinates. I'm following this simple conversion derived from the polar coordinates conversion equations.

Then I'm calculating the distance between the two point applying the euclidean distance but the value I'm finding is not always the same as the distance I can calculate using the haversine formula. In particular I'm noticing that given different longitudes but same latitudes leads to the same distances computed by the two algorithms, whereas having the same longitude and changing the latitude carries different values.

Here is the C code I am using:

double ComputeDistance(double lat1,double lon1, double lat2, double lon2)
{
    
    double dlon, dlat, a, c;
    dlon = lon2- lon1;
    dlat = lat2 - lat1;
    a = pow(sin(dlat/2),2) + cos(lat1) * cos(lat2) * pow(sin(dlon/2),2);
    c = 2 * atan2(sqrt(a), sqrt(1-a));
    return 6378140 * c;  /* 6378140 is the radius of the Earth in meters*/

    
}
int main (int argc, const char * argv[]) {

    double lat1 = 41.788251028649575;
    double lat2 = 41.788251028649575;
    double long1 = -118.1457209154;
    double long2 = -118.1407209154;//just ~10 meters distant
    
    lat1 = DEGREES_TO_RADIANS(lat1);
    lat2 = DEGREES_TO_RADIANS(lat2);
    long1 = DEGREES_TO_RADIANS(long1);
    long2 = DEGREES_TO_RADIANS(long2);
    
    //transform in cartesian coordinates
    double x = 6378140 * cos(lat1) * cos(long1);
    double y = 6378140 * cos(lat1) * sin(long1);
    
    double x2 = 6378140 * cos(lat2) * cos(long2);
    double y2 = 6378140 * cos(lat2) * sin(long2);

    
    double dist = sqrt(pow(x2 - x, 2) + pow(y2 - y, 2));
    printf("DIST %lf\n", dist);
    printf("NDIST %lf\n", ComputeDistance(lat1, long1, lat2, long2));

    return 0;
}

Am I doing something incorrect or there is some math behind it I am not seeing (and maybe ask this on Mathoverflow boards?). UPDATE It is not necessary to cross boards, as someone correctly pointed this conversion is not meaningful for computing the exact distance between two points (the distance between the two poles is zero). So I am reformulating it as: why at small deltas (0.0001 which corresponds to 10 mts more or less) of latitudes the distance appear to be so different from the haversine formula (20-25%)?

UPDATE 2: As Oli Charlesworth pointed out, not considering the z axis makes this conversion a projection that does not mind the north-south difference. This is also the cause of the difference in deltas I was pointing out. In fact, in the correct transform the z is related to the latitude and if you consider it , then compute the euclidean distance between the two points (in a 3d space now), both latitude and longitude will lead to a good approximation for small deltas. For Example for a degree of latitude the error is ~ 1.41 meters.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
rano
  • 5,616
  • 4
  • 40
  • 66
  • 3
    "Then I'm calculating the distance between the two point applying the euclidean distance but the value I'm finding is not always the same as the distance I can calculate using the haversine formula." The euclidean distance is the straight line distance b/w the points while the "haversine formula" calculates the distance along a great circle on the sphere. Why should these ever give the same result? – Jonathan Aug 24 '10 at 22:31
  • I'm calculating the distance between the projected point in the cartesian system – rano Aug 24 '10 at 22:41
  • @rano: which isn't the same as the distance along the surface of the Earth. – Oliver Charlesworth Aug 24 '10 at 22:48
  • @Oli Charlesworth, well sometimes it seems it is :P, it depends on the projection you are doing,which kind of projection is the simple conversion I am doing ? – rano Aug 24 '10 at 22:49
  • 1
    under the simple projection onto the xy-plane, the north pole and south pole have no "distance" between them. i think the likelihood of that projection being what you want is ~nil – Jonathan Aug 24 '10 at 22:50
  • @rano: I'm not sure this is a meaninful projection. You're simply doing a change of coordinates, and neglecting to take `z` into account. – Oliver Charlesworth Aug 24 '10 at 22:54
  • @Oli Charleworth `z` can be simply taken in account as shown in the link, the fact that this is not-so-meaningful is true, since as @Jonathan said, some distances like that between poles is nihil. – rano Aug 24 '10 at 22:58
  • @rano: Yes, `z` can be taken into account, but then it's simply a coordinate transform, and not a projection. I think you missed Jonathan's point; you've completely lost all information about the `z` direction (i.e. distances parallel to the polar axis of the earth). – Oliver Charlesworth Aug 24 '10 at 22:59
  • @Oli yes, transform is more appropriate as term – rano Aug 24 '10 at 23:05
  • @rano: "transform" would be appropriate if you ended up with three dimensions. You have a projection, but it's a useless one! – Oliver Charlesworth Aug 24 '10 at 23:06
  • @Oli I simply have not written it, I dare to say it is not so useful for distances but not always useless – rano Aug 24 '10 at 23:09
  • @rano: I think you're still missing the point. If you calculated `z`, you would have a *transform*. You don't, so you have a *projection*. However, this projection is losing all north-south information. At any rate, this will never give you the same "distance" as that calculated along the surface of the Earth (for arbitrary points), no matter which projection you chose. – Oliver Charlesworth Aug 24 '10 at 23:14
  • @Oli the difference between 'projection' and 'transform' is clear now. The hope of having 'the same' distance as calculated on Earth is long lost – rano Aug 24 '10 at 23:20
  • 1
    @rano: In answer to why it appears to work for longtitude but not latitude. You are relying on small-angle approximations (if you try longtitude deltas of, say, a few degrees, you'll see them diverge). Latitude doesn't work at all because you're not preserving north-south distance properly. – Oliver Charlesworth Aug 24 '10 at 23:25
  • @Oli thanks, +1 for the comment – rano Aug 25 '10 at 06:42

1 Answers1

1

From this, there is no 2D map projection where distance is preserved. Calculating the distance from the 2D projection of points is useless.

Forrest Voight
  • 2,249
  • 16
  • 21
  • 1
    I guess you are wrong, there are some projections, as stated in that page that preserve some distances. "...equidistant projection such as the Azimuthal equidistant projection. There are also projections (Maurer, Close) where true distances from two points are preserved" http://en.wikipedia.org/wiki/Azimuthal_equidistant_projection – rano Aug 24 '10 at 22:42
  • @rano: Yes, so no matter what the projection, the distance between two *arbitrary* points will not be preserved. – Oliver Charlesworth Aug 24 '10 at 22:46
  • as answered above, I know that, I'm asking why longitude seems to preserve it with that classical conversion – rano Aug 24 '10 at 22:50