我需要在地图上绘制一个地理投影圆。例如,我希望根据纬度和经度来定义中心,并以米为单位定义其半径。我正在使用KDE Marble
. 在 API 中有一个函数drawEllipse
,它将中心作为(纬度,经度)以及椭圆的宽度和高度。
GeoPainter::drawEllipse(const GeoDataCoordinates& center, qreal width, qreal height, bool isGeoProjected = false)
对于地理投影椭圆,宽度和高度被认为是度数。但是我需要它们以米为单位。度和米之间没有简单的转换,因为它取决于地球中心的位置。
我需要将圆的半径(以米为单位)转换为从地球中心指向中心的向量的一对位移度。
我也在使用 boost geometry 来做其他的几何计算。是否有任何功能boost-geometry
可以执行此转换?
更新我
我首先尝试有两个 GeoDataCoordinates 一个是中心,另一个是外围。我预计他们的纬度和经度之间的差异将适用于该drawEllipse
功能。
painter->drawEllipse(_center, std::abs(_border.longitude(GeoDataCoordinates::Degree)-_center.longitude(GeoDataCoordinates::Degree)), std::abs(_border.latitude(GeoDataCoordinates::Degree)-_center.latitude(GeoDataCoordinates::Degree)), true);
然而,它产生的椭圆比我预期的要小。应该在圆周上的边界点超出了椭圆。
更新二
然后我尝试使用维基百科上的中心角公式
double angular_distance = acos(
(sin(_center.latitude(GeoDataCoordinates::Radian))*sin(_border.latitude(GeoDataCoordinates::Radian)))
+(cos(_center.latitude(GeoDataCoordinates::Radian))*cos(_border.latitude(GeoDataCoordinates::Radian)))
*(double)cos(std::abs(_border.longitude(GeoDataCoordinates::Radian)-_center.longitude(GeoDataCoordinates::Radian)))
);
painter->drawEllipse(_center, stmr::internal::rad2deg(angular_distance), stmr::internal::rad2deg(angular_distance), true);
结果与上一个没有太大区别。然而,这一次椭圆比前一个略大。
更新三
圆弧与球体大圆周长之间的距离比用于计算@ttemple 答案中的角位移
painter->drawEllipse(_center, 2*(distance/earthRadiusKm)* 180.0/3.14159, 2*(distance/earthRadiusKm)* 180.0/3.14159, true);
产生一个正确的椭圆。
2.0
因此,我将角度与UPDATE II代码相乘,这也产生了类似的(如UPDATE III)结果。
更新四
但问题drawEllipse
在于它实际上在缩小状态下绘制了一个点少得多的多边形。有时它看起来像一个正方形。
因此,在椭圆圆周上绘制更多点是一个更好的选择,这样围栏在缩小视图中也看起来像一个圆圈。评论中发布的KDE 论坛链接也是如此。
GeoDataLineString ellipse;
qreal lon = 0.0;
qreal lat = 0.0;
int precision = 180;
for ( int i = 0; i < precision; ++i){
qreal t = 1.0 - 2.0 * (qreal)(i) / (qreal)(precision);
lat = center.latitude(GeoDataCoordinates::Degree) + 0.5 * radius * sqrt(1.0 - t * t);
lon = center.longitude(GeoDataCoordinates::Degree) + 0.5 * radius * t;
ellipse << GeoDataCoordinates(lon, lat, 0.0, GeoDataCoordinates::Degree);
}
for ( int i = 0; i < precision; ++i){
qreal t = 2.0 * (qreal)(i) / (qreal)(precision) - 1.0;
lat = center.latitude(GeoDataCoordinates::Degree) - 0.5 * radius * sqrt(1.0 - t * t);
lon = center.longitude(GeoDataCoordinates::Degree) + 0.5 * radius * t;
ellipse << GeoDataCoordinates(lon, lat, 0.0, GeoDataCoordinates::Degree);
}
painter->drawPolyline(ellipse);
然而,它绘制了一个半径比输入大得多的圆。我认为那里发布的片段不完整。我在代码中忽略了一些未使用的条件块。代码还特别在注释中提到围栏的半径必须以公里为单位。我这样做了。我也不明白这背后的数学。它不会在片段中的任何地方使用地球半径。可能对这个代码稍加修正会产生一个更好的椭圆。数学看起来像一些产生一半椭圆的参数曲线。但是没有提到方程。
这也仅适用于 lat 和 lon 均为正的第一象限