https://github.com/Dongvdong/gnss_ecef_enu_txt_yaml
1 从txt读取数据按行拆解
测试文件
config.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | #==============# # Camera Model # #==============# Camera.name: EH2022left monocular Camera.setup: monocular Camera.model: perspective Camera.fx: 1220 Camera.fy: 1220 Camera.cx: 960 Camera.cy: 540 Camera.k1: -0 Camera.k2: 0 Camera.p1: 0 Camera.p2: 0 Camera.k3: -0 Camera.k4: 0 Camera.fps: 10 Camera.cols: 1920 Camera.rows: 1080 Camera.color_order: RGB Initial.lat: 34.0342694493 Initial.lon: 108.755911045 Initial.alt: 587.800293 Feature.max_num_keypoints: 3000 Feature.scale_factor: 1.2 Feature.num_levels: 8 PangolinViewer.keyframe_size: 0.07 PangolinViewer.keyframe_line_width: 2 PangolinViewer.graph_line_width: 1 PangolinViewer.point_size: 2 PangolinViewer.camera_size: 0.08 PangolinViewer.camera_line_width: 3 PangolinViewer.viewpoint_x: 0 PangolinViewer.viewpoint_y: -0.65 PangolinViewer.viewpoint_z: -1.9 PangolinViewer.viewpoint_f: 400 Marker.Num: 0 vAcc: 1.0 hAcc: 1.0 Fixed.altitude_flag: 0 Fixed.altitude: 400.0 Save.newmap: 1 Save.data: 1 op.is_Second_Optimize: 0 op.Second_Optimize_Th: 0 op.Remove_Kayframe_Th: 6.0 op.Global_Optimize_Th: 1.0 Loop_Th: 80.0 Relocalize_Th: 80.0 Relocalize_KF_Th: 3.0 V_Instant_Th: 200.0 Tracking_CF_Th: 10.0 |
gps.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 1453132356.600000 34.0342694493 108.755911045 587.800293 1453132357.960000 34.0344383177 108.755909682 587.673778 1453132358.520000 34.0345050891 108.755905295 587.568409 1453132359.160000 34.0345823584 108.755898876 587.587611 1453132359.560000 34.0346306268 108.755897244 587.612678 1453132360.200000 34.0347072511 108.755895617 587.583863 1453132360.840000 34.0347838932 108.755894644 587.438686 1453132361.400000 34.034850341 108.755894968 587.336082 1453132362.040000 34.0349259665 108.755895105 587.321752 1453132362.600000 34.0349939502 108.755895669 587.300844 1453132363.160000 34.0350612885 108.755896463 587.323285 1453132363.720000 34.0351295704 108.755895922 587.301819 1453132364.280000 34.0351972108 108.75589547 587.372351 1453132364.840000 34.0352654922 108.755893457 587.384214 1453132365.480000 34.0353438263 108.755889651 587.441598 1453132366.360000 34.0354513461 108.755882747 587.483984 1453132367.160000 34.0355485419 108.755871215 587.549042 1453132367.880000 34.0356326538 108.755860899 587.705846 1453132368.520000 34.0357092702 108.755849687 587.778205 1453132369.400000 34.0358125899 108.755839936 587.83467 1453132370.280000 34.0359174818 108.755836712 588.006536 1453132371.000000 34.0360013516 108.755839913 588.162609 1453132371.880000 34.0361035224 108.755849286 588.3736 |
CMakeLists.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | cmake_minimum_required(VERSION 3.10) project(ReadTextFile) # 设置 C++ 标准为 C++11 set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED True) # 包含GeographicLib的头文件路径 include_directories( "/usr/include/GeographicLib" ) # 添加可执行文件,并链接主程序文件和自定义类的头文件 add_executable(node main.cpp) set(YAML_CPP_LIBRARIES ${CMAKE_SOURCE_DIR}/src/libyaml-cpp.a) target_link_libraries(node ${YAML_CPP_LIBRARIES} Geographic) |
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | #include <iostream> #include <fstream> #include <sstream> #include <vector> #include <string> #include<iomanip> #include "src/API_GNSS_ENU_TXT_YAML.h" using namespace std; int main() { //string path_="/home/r9000k/v2_project/data/NWPU/"; string path_= "../config/" ; string path_config=path_+ "FHY_config.yaml" ; // 初始点 string path_GNSS=path_+ "FHY_gps.txt" ; // 原始数据 // 1 获取参考点 gnss_data gnss_data_int0(-1,-1,-1,-1); Get_GNSS_INTI_YAML(path_config,gnss_data_int0); std::cout << fixed << setprecision(10)<< endl; std::cout << "原点 纬度: " << gnss_data_int0.lat << endl; std::cout << "原点 经度: " << gnss_data_int0.lon << endl; std::cout << "原点 高度: " << gnss_data_int0.high << endl; // 2 当前点的经纬度和高度,作为局部坐标系的原点 double origin_latitude = gnss_data_int0.lat; // 纬度 double origin_longitude = gnss_data_int0.lon; // 经度 double origin_height = gnss_data_int0.high; // 高度 // 转化为enu,并设置原点 GeographicLib::LocalCartesian geoConverter; geoConverter.Reset(origin_latitude, origin_longitude, origin_height); // 转化为ecef,使用WGS84椭球模型 GeographicLib::Geocentric wgs84(GeographicLib::Constants::WGS84_a(), GeographicLib::Constants::WGS84_f()); // 6378137 298.257223563LL //GeographicLib::Geocentric cgcs2000(6378137.0, 1.0 / 298.257222101); //3 获取数据点 GNSS_TextFileReader reader(path_GNSS, ' ' ); // 读取路径 分隔符号 std::vector<gnss_data> gnss_Lists ; if (reader.readFile()) { //reader.printData(); gnss_Lists = reader.get_gnss_List(); for ( int i=0;i<gnss_Lists.size();i++){ gnss_Lists[i].Set_orinGnss(gnss_data_int0.lat,gnss_data_int0.lon,gnss_data_int0.high); // 设置原始GNSS点 double target_latitude = gnss_Lists[i].lat; double target_longitude = gnss_Lists[i].lon; double target_height = gnss_Lists[i].high; // gnss转化为enu double x, y, z; geoConverter.Forward(target_latitude, target_longitude, target_height, x, y, z); gnss_Lists[i].x=x; gnss_Lists[i].y=y; gnss_Lists[i].z=z; // WGS84 gnss转化为ecef wgs84.Forward(target_latitude, target_longitude, target_height, x, y, z); gnss_Lists[i].ecef_x=x; gnss_Lists[i].ecef_y=y; gnss_Lists[i].ecef_z=z; gnss_data gnss_List_i=gnss_Lists[i]; cout << fixed << setprecision(10)<< endl; cout<< "编号 " << i << " 时间戳 " << gnss_List_i. time << " \n纬度 " << gnss_List_i.lat << " 经度 " << gnss_List_i.lon << " 高度 " << gnss_List_i.high << " \nenu-x " << gnss_List_i.x << " enu-y " << gnss_List_i.y << " enu-z " << gnss_List_i.z << " \necef_x " << gnss_List_i.ecef_x << " ecef_y " << gnss_List_i.ecef_y << " ecef_z " << gnss_List_i.ecef_z << endl; } } return 0; } |
API_GNSS_ENU_TXT_YAML.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | #ifndef API_GNSS_ENU_TXT_YAML #define API_GNSS_ENU_TXT_YAML #include <iostream> #include <fstream> #include <sstream> #include <vector> #include <string> #include<iomanip> #include <yaml-cpp/yaml.h> #include <GeographicLib/LocalCartesian.hpp> using namespace std; class gnss_data { public : // 原始数据 double time ; double lat; double lon; double high; // ENU坐标GNSS的原点 double lat0; double lon0; double high0; // 在ENU坐标下 double x; double y; double z; double ecef_x; double ecef_y; double ecef_z; public : // 初始化 赋予原始数据 gnss_data( double time_, double lat_, double lon_, double high_): time (time_), lat(lat_),lon(lon_),high(high_) {} bool Set_Gnss( double time_, double lat_, double lon_, double high_){ time =time_; lat=lat_; lon=lon_; high=high_; }; bool Set_orinGnss( double lat0_, double lon0_, double high0_){ lat0=lat0_; lon0=lon0_; high0=high0_; } bool Set_ENU( double x_, double y_, double z_){ x=x_; y=y_; z=z_; } }; class GNSS_TextFileReader { public : std::string filename; char delimiter; std::vector<std::vector<std::string>> data; // 二维向量,存储每一行拆分后的数据 std::vector<gnss_data> gnss_List; public : GNSS_TextFileReader( const std::string &filename, char delimiter) : filename(filename), delimiter(delimiter) {} bool readFile() { std::ifstream file(filename); if (!file.is_open()) { std::cerr << "Error opening file " << filename << std::endl; return false ; } std::string line; while (std::getline(file, line)) { std::stringstream ss(line); std::string token; std::vector<std::string> row; while (std::getline(ss, token, delimiter)) { // delimiter 分割 row.push_back(token); } data.push_back(row); // 保存string数据double gnss_data gnss_data_i( stod(row[0]),stod(row[1]),stod(row[2]),stod(row[3])); //保存double数据 gnss_List.push_back(gnss_data_i); } file.close(); return true ; } void printData() { // for (const auto &row : data) { // for (const auto &col : row) { // std::cout << col << " "; // } // std::cout << std::endl; // } //for (const auto &gnss_List_i : gnss_List) { for ( int i=0;i<gnss_List.size();i++){ gnss_data gnss_List_i=gnss_List[i]; cout<< "编号 " << i << " 时间戳 " << gnss_List_i. time << " 纬度 " << gnss_List_i.lat << " 经度 " << gnss_List_i.lon<< " 高度 " << gnss_List_i.high << fixed << setprecision(10)<< endl; } } const std::vector<std::vector<std::string>>& getData() const { return data; } const std::vector<gnss_data>& get_gnss_List() const { return gnss_List; } }; int Get_GNSS_INTI_YAML(std::string read_path,gnss_data &gnss_data_int0) { try { // 读取YAML文件 YAML::Node config = YAML::LoadFile(read_path); // 访问YAML中的数据 std::string lat0 = config[ "Initial.lat" ].as<std::string>(); std::string lon0 = config[ "Initial.lon" ].as<std::string>(); std::string alt0 = config[ "Initial.alt" ].as<std::string>(); double time0=0.0; gnss_data_int0.Set_Gnss(time0,stod(lat0),stod(lon0),stod(alt0)); // // 打印读取的数据 std::cout << "函数内部" << std::endl; //std::cout << fixed << setprecision(10)<< endl; std::cout << "原点 纬度: " << lat0 << std::endl; std::cout << "原点 经度: " << lon0 << std::endl; std::cout << "原点 高度: " << alt0 << std::endl; } catch ( const YAML::Exception& e) { std::cerr << "YAML Exception: " << e.what() << std::endl; return 1; } return 0; } // int main() { // //string path_="/home/r9000k/v2_project/data/NWPU/"; // string path_="../config/"; // string path_config=path_+"FHY_config.yaml";// 初始点 // string path_GNSS=path_+"FHY_gps.txt";// 原始数据 // // 1 获取参考点 // gnss_data gnss_data_int0(-1,-1,-1,-1); // Get_GNSS_INTI_YAML(path_config,gnss_data_int0); // std::cout << fixed << setprecision(10)<< endl; // std::cout << "原点 纬度: " << gnss_data_int0.lat << endl; // std::cout << "原点 经度: " << gnss_data_int0.lon << endl; // std::cout << "原点 高度: " << gnss_data_int0.high << endl; // // 2 当前点的经纬度和高度,作为局部坐标系的原点 // double origin_latitude = gnss_data_int0.lat; // 纬度 // double origin_longitude = gnss_data_int0.lon; // 经度 // double origin_height = gnss_data_int0.high; // 高度 // // 转化为enu,并设置原点 // GeographicLib::LocalCartesian geoConverter; // geoConverter.Reset(origin_latitude, origin_longitude, origin_height); // // 转化为ecef,使用WGS84椭球模型 // GeographicLib::Geocentric wgs84(GeographicLib::Constants::WGS84_a(), GeographicLib::Constants::WGS84_f());// 6378137 298.257223563LL // //GeographicLib::Geocentric cgcs2000(6378137.0, 1.0 / 298.257222101); // //3 获取数据点 // GNSS_TextFileReader reader(path_GNSS, ' '); // 读取路径 分隔符号 // std::vector<gnss_data> gnss_Lists ; // if (reader.readFile()) { // //reader.printData(); // gnss_Lists = reader.get_gnss_List(); // for(int i=0;i<gnss_Lists.size();i++){ // gnss_Lists[i].Set_orinGnss(gnss_data_int0.lat,gnss_data_int0.lon,gnss_data_int0.high);// 设置原始GNSS点 // double target_latitude = gnss_Lists[i].lat; // double target_longitude = gnss_Lists[i].lon; // double target_height = gnss_Lists[i].high; // // gnss转化为enu // double x, y, z; // geoConverter.Forward(target_latitude, target_longitude, target_height, x, y, z); // gnss_Lists[i].x=x; // gnss_Lists[i].y=y; // gnss_Lists[i].z=z; // // WGS84 gnss转化为ecef // wgs84.Forward(target_latitude, target_longitude, target_height, x, y, z); // gnss_Lists[i].ecef_x=x; // gnss_Lists[i].ecef_y=y; // gnss_Lists[i].ecef_z=z; // gnss_data gnss_List_i=gnss_Lists[i]; // cout << fixed << setprecision(10)<< endl; // cout<< "编号 " << i // << " 时间戳 "<< gnss_List_i.time // << " \n纬度 " << gnss_List_i.lat // << " 经度 " << gnss_List_i.lon // << " 高度 "<< gnss_List_i.high // << " \nenu-x " << gnss_List_i.x // << " enu-y " << gnss_List_i.y // << " enu-z "<< gnss_List_i.z // << " \necef_x " << gnss_List_i.ecef_x // << " ecef_y " << gnss_List_i.ecef_y // << " ecef_z "<< gnss_List_i.ecef_z // << endl; // } // } // return 0; // } #endif |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
2020-08-02 python3.7安装
2019-08-02 25 读取配置文件
2019-08-02 25树莓派硬件信息
2019-08-02 24 目标识别MobileNet
2019-08-02 23 人脸识别
2019-08-02 24 Home Assistant