球坐标转换的疑惑
我正在尝试将球面坐标(即 GPS 设备的纬度和经度)转换为笛卡尔坐标。我正在关注这个简单的转换极坐标转换方程。
然后我应用欧几里德距离计算两点之间的距离,但我找到的值并不总是与我可以使用 半正矢公式。特别是,我注意到,给定不同的经度但相同的纬度会导致两种算法计算出相同的距离,而具有相同的经度并更改纬度会带来不同的值。
这是我正在使用的 C 代码:
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;
}
我是否做错了什么,或者背后有一些我没有看到的数学(也许可以在 Mathoverflow 板上问这个?)。 更新没有必要跨越木板,因为有人正确指出这种转换对于计算两点之间的精确距离没有意义(两极之间的距离为零)。所以我将其重新表述为:为什么在小纬度增量(0.0001 对应于或多或少 10 米)处,距离似乎与半正弦公式 (20-25%) 如此不同?
更新2: 正如 Oli Charlesworth 指出的那样,不考虑 z 轴使得这种转换成为一种不介意南北差异的投影。这也是我指出的增量差异的原因。事实上,在正确的变换中,z 与纬度相关,如果你考虑它,然后计算两点之间的欧几里德距离(现在在 3d 空间中),纬度和经度都会产生良好的近似值对于小三角洲。 例如,对于纬度,误差约为 1.41 米。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从这个来看,没有保留距离的二维地图投影。计算点的二维投影的距离是没有用的。
From this, there is no 2D map projection where distance is preserved. Calculating the distance from the 2D projection of points is useless.