diff --git a/Include/nmNum/nmData/nmDataAnalyzeManager.h b/Include/nmNum/nmData/nmDataAnalyzeManager.h index 11e0aa8d..b58b0d2a 100644 --- a/Include/nmNum/nmData/nmDataAnalyzeManager.h +++ b/Include/nmNum/nmData/nmDataAnalyzeManager.h @@ -408,10 +408,13 @@ public: void setMeasureData(nmDataMeasure *pMeasureData); bool removeMeasureData(); - // 模型参考点相关 + // 重建空参考点并通知位置显示刷新;返回对象由本 Manager 持有。 nmDataGeoRef* createGeoRefData(); + // 借用当前参考点,尚未设置时为空;调用方不得释放或跨重建缓存该指针。 nmDataGeoRef* getGeoRefData() const; + // 返回参考点编辑副本;尚未设置时返回默认空对象,不创建真实工程数据。 nmDataGeoRef getGeoRefDataCopy() const; + // 完整替换参考点后通知位置显示刷新;坐标更新不改变求解物性或网格输入版本。 void updateGeoRefData(const nmDataGeoRef& newData); // 预测相关 @@ -802,6 +805,8 @@ public: void sigWellRemoved(QString wellCode, QString wellName); /// @brief 参数面板中的对象列表或对象名称发生变化 void sigGeometryListChanged(); + /** @brief 模型参考点已完整更新、重建或随工程替换;接收方需重新读取当前对象。 */ + void sigGeoReferenceChanged(); /** @brief 当前分析最后一次排队的 PEBI 建网流程已经结束。 */ void sigPebiGridGenerationFinished(quint64 nGridInputRevision, bool bSucceeded); diff --git a/Include/nmNum/nmData/nmUtmProjection.h b/Include/nmNum/nmData/nmUtmProjection.h new file mode 100644 index 00000000..ac6ba8d3 --- /dev/null +++ b/Include/nmNum/nmData/nmUtmProjection.h @@ -0,0 +1,46 @@ +#ifndef NMUTMPROJECTION_H +#define NMUTMPROJECTION_H + +#include "nmData_global.h" +#include + +// WGS84/UTM 坐标工具,不依赖 QWidget、分析管理器或当前工程。 +// 经纬度统一使用十进制度,投影坐标统一使用米;显示单位由调用方交给框架换算。 +namespace nmUtmProjection +{ + // 检查有限经纬度及 UTM 覆盖范围:纬度 [-80, 84],经度 [-180, 180]。 + NM_DATA_EXPORT bool isValidGeographic(double dLatitude, double dLongitude); + + // 检查有限米制坐标及基本范围:东向 [0, 1000000],北向 [0, 10000000]。 + // 此检查不能代替反投影后的纬度范围检查。 + NM_DATA_EXPORT bool isValidProjected(double dEasting, double dNorthing); + + // 解析两位经度带号和纬度带字母(如 50S);S 是纬度带,不表示南半球。 + // 排除 32X/34X/36X 等不存在的自然分带;失败时调用方不得使用输出参数。 + NM_DATA_EXPORT bool parseZone(const QString& sZone, int& nZone, char& cBand); + + // 将合法纬度(度)转换为 C~X 纬度带;纬度无效时返回 0,X 带包含北纬 84 度。 + NM_DATA_EXPORT char latitudeBand(double dLatitude); + + // 格式化带号和字母,不改变投影坐标;基本字符或数值不合法时返回空串。 + // 是否属于有效自然分带仍由 parseZone 检查。 + NM_DATA_EXPORT QString zoneText(int nZone, char cBand); + + // 正投影:输入纬度/经度(度),输出自然分带和东向/北向(米)。 + // 包含 Norway/Svalbard 特殊带宽及 +/-180 度边界;失败时不要使用输出参数。 + NM_DATA_EXPORT bool geographicToUtm(double dLatitude, double dLongitude, QString& sZone, + double& dEasting, double& dNorthing); + + // 反投影:按指定经度带及字母对应的半球解释米制坐标,输出纬度/经度(度)。 + // 不按反算位置自动换成相邻自然带:模型偏移坐标必须继续使用参考点的投影带。 + // 字母用于确定假北值,不限制结果仍处于原 8 度纬度带;超出半球/UTM 范围返回 false。 + // 失败时不要使用输出参数,调用方应清空派生经纬度,避免保留上一次结果。 + NM_DATA_EXPORT bool utmToGeographic(int nZone, char cBand, double dEasting, double dNorthing, + double& dLatitude, double& dLongitude); + + // 返回有效自然分带覆盖区域的中心经纬度(度),供用户主动选带时初始化参考点。 + // 特殊宽窄带使用覆盖区域中心;无效分带返回 false,不产生默认坐标。 + NM_DATA_EXPORT bool zoneCenter(int nZone, char cBand, double& dLatitude, double& dLongitude); +} + +#endif // NMUTMPROJECTION_H diff --git a/Include/nmNum/nmSubWxs/nmWxPointerPosDlg.h b/Include/nmNum/nmSubWxs/nmWxPointerPosDlg.h index 8756d787..b2d85ef7 100644 --- a/Include/nmNum/nmSubWxs/nmWxPointerPosDlg.h +++ b/Include/nmNum/nmSubWxs/nmWxPointerPosDlg.h @@ -14,7 +14,8 @@ class nmDataAnalyzeManager; /** * @brief 模型位置显示窗口,跟随所属画布的鼠标位置更新坐标,不修改分析数据。 * @note 本地 X/Y 和东向、北向坐标以米接收,由框架单位绑定器负责显示换算。 - * 坐标均可选中复制;UTM 带只展示当前参考点的设置,不提供编辑入口。 + * 经纬度由参考点 UTM 坐标加本地偏移后统一反投影,不混用旧工程中独立保存的经纬度。 + * 坐标均可选中复制;UTM 带展示参考点的投影设置,跨自然分带时仍使用该带解释平面坐标。 * 输入列与单位列按 8:5 横向伸展,纵向采用字体和主题的推荐高度。 */ class NM_SUB_WXS_EXPORT nmWxPointerPosDlg : public iDlgBase @@ -29,6 +30,13 @@ public slots: /** @brief 接收画布的米制坐标,刷新显示并保留各字段已选择的单位。 */ void setPointerPos(double x, double y); +private slots: + /** @brief 参考点变更后按最近的米制鼠标位置重算,无需等待下一次鼠标移动。 */ + void refreshCoordinates(); + + /** @brief 所属分析销毁时解除弱引用并清空显示,避免停留在已关闭分析的坐标上。 */ + void onDataManagerDestroyed(); + private: /** @brief 四个长度字段分别保存单位选择,不混用本地坐标与地理坐标。 */ enum LengthField @@ -60,16 +68,18 @@ private: // 借用所属分析的管理器,删除时自动置空,不使用全局当前分析。 // 参考点可能在窗口打开后新增、清空或重建,每次刷新重新读取,不缓存其裸指针。 QPointer m_pDataManager; + double m_dLastLocalX; // 最近收到的本地 X,单位 m;保存 NaN/无穷值以维持无效位置的清空状态。 + double m_dLastLocalY; // 最近收到的本地 Y,单位 m;参考点刷新只使用原值,不反读已换单位的文本。 // 长度控件均由 Qt 父子关系释放;绑定状态独立记录,禁止以错误单位展示原始值。 - QLineEdit* m_pLengthEdits[LengthFieldCount]; - QComboBox* m_pLengthUnits[LengthFieldCount]; - bool m_bUnitsBound[LengthFieldCount]; + QLineEdit* m_pLengthEdits[LengthFieldCount]; // 本地 X/Y 与东向/北向坐标的只读输入框。 + QComboBox* m_pLengthUnits[LengthFieldCount]; // 各字段独立的框架长度单位选择。 + bool m_bUnitsBound[LengthFieldCount]; // 单位绑定是否成功,失败时禁止显示未换算值。 // 非长度字段使用普通只读文本框,经纬度文本本身包含度、分、秒符号。 - QLineEdit* m_pLonEdit; - QLineEdit* m_pLatEdit; - QLineEdit* m_pUtmZoneEdit; + QLineEdit* m_pLonEdit; // WGS84 经度,按度分秒显示。 + QLineEdit* m_pLatEdit; // WGS84 纬度,按度分秒显示。 + QLineEdit* m_pUtmZoneEdit; // 解释当前投影坐标所用的参考点分带。 }; #endif // NMWXPOINTERPOSDLG_H diff --git a/Src/nmNum/nmData/nmDataAnalyzeManager.cpp b/Src/nmNum/nmData/nmDataAnalyzeManager.cpp index 852e77e1..921b9015 100644 --- a/Src/nmNum/nmData/nmDataAnalyzeManager.cpp +++ b/Src/nmNum/nmData/nmDataAnalyzeManager.cpp @@ -2891,6 +2891,7 @@ nmDataGeoRef* nmDataAnalyzeManager::createGeoRefData() } m_pGeoRefData = new nmDataGeoRef; + emit sigGeoReferenceChanged(); return m_pGeoRefData; } @@ -2915,6 +2916,8 @@ void nmDataAnalyzeManager::updateGeoRefData(const nmDataGeoRef& newData) } *m_pGeoRefData = newData; // 调用赋值运算符 + // 五个属性全部替换后再通知,已打开的位置窗口立即按最近的鼠标位置刷新。 + emit sigGeoReferenceChanged(); } nmDataForecast* nmDataAnalyzeManager::createForecastData() @@ -6197,6 +6200,7 @@ bool nmDataAnalyzeManager::ReadProjectData(const QString & filePath) return false; } + emit sigGeoReferenceChanged(); return true; } @@ -6958,6 +6962,8 @@ bool nmDataAnalyzeManager::loadNmResult(QString sLoadAnalDir) swapLoadedProjectState(oLoadedManager); rebuildLoadedAttributeRegistry(); rebindPebiResultSnapshotToWells(); + // 成果发布可能替换或移除参考点,已有位置窗口必须读取发布后的对象。 + emit sigGeoReferenceChanged(); emit sigPebiResultSnapshotChanged(); notifyPebiResultStateChangedIfNeeded(oPreviousResultState); return true; diff --git a/Src/nmNum/nmData/nmUtmProjection.cpp b/Src/nmNum/nmData/nmUtmProjection.cpp new file mode 100644 index 00000000..4e099be7 --- /dev/null +++ b/Src/nmNum/nmData/nmUtmProjection.cpp @@ -0,0 +1,171 @@ +#include "nmUtmProjection.h" + +#include +#include + +namespace nmUtmProjection +{ +// 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 + || sZone.at(0) < QLatin1Char('0') || sZone.at(0) > QLatin1Char('9') + || sZone.at(1) < QLatin1Char('0') || sZone.at(1) > QLatin1Char('9')) 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) +{ + if (!isValidGeographic(dLatitude, 0.0)) return 0; + // X 带覆盖 72~84 度,最后一个索引需截断,避免 84 度越界。 + const int nBand = qBound(0, static_cast(floor((dLatitude + 80.0) / 8.0)), 19); + return s_szBands[nBand]; +} + +// 生成与旧工程一致的三字符分带标识,如 01C、50S。 +QString zoneText(int nZone, char cBand) +{ + if (nZone < 1 || nZone > 60 || !QString::fromLatin1(s_szBands).contains(QChar(cBand))) + return QString(); + return QString("%1%2").arg(nZone, 2, 10, QLatin1Char('0')).arg(QChar(cBand)); +} + +// 按合法的度制经纬度选择自然 UTM 经度带,包含区域特例及日期变更线边界。 +static int longitudeZone(double dLatitude, double dLongitude) +{ + // +180 度属于第 60 带,不能产生不存在的第 61 带。 + int nZone = qBound(1, static_cast(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) +{ + int nParsedZone = 0; // 公共入口不能依赖调用方已检查分带,避免无效带号直接参与计算。 + char cParsedBand = 0; + if (!parseZone(zoneText(nZone, cBand), nParsedZone, cParsedBand) + || !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); +} + +// 为已校验的分带求覆盖区域中心(度),供用户主动选带时初始化参考点。 +bool zoneCenter(int nZone, char cBand, double& dLatitude, double& dLongitude) +{ + int nParsedZone = 0; + char cParsedBand = 0; + if (!parseZone(zoneText(nZone, cBand), nParsedZone, cParsedBand)) return false; + 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; + return true; +} + +} // namespace nmUtmProjection diff --git a/Src/nmNum/nmSubWxs/nmWxGeoRefDlg.cpp b/Src/nmNum/nmSubWxs/nmWxGeoRefDlg.cpp index f5fce06f..61661cc1 100644 --- a/Src/nmNum/nmSubWxs/nmWxGeoRefDlg.cpp +++ b/Src/nmNum/nmSubWxs/nmWxGeoRefDlg.cpp @@ -1,4 +1,5 @@ #include "nmWxGeoRefDlg.h" +#include "nmUtmProjection.h" #include "iUnitGroup.h" #include "iUnitHelper.h" @@ -14,161 +15,9 @@ #include #include #include -#include 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(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(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; diff --git a/Src/nmNum/nmSubWxs/nmWxPointerPosDlg.cpp b/Src/nmNum/nmSubWxs/nmWxPointerPosDlg.cpp index e69d522e..2cb5e32a 100644 --- a/Src/nmNum/nmSubWxs/nmWxPointerPosDlg.cpp +++ b/Src/nmNum/nmSubWxs/nmWxPointerPosDlg.cpp @@ -3,6 +3,7 @@ #include "iUnitWxBinder.h" #include "nmDataAnalyzeManager.h" #include "nmDataGeoRef.h" +#include "nmUtmProjection.h" #include "nmGuiPlot.h" #include "ZxBaseUtil.h" @@ -16,7 +17,6 @@ #include #include #include -#include namespace { @@ -57,6 +57,8 @@ namespace nmWxPointerPosDlg::nmWxPointerPosDlg(QWidget* parent) : iDlgBase(parent), + m_dLastLocalX(0.0), + m_dLastLocalY(0.0), m_pLonEdit(nullptr), m_pLatEdit(nullptr), m_pUtmZoneEdit(nullptr) @@ -76,6 +78,11 @@ nmWxPointerPosDlg::nmWxPointerPosDlg(QWidget* parent) } initUI(); + if (m_pDataManager) { + // Manager 在完整替换参考点后通知,避免属性逐个赋值时读到不一致的中间状态。 + connect(m_pDataManager.data(), SIGNAL(sigGeoReferenceChanged()), this, SLOT(refreshCoordinates())); + connect(m_pDataManager.data(), SIGNAL(destroyed(QObject*)), this, SLOT(onDataManagerDestroyed())); + } // 沿用原窗口的初始状态:尚未收到鼠标位置时,展示本地原点及对应参考点。 setPointerPos(0.0, 0.0); setWindowTitle(tr("Pointer position")); @@ -182,8 +189,22 @@ void nmWxPointerPosDlg::clearGeographicValues() setLengthValue(Northing, 0.0, false); } +void nmWxPointerPosDlg::refreshCoordinates() +{ + setPointerPos(m_dLastLocalX, m_dLastLocalY); +} + +void nmWxPointerPosDlg::onDataManagerDestroyed() +{ + // 显式置空,不依赖 QPointer 在不同 Qt 版本中的销毁通知先后顺序。 + m_pDataManager = nullptr; + refreshCoordinates(); +} + void nmWxPointerPosDlg::setPointerPos(double x, double y) { + m_dLastLocalX = x; + m_dLastLocalY = y; // 若所属分析已释放或画布已改绑,不把新画布的坐标套用到旧分析的参考点。 nmGuiPlot* pOwnerPlot = qobject_cast(parentWidget()); const bool bValidPoint = !m_pDataManager.isNull() && pOwnerPlot != nullptr && @@ -206,61 +227,41 @@ void nmWxPointerPosDlg::setPointerPos(double x, double y) } const QString sZone = pGeoRef->getUtmZone().getValue().toString().trimmed(); - bool bZoneOK = false; - const int nZone = sZone.left(2).toInt(&bZoneOK); - const QString sBands("CDEFGHJKLMNPQRSTUVWX"); - if (!bZoneOK || nZone < 1 || nZone > 60 || sZone.size() != 3 || - !sBands.contains(sZone.at(2))) + int nZone = 0; // 参考点的经度带号,整个本地网格始终使用同一投影坐标系。 + char cBand = 0; // 参考点的纬度带,用于识别南北半球及假北值。 + if (!nmUtmProjection::parseZone(sZone, nZone, cBand)) { clearGeographicValues(); return; } - // 空字段不能按 0 参与换算;无效数据只清空显示,不弹校验提示。 + // 以分带和 UTM 原值为准,不读取另存的参考点经纬度,避免旧工程两组数值不一致时混算。 bool bEastingOK = false; bool bNorthingOK = false; - bool bLongitudeOK = false; - bool bLatitudeOK = false; const double dBaseEasting = pGeoRef->getEasting().getValue().toDouble(&bEastingOK); const double dBaseNorthing = pGeoRef->getNorthing().getValue().toDouble(&bNorthingOK); - const double dBaseLongitude = pGeoRef->getLongitude().getValue().toDouble(&bLongitudeOK); - const double dBaseLatitude = pGeoRef->getLatitude().getValue().toDouble(&bLatitudeOK); - if (!bEastingOK || !bNorthingOK || !bLongitudeOK || !bLatitudeOK || - !qIsFinite(dBaseEasting) || !qIsFinite(dBaseNorthing) || - !qIsFinite(dBaseLongitude) || !qIsFinite(dBaseLatitude) || - qAbs(dBaseLongitude) > 180.0 || qAbs(dBaseLatitude) > 90.0) + double dLatitude = 0.0; // WGS84 反投影结果,单位为度;仅成功后才格式化显示。 + double dLongitude = 0.0; + if (!bEastingOK || !bNorthingOK || !nmUtmProjection::utmToGeographic( + nZone, cBand, dBaseEasting, dBaseNorthing, dLatitude, dLongitude)) { clearGeographicValues(); return; } - // 模型坐标为相对参考点的米制偏移,显示单位不参与坐标计算。 + // 米制偏移先加在同一投影平面中,再反算经纬度。跨自然经度带/纬度带时不重选投影带, + // 否则相同东向/北向数值会被解释为另一位置;参考点设置与模型位置共用同一反投影入口。 const double dEasting = dBaseEasting + x; const double dNorthing = dBaseNorthing + y; - if (!qIsFinite(dEasting) || !qIsFinite(dNorthing)) + if (!nmUtmProjection::utmToGeographic(nZone, cBand, dEasting, dNorthing, dLatitude, dLongitude)) { + // 非有限值、半球边界或 UTM 覆盖范围外不展示伪坐标;保留本地 X/Y 便于定位。 clearGeographicValues(); return; } m_pUtmZoneEdit->setText(sZone); setLengthValue(Easting, dEasting, true); setLengthValue(Northing, dNorthing, true); - - // 沿用现有的小范围经纬度近似换算,并非严格的 UTM 反投影。 - // 极点附近经度换算分母趋近于零,此时保留平面坐标,清空无法可靠计算的经纬度。 - const double dMetersPerDegreeLat = 111111.0; - const double dPi = 3.14159265358979323846; - const double dMetersPerDegreeLon = dMetersPerDegreeLat * - std::cos(dBaseLatitude * dPi / 180.0); - if (qAbs(dMetersPerDegreeLon) < 1.0e-6) - { - m_pLonEdit->clear(); - m_pLatEdit->clear(); - return; - } - - const double dLatitude = dBaseLatitude + y / dMetersPerDegreeLat; - const double dLongitude = dBaseLongitude + x / dMetersPerDegreeLon; m_pLonEdit->setText(formatCoordinate(dLongitude, false)); m_pLatEdit->setText(formatCoordinate(dLatitude, true)); }