ECEF-LLA转换

相关资料:

https://convertecef.com/index?x=-4994693.1167072&y=4033563.5714824&z=2058287.9105044

http://jingweidu.757dy.com/

https://www.bejson.com/convert/map/

https://www.bejson.com/convert/map/

 

实例:

CEF2LLA.pro

 1 QT       += core gui
 2 
 3 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
 4 
 5 CONFIG += c++11
 6 
 7 # The following define makes your compiler emit warnings if you use
 8 # any Qt feature that has been marked deprecated (the exact warnings
 9 # depend on your compiler). Please consult the documentation of the
10 # deprecated API in order to know how to port your code away from it.
11 DEFINES += QT_DEPRECATED_WARNINGS
12 
13 # You can also make your code fail to compile if it uses deprecated APIs.
14 # In order to do so, uncomment the following line.
15 # You can also select to disable deprecated APIs only up to a certain version of Qt.
16 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
17 
18 SOURCES += \
19     main.cpp \
20     mainwindow.cpp
21 
22 HEADERS += \
23     mainwindow.h
24 
25 FORMS += \
26     mainwindow.ui
27 
28 # Default rules for deployment.
29 qnx: target.path = /tmp/$${TARGET}/bin
30 else: unix:!android: target.path = /opt/$${TARGET}/bin
31 !isEmpty(target.path): INSTALLS += target
View Code

main.cpp

 1 #include "mainwindow.h"
 2 
 3 #include <QApplication>
 4 
 5 int main(int argc, char *argv[])
 6 {
 7     QApplication a(argc, argv);
 8     MainWindow w;
 9     w.show();
10     return a.exec();
11 }
View Code

mainwindow.h

 1 #ifndef MAINWINDOW_H
 2 #define MAINWINDOW_H
 3 
 4 #include <QMainWindow>
 5 
 6 QT_BEGIN_NAMESPACE
 7 namespace Ui { class MainWindow; }
 8 QT_END_NAMESPACE
 9 
10 class MainWindow : public QMainWindow
11 {
12     Q_OBJECT
13 
14 public:
15     MainWindow(QWidget *parent = nullptr);
16     ~MainWindow();
17 
18 private slots:
19     void on_pushButton_clicked();
20 
21     void on_pushButton_2_clicked();
22 
23 private:
24     Ui::MainWindow *ui;
25 };
26 #endif // MAINWINDOW_H
View Code

mainwindow.cpp

  1 #include "mainwindow.h"
  2 #include "ui_mainwindow.h"
  3 
  4 const float M_PI = 3.1415926;
  5 
  6 struct GeographicCoordinates {
  7     double latitude;
  8     double longitude;
  9     double height;
 10 };
 11 
 12 struct CartesianCoordinates {
 13     double x;
 14     double y;
 15     double z;
 16 };
 17 
 18 CartesianCoordinates LLHtoECEF(GeographicCoordinates llh, double WGS84_a, double WGS84_f) {
 19     // 将LLH坐标转换为弧度
 20     double lat_rad = llh.latitude * M_PI / 180.0;
 21     double lon_rad = llh.longitude * M_PI / 180.0;
 22 
 23     // 计算辅助变量
 24     double sin_lat = sin(lat_rad);
 25     double cos_lat = cos(lat_rad);
 26     double sin_lon = sin(lon_rad);
 27     double cos_lon = cos(lon_rad);
 28 
 29     // 计算WGS84椭球体的扁率平方
 30     double e2 = (2 - WGS84_f) * WGS84_f;
 31 
 32     // 计算辅助变量
 33     double N = WGS84_a / sqrt(1 - e2 * sin_lat * sin_lat);
 34 
 35     // 计算ECEF坐标
 36     CartesianCoordinates ecef;
 37     ecef.x = (N + llh.height) * cos_lat * cos_lon;
 38     ecef.y = (N + llh.height) * cos_lat * sin_lon;
 39     ecef.z = (N * (1 - e2) + llh.height) * sin_lat;
 40 
 41     return ecef;
 42 }
 43 
 44 MainWindow::MainWindow(QWidget *parent)
 45     : QMainWindow(parent)
 46     , ui(new Ui::MainWindow)
 47 {
 48     ui->setupUi(this);
 49     this->setWindowTitle(QStringLiteral("ECEF-LLA转换"));
 50 }
 51 
 52 MainWindow::~MainWindow()
 53 {
 54     delete ui;
 55 }
 56 
 57 
 58 void MainWindow::on_pushButton_clicked()
 59 {
 60     double latitude, longitude, height;
 61     CartesianCoordinates xyz = {ui->coordX->value()
 62                                 , ui->coordY->value()
 63                                 , ui->coordZ->value()};
 64 
 65     double WGS84_A = 6378137.0;      // major axis
 66     double WGS84_E = 0.081819190842622;   // first eccentricity
 67 
 68     double b = sqrt(WGS84_A * WGS84_A * (1 - WGS84_E * WGS84_E));
 69     double ep = sqrt((WGS84_A * WGS84_A - b * b) / (b * b));
 70     double p = hypot(xyz.x, xyz.y);
 71     double th = atan2(WGS84_A * xyz.z, b * p);
 72     double lon = atan2(xyz.y, xyz.x);
 73     double lat = atan2((xyz.z + ep * ep * b * pow(sin(th), 3)), (p - WGS84_E * WGS84_E * WGS84_A * pow(cos(th), 3)));
 74     double N = WGS84_A / sqrt(1 - WGS84_E * WGS84_E * sin(lat) * sin(lat));
 75     double alt = p / cos(lat) - N;
 76     latitude = lat*180 / M_PI;
 77     longitude = lon*180 / M_PI;
 78     height = alt;
 79 
 80     QString s = " lat:%1 \n lon:%2 \n alt:%3";
 81     ui->textEdit->append(
 82                 s.arg(QString::number(latitude, 'f', 9))
 83                 .arg(QString::number(longitude, 'f', 9))
 84                 .arg(QString::number(height, 'f', 9))
 85                 );
 86 }
 87 
 88 void MainWindow::on_pushButton_2_clicked()
 89 {
 90     double WGS84_a = 6378137.0;
 91     double WGS84_f = 1 / 298.257223563;
 92     GeographicCoordinates llh = {ui->coordLON->value()
 93                                  , ui->coordLAT->value()
 94                                  , ui->coordALT->value()
 95                                 }; // 纽约市经纬度高度为0的坐标
 96     CartesianCoordinates ecef = LLHtoECEF(llh, WGS84_a, WGS84_f);
 97     QString s = " x:%1 \n y:%2 \n z:%3";
 98     ui->textEdit->append(
 99                 s.arg(QString::number(ecef.x, 'f', 9))
100                 .arg(QString::number(ecef.y, 'f', 9))
101                 .arg(QString::number(ecef.z, 'f', 9))
102                 );
103 
104 }
View Code

