使用 boost::units 计算地理坐标
using boost::units to calculate geo coordinates
我正在尝试对来自[计算 Latitude/Longitude 点之间的距离、方位角等][1] 的公式进行类型安全的 C++ 实现。不幸的是,我 运行 陷入了 C++ 类型的错误,这些错误将实际问题掩埋在无休止的模板扩展中。我觉得我真的很接近:
#include <iostream>
#include <boost/units/systems/angle/degrees.hpp>
#include <boost/units/systems/si/length.hpp>
#include <boost/units/systems/si/plane_angle.hpp>
#include <boost/units/systems/si/prefixes.hpp>
#include <boost/units/cmath.hpp>
#include <boost/units/io.hpp>
using namespace boost::units;
using namespace boost::units::si;
struct Position
{
public:
using angle_type = quantity<degree::plane_angle>;
Position(const angle_type &latitude, const angle_type &longitude) : m_latitude{latitude}, m_longitude{longitude} {}
angle_type m_latitude;
angle_type m_longitude;
};
const quantity<si::length> earth_radius{6371. * kilo * meters};
Position move(const Position &start, quantity<degree::plane_angle> bearing, quantity<si::length> distance)
{
const auto angular_distance = distance / earth_radius;
auto latitude2 = asin(sin(start.m_latitude) * cos(angular_distance) +
cos(start.m_latitude) * sin(angular_distance) * cos(bearing));
auto longitude2 = start.m_longitude + atan2(sin(bearing) * sin(angular_distance) * cos(start.m_latitude),
cos(angular_distance) - sin(start.m_latitude) * sin(latitude2));
return Position(latitude2, longitude2);
}
int main(int, char **)
{
return 0;
}
错误信息:
/home/jpo/src/units/main.cpp:30:41: error: no match for ‘operator+’
(operand types are ‘const angle_type’ and ‘double’)
30 | auto longitude2 = start.m_longitude + atan2(sin(bearing) * sin(angular_distance) * cos(start.m_latitude),
| ~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| | |
| | double
| const angle_type
问题是,
const auto angular_distance = distance / earth_radius;
本该是角的时候是无量纲的。将其更改为
quantity<degree::plane_angle> angular_distance{(distance / earth_radius).value() * radians};
编译代码。
我正在尝试对来自[计算 Latitude/Longitude 点之间的距离、方位角等][1] 的公式进行类型安全的 C++ 实现。不幸的是,我 运行 陷入了 C++ 类型的错误,这些错误将实际问题掩埋在无休止的模板扩展中。我觉得我真的很接近:
#include <iostream>
#include <boost/units/systems/angle/degrees.hpp>
#include <boost/units/systems/si/length.hpp>
#include <boost/units/systems/si/plane_angle.hpp>
#include <boost/units/systems/si/prefixes.hpp>
#include <boost/units/cmath.hpp>
#include <boost/units/io.hpp>
using namespace boost::units;
using namespace boost::units::si;
struct Position
{
public:
using angle_type = quantity<degree::plane_angle>;
Position(const angle_type &latitude, const angle_type &longitude) : m_latitude{latitude}, m_longitude{longitude} {}
angle_type m_latitude;
angle_type m_longitude;
};
const quantity<si::length> earth_radius{6371. * kilo * meters};
Position move(const Position &start, quantity<degree::plane_angle> bearing, quantity<si::length> distance)
{
const auto angular_distance = distance / earth_radius;
auto latitude2 = asin(sin(start.m_latitude) * cos(angular_distance) +
cos(start.m_latitude) * sin(angular_distance) * cos(bearing));
auto longitude2 = start.m_longitude + atan2(sin(bearing) * sin(angular_distance) * cos(start.m_latitude),
cos(angular_distance) - sin(start.m_latitude) * sin(latitude2));
return Position(latitude2, longitude2);
}
int main(int, char **)
{
return 0;
}
错误信息:
/home/jpo/src/units/main.cpp:30:41: error: no match for ‘operator+’
(operand types are ‘const angle_type’ and ‘double’)
30 | auto longitude2 = start.m_longitude + atan2(sin(bearing) * sin(angular_distance) * cos(start.m_latitude),
| ~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| | |
| | double
| const angle_type
问题是,
const auto angular_distance = distance / earth_radius;
本该是角的时候是无量纲的。将其更改为
quantity<degree::plane_angle> angular_distance{(distance / earth_radius).value() * radians};
编译代码。