|
|
|
|
@ -1,4 +1,5 @@
|
|
|
|
|
#include "nmWxGeoRefDlg.h"
|
|
|
|
|
#include "nmUtmProjection.h"
|
|
|
|
|
|
|
|
|
|
#include "iUnitGroup.h"
|
|
|
|
|
#include "iUnitHelper.h"
|
|
|
|
|
@ -14,161 +15,9 @@
|
|
|
|
|
#include <QPushButton>
|
|
|
|
|
#include <QVBoxLayout>
|
|
|
|
|
#include <QtCore/qnumeric.h>
|
|
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
|
|
namespace
|
|
|
|
|
{
|
|
|
|
|
// WGS84 投影计算只处理度和米,不访问控件、工程对象或显示单位。
|
|
|
|
|
const double s_dPi = 3.14159265358979323846;
|
|
|
|
|
const double s_dSemiMajor = 6378137.0; // WGS84 长半轴,单位 m。
|
|
|
|
|
const double s_dFlattening = 1.0 / 298.257223563;
|
|
|
|
|
const double s_dEccentricity2 = 2.0 * s_dFlattening - s_dFlattening * s_dFlattening;
|
|
|
|
|
const double s_dScale = 0.9996; // UTM 中央经线比例因子。
|
|
|
|
|
const char s_szBands[] = "CDEFGHJKLMNPQRSTUVWX";
|
|
|
|
|
|
|
|
|
|
// 检查度制经纬度是否有限且位于 WGS84/UTM 支持范围内。
|
|
|
|
|
bool isValidGeographic(double dLatitude, double dLongitude)
|
|
|
|
|
{
|
|
|
|
|
// UTM 仅覆盖南纬 80 度至北纬 84 度;极区需要另一套投影,不能沿用 UTM 公式。
|
|
|
|
|
return qIsFinite(dLatitude) && qIsFinite(dLongitude)
|
|
|
|
|
&& dLatitude >= -80.0 && dLatitude <= 84.0
|
|
|
|
|
&& dLongitude >= -180.0 && dLongitude <= 180.0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 检查米制投影坐标的基本范围;画布显示范围不参与参考点合法性判断。
|
|
|
|
|
bool isValidProjected(double dEasting, double dNorthing)
|
|
|
|
|
{
|
|
|
|
|
// 检查参考点本身,不随画布缩放范围改变允许输入的坐标范围。
|
|
|
|
|
return qIsFinite(dEasting) && qIsFinite(dNorthing)
|
|
|
|
|
&& dEasting >= 0.0 && dEasting <= 1000000.0
|
|
|
|
|
&& dNorthing >= 0.0 && dNorthing <= 10000000.0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 解析两位经度带号和一位纬度带字母;失败时输出参数不可用于转换。
|
|
|
|
|
bool parseZone(const QString& sZone, int& nZone, char& cBand)
|
|
|
|
|
{
|
|
|
|
|
bool bNumber = false;
|
|
|
|
|
nZone = sZone.left(2).toInt(&bNumber);
|
|
|
|
|
if (sZone.size() != 3 || !bNumber || nZone < 1 || nZone > 60) return false;
|
|
|
|
|
cBand = sZone.at(2).toLatin1();
|
|
|
|
|
if (!QString::fromLatin1(s_szBands).contains(QChar(cBand))) return false;
|
|
|
|
|
// Svalbard 的 X 纬度带只有加宽后的奇数分带,32X/34X/36X 不存在。
|
|
|
|
|
return cBand != 'X' || (nZone != 32 && nZone != 34 && nZone != 36);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据合法的度制纬度确定 C~X 纬度带,跳过 I、O。
|
|
|
|
|
char latitudeBand(double dLatitude)
|
|
|
|
|
{
|
|
|
|
|
// X 带覆盖 72~84 度,最后一个索引需截断,避免 84 度越界。
|
|
|
|
|
const int nBand = qBound(0, static_cast<int>(floor((dLatitude + 80.0) / 8.0)), 19);
|
|
|
|
|
return s_szBands[nBand];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 生成与旧工程一致的三字符分带标识,如 01C、50S。
|
|
|
|
|
QString zoneText(int nZone, char cBand)
|
|
|
|
|
{
|
|
|
|
|
return QString("%1%2").arg(nZone, 2, 10, QLatin1Char('0')).arg(QChar(cBand));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 按合法的度制经纬度选择自然 UTM 经度带,包含区域特例及日期变更线边界。
|
|
|
|
|
int longitudeZone(double dLatitude, double dLongitude)
|
|
|
|
|
{
|
|
|
|
|
// +180 度属于第 60 带,不能产生不存在的第 61 带。
|
|
|
|
|
int nZone = qBound(1, static_cast<int>(floor((dLongitude + 180.0) / 6.0)) + 1, 60);
|
|
|
|
|
// 按 UTM 约定处理 Norway 和 Svalbard 的特殊带宽。
|
|
|
|
|
if (dLatitude >= 56.0 && dLatitude < 64.0 && dLongitude >= 3.0 && dLongitude < 12.0)
|
|
|
|
|
nZone = 32;
|
|
|
|
|
if (dLatitude >= 72.0 && dLongitude >= 0.0 && dLongitude < 42.0) {
|
|
|
|
|
if (dLongitude < 9.0) nZone = 31;
|
|
|
|
|
else if (dLongitude < 21.0) nZone = 33;
|
|
|
|
|
else if (dLongitude < 33.0) nZone = 35;
|
|
|
|
|
else nZone = 37;
|
|
|
|
|
}
|
|
|
|
|
return nZone;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WGS84 正投影:输入纬度/经度(度),成功后输出自然分带和东向/北向坐标(米)。
|
|
|
|
|
bool geographicToUtm(double dLatitude, double dLongitude, QString& sZone,
|
|
|
|
|
double& dEasting, double& dNorthing)
|
|
|
|
|
{
|
|
|
|
|
if (!isValidGeographic(dLatitude, dLongitude)) return false;
|
|
|
|
|
const int nZone = longitudeZone(dLatitude, dLongitude);
|
|
|
|
|
const double dLat = dLatitude * s_dPi / 180.0;
|
|
|
|
|
const double dDeltaLon = (dLongitude - (-183.0 + nZone * 6.0)) * s_dPi / 180.0;
|
|
|
|
|
const double dE2 = s_dEccentricity2;
|
|
|
|
|
const double dEp2 = dE2 / (1.0 - dE2); // 第二偏心率平方,用于横轴墨卡托级数系数。
|
|
|
|
|
const double dN = s_dSemiMajor / sqrt(1.0 - dE2 * sin(dLat) * sin(dLat));
|
|
|
|
|
const double dT = tan(dLat) * tan(dLat);
|
|
|
|
|
const double dC = dEp2 * cos(dLat) * cos(dLat);
|
|
|
|
|
const double dA = cos(dLat) * dDeltaLon;
|
|
|
|
|
const double dA2 = dA * dA;
|
|
|
|
|
const double dM = s_dSemiMajor * (
|
|
|
|
|
(1.0 - dE2 / 4.0 - 3.0 * dE2 * dE2 / 64.0 - 5.0 * dE2 * dE2 * dE2 / 256.0) * dLat
|
|
|
|
|
- (3.0 * dE2 / 8.0 + 3.0 * dE2 * dE2 / 32.0 + 45.0 * dE2 * dE2 * dE2 / 1024.0) * sin(2.0 * dLat)
|
|
|
|
|
+ (15.0 * dE2 * dE2 / 256.0 + 45.0 * dE2 * dE2 * dE2 / 1024.0) * sin(4.0 * dLat)
|
|
|
|
|
- 35.0 * dE2 * dE2 * dE2 / 3072.0 * sin(6.0 * dLat));
|
|
|
|
|
dEasting = 500000.0 + s_dScale * dN * (dA + (1.0 - dT + dC) * dA * dA2 / 6.0
|
|
|
|
|
+ (5.0 - 18.0 * dT + dT * dT + 72.0 * dC - 58.0 * dEp2) * dA * dA2 * dA2 / 120.0);
|
|
|
|
|
dNorthing = s_dScale * (dM + dN * tan(dLat) * (dA2 / 2.0
|
|
|
|
|
+ (5.0 - dT + 9.0 * dC + 4.0 * dC * dC) * dA2 * dA2 / 24.0
|
|
|
|
|
+ (61.0 - 58.0 * dT + dT * dT + 600.0 * dC - 330.0 * dEp2) * dA2 * dA2 * dA2 / 720.0));
|
|
|
|
|
if (dLatitude < 0.0) dNorthing += 10000000.0; // 南半球假北值。
|
|
|
|
|
sZone = zoneText(nZone, latitudeBand(dLatitude));
|
|
|
|
|
return isValidProjected(dEasting, dNorthing);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WGS84 反投影:输入已校验的分带及米制坐标,成功后输出纬度/经度(度)。
|
|
|
|
|
bool utmToGeographic(int nZone, char cBand, double dEasting, double dNorthing,
|
|
|
|
|
double& dLatitude, double& dLongitude)
|
|
|
|
|
{
|
|
|
|
|
if (!isValidProjected(dEasting, dNorthing)) return false;
|
|
|
|
|
const double dE2 = s_dEccentricity2;
|
|
|
|
|
const double dEp2 = dE2 / (1.0 - dE2);
|
|
|
|
|
const double dE1 = (1.0 - sqrt(1.0 - dE2)) / (1.0 + sqrt(1.0 - dE2));
|
|
|
|
|
const double dY = dNorthing - (cBand < 'N' ? 10000000.0 : 0.0);
|
|
|
|
|
const double dMu = dY / (s_dScale * s_dSemiMajor
|
|
|
|
|
* (1.0 - dE2 / 4.0 - 3.0 * dE2 * dE2 / 64.0 - 5.0 * dE2 * dE2 * dE2 / 256.0));
|
|
|
|
|
const double dPhi = dMu + (3.0 * dE1 / 2.0 - 27.0 * pow(dE1, 3.0) / 32.0) * sin(2.0 * dMu)
|
|
|
|
|
+ (21.0 * dE1 * dE1 / 16.0 - 55.0 * pow(dE1, 4.0) / 32.0) * sin(4.0 * dMu)
|
|
|
|
|
+ 151.0 * pow(dE1, 3.0) / 96.0 * sin(6.0 * dMu)
|
|
|
|
|
+ 1097.0 * pow(dE1, 4.0) / 512.0 * sin(8.0 * dMu);
|
|
|
|
|
if (!qIsFinite(dPhi) || qAbs(cos(dPhi)) < 1e-6) return false;
|
|
|
|
|
const double dC = dEp2 * cos(dPhi) * cos(dPhi);
|
|
|
|
|
const double dT = tan(dPhi) * tan(dPhi);
|
|
|
|
|
const double dN = s_dSemiMajor / sqrt(1.0 - dE2 * sin(dPhi) * sin(dPhi));
|
|
|
|
|
const double dR = s_dSemiMajor * (1.0 - dE2) / pow(1.0 - dE2 * sin(dPhi) * sin(dPhi), 1.5);
|
|
|
|
|
const double dD = (dEasting - 500000.0) / (dN * s_dScale);
|
|
|
|
|
const double dD2 = dD * dD;
|
|
|
|
|
dLatitude = (dPhi - dN * tan(dPhi) / dR * (dD2 / 2.0
|
|
|
|
|
- (5.0 + 3.0 * dT + 10.0 * dC - 4.0 * dC * dC - 9.0 * dEp2) * dD2 * dD2 / 24.0
|
|
|
|
|
+ (61.0 + 90.0 * dT + 298.0 * dC + 45.0 * dT * dT - 252.0 * dEp2 - 3.0 * dC * dC)
|
|
|
|
|
* dD2 * dD2 * dD2 / 720.0)) * 180.0 / s_dPi;
|
|
|
|
|
dLongitude = -183.0 + nZone * 6.0 + (dD - (1.0 + 2.0 * dT + dC) * dD * dD2 / 6.0
|
|
|
|
|
+ (5.0 - 2.0 * dC + 28.0 * dT - 3.0 * dC * dC + 8.0 * dEp2 + 24.0 * dT * dT)
|
|
|
|
|
* dD * dD2 * dD2 / 120.0) / cos(dPhi) * 180.0 / s_dPi;
|
|
|
|
|
if (!qIsFinite(dLatitude) || !qIsFinite(dLongitude)) return false;
|
|
|
|
|
// 仅容忍投影级数在覆盖边界的微小数值误差,不把极区坐标截成合法纬度。
|
|
|
|
|
if (dLatitude < -80.0000001 || dLatitude > 84.0000001) return false;
|
|
|
|
|
dLatitude = qBound(-80.0, dLatitude, 84.0);
|
|
|
|
|
if ((cBand < 'N' && dLatitude >= 0.0) || (cBand >= 'N' && dLatitude < 0.0)) return false;
|
|
|
|
|
if (dLongitude > 180.0) dLongitude -= 360.0;
|
|
|
|
|
if (dLongitude < -180.0) dLongitude += 360.0;
|
|
|
|
|
return isValidGeographic(dLatitude, dLongitude);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 为已校验的分带求覆盖区域中心(度),供用户主动选带时初始化参考点。
|
|
|
|
|
void zoneCenter(int nZone, char cBand, double& dLatitude, double& dLongitude)
|
|
|
|
|
{
|
|
|
|
|
const int nBand = QString::fromLatin1(s_szBands).indexOf(QChar(cBand));
|
|
|
|
|
dLatitude = cBand == 'X' ? 78.0 : -76.0 + nBand * 8.0;
|
|
|
|
|
dLongitude = -183.0 + nZone * 6.0;
|
|
|
|
|
// 特殊加宽/缩窄带使用实际覆盖区域中心,确保初始化后仍属于用户选中的带。
|
|
|
|
|
if (cBand == 'V' && nZone == 31) dLongitude = 1.5;
|
|
|
|
|
if (cBand == 'V' && nZone == 32) dLongitude = 7.5;
|
|
|
|
|
if (cBand == 'X' && nZone == 31) dLongitude = 4.5;
|
|
|
|
|
if (cBand == 'X' && nZone == 37) dLongitude = 37.5;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 创建与模型位置、测量工具一致的三列坐标网格,布局所有权交给分组控件。
|
|
|
|
|
QGridLayout* createCoordinateGrid(QGroupBox* pGroup)
|
|
|
|
|
{
|
|
|
|
|
@ -229,7 +78,7 @@ void nmWxGeoRefDlg::initUI()
|
|
|
|
|
foreach (const QString& sZone, listZones) {
|
|
|
|
|
int nZone = 0;
|
|
|
|
|
char cBand = 0;
|
|
|
|
|
if (parseZone(sZone, nZone, cBand)) m_pUtmZoneComboBox->addItem(sZone);
|
|
|
|
|
if (nmUtmProjection::parseZone(sZone, nZone, cBand)) m_pUtmZoneComboBox->addItem(sZone);
|
|
|
|
|
}
|
|
|
|
|
m_pUtmZoneComboBox->setCurrentIndex(-1);
|
|
|
|
|
pProjectionGrid->addWidget(m_pUtmZoneComboBox, 0, 1, 1, 2);
|
|
|
|
|
@ -335,10 +184,10 @@ void nmWxGeoRefDlg::loadDataToUI()
|
|
|
|
|
m_pUtmZoneComboBox->setCurrentIndex(m_pUtmZoneComboBox->findText(m_sUtmZone));
|
|
|
|
|
int nZone = 0;
|
|
|
|
|
char cBand = 0;
|
|
|
|
|
m_bHasCoordinates = bComplete && parseZone(m_sUtmZone, nZone, cBand)
|
|
|
|
|
m_bHasCoordinates = bComplete && nmUtmProjection::parseZone(m_sUtmZone, nZone, cBand)
|
|
|
|
|
&& m_pUtmZoneComboBox->currentIndex() >= 0
|
|
|
|
|
&& isValidGeographic(m_dBaseValues[Latitude], m_dBaseValues[Longitude])
|
|
|
|
|
&& isValidProjected(m_dBaseValues[Easting], m_dBaseValues[Northing]);
|
|
|
|
|
&& nmUtmProjection::isValidGeographic(m_dBaseValues[Latitude], m_dBaseValues[Longitude])
|
|
|
|
|
&& nmUtmProjection::isValidProjected(m_dBaseValues[Easting], m_dBaseValues[Northing]);
|
|
|
|
|
m_bInputValid = m_bHasCoordinates;
|
|
|
|
|
// 加载时不重新投影或写回舍入值,避免仅打开旧工程就移动参考点。
|
|
|
|
|
m_bRefreshing = false;
|
|
|
|
|
@ -370,17 +219,17 @@ bool nmWxGeoRefDlg::updateCoordinates(InputSource eSource)
|
|
|
|
|
bool bValid = unitsAvailable();
|
|
|
|
|
if (eSource == GeographicInput) {
|
|
|
|
|
bValid = bValid && readField(Longitude, dLongitude) && readField(Latitude, dLatitude)
|
|
|
|
|
&& geographicToUtm(dLatitude, dLongitude, sZone, dEasting, dNorthing);
|
|
|
|
|
&& nmUtmProjection::geographicToUtm(dLatitude, dLongitude, sZone, dEasting, dNorthing);
|
|
|
|
|
} else if (eSource == ProjectedInput) {
|
|
|
|
|
int nZone = 0;
|
|
|
|
|
char cBand = 0;
|
|
|
|
|
bValid = bValid && parseZone(sZone, nZone, cBand)
|
|
|
|
|
bValid = bValid && nmUtmProjection::parseZone(sZone, nZone, cBand)
|
|
|
|
|
&& readField(Easting, dEasting) && readField(Northing, dNorthing)
|
|
|
|
|
&& utmToGeographic(nZone, cBand, dEasting, dNorthing, dLatitude, dLongitude);
|
|
|
|
|
&& nmUtmProjection::utmToGeographic(nZone, cBand, dEasting, dNorthing, dLatitude, dLongitude);
|
|
|
|
|
if (bValid) {
|
|
|
|
|
// 字母表示纬度带,不仅是南北半球;北向坐标跨带后同步更新字母。
|
|
|
|
|
sZone = zoneText(nZone, latitudeBand(dLatitude));
|
|
|
|
|
bValid = parseZone(sZone, nZone, cBand);
|
|
|
|
|
sZone = nmUtmProjection::zoneText(nZone, nmUtmProjection::latitudeBand(dLatitude));
|
|
|
|
|
bValid = nmUtmProjection::parseZone(sZone, nZone, cBand);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return m_bInputValid;
|
|
|
|
|
@ -457,10 +306,10 @@ void nmWxGeoRefDlg::onUtmZoneChanged(int nIndex)
|
|
|
|
|
const QString sSelected = m_pUtmZoneComboBox->itemText(nIndex);
|
|
|
|
|
double dLatitude = 0.0, dLongitude = 0.0, dEasting = 0.0, dNorthing = 0.0;
|
|
|
|
|
QString sZone;
|
|
|
|
|
if (!parseZone(sSelected, nZone, cBand)) return;
|
|
|
|
|
zoneCenter(nZone, cBand, dLatitude, dLongitude);
|
|
|
|
|
if (!nmUtmProjection::parseZone(sSelected, nZone, cBand)) return;
|
|
|
|
|
if (!nmUtmProjection::zoneCenter(nZone, cBand, dLatitude, dLongitude)) return;
|
|
|
|
|
// 沿用选带后填入区域中心的交互;四个值由同一次正投影产生,避免默认坐标相互矛盾。
|
|
|
|
|
m_bInputValid = geographicToUtm(dLatitude, dLongitude, sZone, dEasting, dNorthing)
|
|
|
|
|
m_bInputValid = nmUtmProjection::geographicToUtm(dLatitude, dLongitude, sZone, dEasting, dNorthing)
|
|
|
|
|
&& sZone == sSelected;
|
|
|
|
|
if (m_bInputValid) {
|
|
|
|
|
m_eLastInput = NoInput;
|
|
|
|
|
|