mainwindow.ui

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <ui version="4.0">
  3  <class>MainWindow</class>
  4  <widget class="QMainWindow" name="MainWindow">
  5   <property name="geometry">
  6    <rect>
  7     <x>0</x>
  8     <y>0</y>
  9     <width>457</width>
 10     <height>399</height>
 11    </rect>
 12   </property>
 13   <property name="windowTitle">
 14    <string>MainWindow</string>
 15   </property>
 16   <widget class="QWidget" name="centralwidget">
 17    <layout class="QVBoxLayout" name="verticalLayout_3">
 18     <item>
 19      <layout class="QHBoxLayout" name="horizontalLayout">
 20       <item>
 21        <layout class="QVBoxLayout" name="verticalLayout_2">
 22         <item>
 23          <widget class="QDoubleSpinBox" name="coordLON">
 24           <property name="decimals">
 25            <number>7</number>
 26           </property>
 27           <property name="minimum">
 28            <double>-99999999999999.000000000000000</double>
 29           </property>
 30           <property name="maximum">
 31            <double>99999999999999.000000000000000</double>
 32           </property>
 33           <property name="value">
 34            <double>40.712800000000001</double>
 35           </property>
 36          </widget>
 37         </item>
 38         <item>
 39          <widget class="QDoubleSpinBox" name="coordLAT">
 40           <property name="decimals">
 41            <number>6</number>
 42           </property>
 43           <property name="minimum">
 44            <double>-99999999999999.000000000000000</double>
 45           </property>
 46           <property name="maximum">
 47            <double>99999999999999.000000000000000</double>
 48           </property>
 49           <property name="value">
 50            <double>-74.006000000000000</double>
 51           </property>
 52          </widget>
 53         </item>
 54         <item>
 55          <widget class="QDoubleSpinBox" name="coordALT">
 56           <property name="decimals">
 57            <number>6</number>
 58           </property>
 59           <property name="minimum">
 60            <double>-99999999999999.000000000000000</double>
 61           </property>
 62           <property name="maximum">
 63            <double>99999999999999.000000000000000</double>
 64           </property>
 65           <property name="value">
 66            <double>0.000000000000000</double>
 67           </property>
 68          </widget>
 69         </item>
 70         <item>
 71          <widget class="QPushButton" name="pushButton_2">
 72           <property name="text">
 73            <string>LLA2ECEF</string>
 74           </property>
 75          </widget>
 76         </item>
 77        </layout>
 78       </item>
 79       <item>
 80        <layout class="QVBoxLayout" name="verticalLayout">
 81         <item>
 82          <widget class="QDoubleSpinBox" name="coordX">
 83           <property name="decimals">
 84            <number>6</number>
 85           </property>
 86           <property name="minimum">
 87            <double>-99999999999999.000000000000000</double>
 88           </property>
 89           <property name="maximum">
 90            <double>99999999999999.000000000000000</double>
 91           </property>
 92           <property name="value">
 93            <double>1333998.784131000051275</double>
 94           </property>
 95          </widget>
 96         </item>
 97         <item>
 98          <widget class="QDoubleSpinBox" name="coordY">
 99           <property name="decimals">
100            <number>6</number>
101           </property>
102           <property name="minimum">
103            <double>-99999999999999.000000000000000</double>
104           </property>
105           <property name="maximum">
106            <double>99999999999999.000000000000000</double>
107           </property>
108           <property name="value">
109            <double>-4654044.896168000064790</double>
110           </property>
111          </widget>
112         </item>
113         <item>
114          <widget class="QDoubleSpinBox" name="coordZ">
115           <property name="decimals">
116            <number>6</number>
117           </property>
118           <property name="minimum">
119            <double>-99999999999999.000000000000000</double>
120           </property>
121           <property name="maximum">
122            <double>99999999999999.000000000000000</double>
123           </property>
124           <property name="value">
125            <double>4138300.073983999900520</double>
126           </property>
127          </widget>
128         </item>
129         <item>
130          <widget class="QPushButton" name="pushButton">
131           <property name="text">
132            <string>ECEF2LLA</string>
133           </property>
134          </widget>
135         </item>
136        </layout>
137       </item>
138      </layout>
139     </item>
140     <item>
141      <widget class="QTextEdit" name="textEdit"/>
142     </item>
143    </layout>
144   </widget>
145  </widget>
146  <resources/>
147  <connections/>
148 </ui>
View Code

 

 

posted on 2025-05-17 23:17  疯狂delphi  阅读(72)  评论(0)    收藏  举报

导航