源代码:
//tifinfo.h
void getGeoTransform(const QString path, double *value);
private:
Ui::TifInfoClass ui;
QString m_previousDir;//记录上次打开的文件夹
QString m_Path;//文件路径
GDALDataset *m_Dataset;
private slots:
void selBtnClickedSlot();
void calcBtnClickedSlot();
//tifinfo.cpp
TifInfo::TifInfo(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
this->setFixedSize(600, 400);
m_Path = "";
m_previousDir = "\.";
connect(ui.selBtn, SIGNAL(clicked()), this, SLOT(selBtnClickedSlot()));
connect(ui.calcBtn, SIGNAL(clicked()), this, SLOT(calcBtnClickedSlot()));
}
TifInfo::~TifInfo()
{
}
void TifInfo::selBtnClickedSlot()
{
m_Path = QFileDialog::getOpenFileName(this, "选择文件", m_previousDir, "Files (*.tif)");
if (m_Path.isEmpty()) return;
QFileInfo fInfo(m_Path);
m_previousDir = fInfo.absolutePath();
ui.pathEdt->setText(m_Path);
}
void TifInfo::calcBtnClickedSlot()
{
if (m_Path.isEmpty()) return;
double value[6];
getGeoTransform(m_Path, value);
QFileInfo fInfo(m_Path);
QString showStr = "";
showStr.append(fInfo.fileName() + " :\n");
showStr.append("左上: " + QString::number(value[1], 'f', 12) + " , " + QString::number(value[0], 'f', 12) + "\n");
showStr.append("右上: " + QString::number(value[1], 'f', 12) + " , " + QString::number(value[2], 'f', 12) + "\n");
showStr.append("左下: " + QString::number(value[3], 'f', 12) + " , " + QString::number(value[0], 'f', 12) + "\n");
showStr.append("右下: " + QString::number(value[3], 'f', 12) + " , " + QString::number(value[2], 'f', 12) + "\n");
ui.textBrowser->append(showStr);
m_Path = "";
}
void TifInfo::getGeoTransform(const QString path, double *value)
{
GDALAllRegister();
CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
m_Dataset = (GDALDataset *)GDALOpen(path.toStdString().c_str(), GA_ReadOnly);
if (m_Dataset == NULL)
{
ui.textBrowser->append("无法打开文件!");
return;
}
double adfGeoTransform[6];
int XSize = m_Dataset->GetRasterXSize();
int YSize = m_Dataset->GetRasterYSize();
int imgFixedWidth = 3000;
if (m_Dataset->GetGeoTransform(adfGeoTransform) == CE_None)
{
value[0] = adfGeoTransform[0];
value[1] = adfGeoTransform[3];
value[2] = adfGeoTransform[1] * (double)XSize + adfGeoTransform[0];
value[3] = adfGeoTransform[5] * (double)YSize + adfGeoTransform[3];
value[4] = imgFixedWidth;
value[5] = (imgFixedWidth*YSize) / XSize;
if (value[0] > 180 || value[0] < -180)//墨卡托转WGS84
{
value[0] = Mercator2Lon(value[0]);
value[1] = Mercator2Lat(value[1]);
value[2] = Mercator2Lon(value[2]);
value[3] = Mercator2Lat(value[3]);
}
}
GDALClose(m_Dataset);
}
//GlobalFunction.h
#include <QtMath>
static double Mercator2Lon(double lon)//墨卡托转WGS84:经度
{
return lon / 20037508.34 * 180.0;
}
static double Mercator2Lat(double lat)//墨卡托转WGS84:纬度
{
double result = 0;
double mid = lat / 20037508.34 * 180.0;
result = 180.0 / M_PI*(2.0 * qAtan(qExp(mid*M_PI / 180.0)) - M_PI / 2.0);
return result;
}
效果:
评论区