地理計算庫 GeographicLib 極簡入門
- 時間:
20210412
文章目錄
- 1. 檔案
- 2. 安裝
- 3. 專案集成
- 4. 極簡的例子
1. 檔案
- GeographicLib - 官方檔案
- GeographicLib::LocalCartesian - 官方檔案
2. 安裝
-
克隆原始碼并編譯安裝 GeographicLib , (碼云鏡像更快,已有較多人轉儲,不用自己匯入,如需自己匯入,參考gitee檔案),
git clone https://gitee.com/masonqin/geographiclib.git cd geographiclib mkdir build && cd build cmake .. #無報錯 make #無報錯 sudo make install #無報錯
3. 專案集成
-
撰寫 CMakeLists.txt,非常簡單的老三句,
find_package (GeographicLib REQUIRED) include_directories(${GeographicLib_INCLUDE_DIRS}) target_link_libraries(<可執行程式> ${GeographicLib_LIBRARIES}) -
代碼集成,最常用的功能,使用區域坐標系轉換,從 經緯高 到 ENU ,
#include <GeographicLib/LocalCartesian.hpp> //包含頭檔案 //經緯度原點初始化 GeographicLib::LocalCartesian geo_converter; geo_converter.Reset(latitude, longitude, altitude); //經緯度轉ENU geo_converter.Forward(latitude, longitude, altitude, local_E, local_N, local_U); -
初始化原點用了 Reset 函式,Forward 用于將經緯高轉換成米制坐標[E,N,U] ,倆函式都是呼叫 geo_converter 物件的成員函式,geo_converter 就是 GeographicLib::LocalCartesian 的實體,
- GeographicLib::LocalCartesian 簡介,官方檔案
- 功能就是把橢球體下的地理坐標系坐標轉為ENU區域系下的坐標,
- Reset 函式的作用是重置原點,LocalCartesian建構式是默認在(0,0,0)也就是地心,
- Forward (lat, lon, alt, x, y, z)函式就是把經緯高轉換為ENU,前三個傳入,后三個傳出,
- GeographicLib::LocalCartesian 簡介,官方檔案
4. 極簡的例子
-
新建 geographiclib_demo 檔案夾,在里面新建 CMakeLists.txt 和 main.cpp,
-
main.cpp ,經緯度資料轉ENU米制坐標,資料檔案為lonlat.csv,格式為**[緯度,經度,高度]**,
#include <iostream> #include <vector> #include <string> #include <fstream> #include <sstream> #include <GeographicLib/LocalCartesian.hpp> //header file typedef std::vector<std::vector<double>> dataMat_NxN; void ReadCSV(std::string str_csv_file, dataMat_NxN &data_NxN) { std::ifstream in_file(str_csv_file, std::ios::in); std::string std_line; while (getline(in_file, std_line)) { std::stringstream ss(std_line); std::string data_each; std::vector<double> data_array; while (getline(ss, data_each, ',')) { data_array.push_back(atof(data_each.c_str())); } data_NxN.push_back(data_array); } } void WriteDat(std::string str_dat_file, dataMat_NxN &data_NxN) { std::ofstream out_file(str_dat_file, std::ios::out); for (int i = 0; i < data_NxN.size(); ++i) { for (int j = 0; j < data_NxN[i].size(); ++j) { out_file << data_NxN[i][j]<<" "; } out_file << "\n"; } } int main() { std::cout << "Demo program of GeographicLib." << std::endl; // geo origin init GeographicLib::LocalCartesian geo_converter; // data structure define dataMat_NxN geo_data; dataMat_NxN ENU_data; // read data from file std::string str_geo_data = "../lonlat.csv";//lat,lon,h ReadCSV(str_geo_data, geo_data); if (geo_data.size() > 0) { if (geo_data[0].size() == 3) { geo_converter.Reset(geo_data[0][0], geo_data[0][1], geo_data[0][2]); for (int i = 0; i < geo_data.size(); ++i) { double local_E, local_N, local_U; // convert[lat,lon,hgt] to ENU geo_converter.Forward(geo_data[i][0], geo_data[i][1], geo_data[i][2], local_E, local_N, local_U); std::vector<double> data_each; data_each.emplace_back(local_E); data_each.emplace_back(local_N); data_each.emplace_back(local_U); ENU_data.emplace_back(data_each); std::cout << "i=" << i << std::endl; } } } std::string str_ENU_data = "../enu.dat";//e,n,u WriteDat(str_ENU_data, ENU_data); return 0; } -
CMakeLists.txt 檔案
cmake_minimum_required(VERSION 3.10) project(geographiclib_demo) set(CMAKE_CXX_STANDARD 14) find_package (GeographicLib REQUIRED) include_directories(${GeographicLib_INCLUDE_DIRS}) add_executable(geographiclib_demo main.cpp) target_link_libraries(geographiclib_demo ${GeographicLib_LIBRARIES}) -
編譯 & 執行
git clone https://gitee.com/jqf64078/geographiclib_demo.git cd geographiclib_demo mkdir build && cd build cmake .. make ./geographiclib_demo -
轉換ENU米制坐標結果繪制,matlab 繪制,
enu_data = load('enu.dat'); plot(enu_data(:,1),enu_data(:,2));grid on;xlabel('East'),ylabel('North'); -
csv 檔案格式,程式只是簡單地定義 [緯度,經度,高度] 如下所示的格式進行讀取,
lat, lon, h,
lat, lon, h,
lat, lon, h,
…
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/275850.html
標籤:區塊鏈
上一篇:創建Vue專案以及報錯解決
