You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nmWTAI-Platform/Src/nmNum/nmData/nmUtmProjection.cpp

172 lines
8.9 KiB
C++

#include "nmUtmProjection.h"
#include <QtCore/qnumeric.h>
#include <cmath>
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<int>(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<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)
{
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