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.

257 lines
5.4 KiB
C++

2 years ago
#include "StdAfx.h"
#include "GetElevation.h"
// tiff<66>ļ<EFBFBD><C4BC><EFBFBD>ȡ
#include "gdal_priv.h"
#include "cpl_conv.h" //for CPLMalloc()
// boost-1.56:׼C++<2B><>׼<EFBFBD><D7BC>
#include <boost/lexical_cast.hpp>
#include <boost/format.hpp>
// ȫ<>ֱ<EFBFBD><D6B1><EFBFBD>
std::deque<struDemPic*> g_DemPicList;
// <20><><EFBFBD>ܣ<EFBFBD><DCA3><EFBFBD><EFBFBD><EFBFBD>DEM<45><4D><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD>
void ClearDemMem()
{
if (g_DemPicList.empty() == false)
{
for (size_t i = 0; i < g_DemPicList.size(); i++)
{
if (g_DemPicList.at(i) != nullptr && g_DemPicList.at(i)->pDEMData != nullptr)
{
delete[] g_DemPicList.at(i)->pDEMData;
g_DemPicList.at(i)->pDEMData = nullptr;
delete g_DemPicList.at(i);
g_DemPicList.at(i) = nullptr;
}
}
g_DemPicList.clear();
}
}
// <20><><EFBFBD>ھ<EFBFBD>γ<EFBFBD><CEB3> <20><><EFBFBD><EFBFBD>tif<69>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><><CDBC><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD>Ӧ<EFBFBD><D3A6>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD><EFBFBD>½Ǿ<C2BD>γ<EFBFBD><CEB3>)
string GetDemName( const double dLonDeg, const double dLatDeg )
{
string sLatLon; // <20><><EFBFBD><EFBFBD>
int iLon = (int) dLonDeg;
int iLat = (int) dLatDeg;
//<2F>жϾ<D0B6>γ<EFBFBD><CEB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Χ֮<CEA7><D6AE>
if (abs(dLonDeg) > 180)
{
return "";
}
if (abs(dLatDeg) > 90)
{
return "";
}
string sLat, sLon;
// γ<><CEB3> <20>ϱ<EFBFBD>γ <20><><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD>
boost::format fmt("S%02d");
if (dLatDeg <= 0)
{
fmt % ((-iLat) + 1);
sLat = fmt.str();
}
else
{
fmt = boost::format("N%02d");
fmt % iLat;
sLat = fmt.str();
}
// <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD>
if (dLonDeg <= 0)
{
fmt = boost::format("W%03d");
fmt % ((-iLon) + 1);
sLon = fmt.str();
}
else
{
fmt = boost::format("E%03d");
fmt % iLon;
sLon = fmt.str();
}
sLatLon = "ASTGTM_" + sLat + sLon + "_dem.tif";
return sLatLon;
}
// <20><>Ѱ<EFBFBD>ļ<EFBFBD>·<EFBFBD><C2B7><EFBFBD>Ƿ<EFBFBD><C7B7>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD><EFBFBD>
int FindDemDataSet( const string strDemName )
{
for ( unsigned int i = 0; i < g_DemPicList.size(); i++ )
{
if ( g_DemPicList[i]->strDemName == strDemName )
{
return i;
}
}
return -1;
}
bool GetDem( float& fElevator, double dLonDeg, double dLatDeg, struDemPic* pDemPic, bool bNearst)
{
// <20><>ֵ<EFBFBD>ж<EFBFBD>
if (pDemPic == NULL)
{
return false;
}
// <20>ֱ<EFBFBD><D6B1><EFBFBD> <20>ж<EFBFBD>
if (pDemPic->adfGeoTransform[1] <= 0 || pDemPic->adfGeoTransform[5] >= 0)
{
return false;
}
// <20>߽<EFBFBD>
double leftLon = pDemPic->adfGeoTransform[0];
double rightLon = leftLon + 1;
double topLat = pDemPic->adfGeoTransform[3];
double bottomLat = topLat - 1;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
double px = (dLonDeg - pDemPic->adfGeoTransform[0]) / pDemPic->adfGeoTransform[1];
double py = (dLatDeg - pDemPic->adfGeoTransform[3]) / pDemPic->adfGeoTransform[5];
int ipx = int(px);
int ipy = int(py);
// <20><>ͼ<EFBFBD><CDBC><EFBFBD>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>±߽<C2B1><DFBD><EFBFBD><EFBFBD>ұ߽<D2B1>
if (dLonDeg >= leftLon && dLonDeg < rightLon && dLatDeg > bottomLat && dLatDeg <= topLat)
{
// <20><><EFBFBD><EFBFBD><EFBFBD>ڷ<EFBFBD>
if (bNearst == true)
{
int x = int(px + 0.5);
int y = int(py + 0.5);
fElevator = *(pDemPic->pDEMData + y * pDemPic->iXSize + x);
}
else
{
// <20><><EFBFBD><EFBFBD><><CBAB><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>ֵ
int yt = 0, yb = 0; // y <20><><EFBFBD><EFBFBD>
int xl = 0, xr = 0; // x <20><><EFBFBD><EFBFBD>
yt = int(py);
yb = yt + 1;
xl = int(px);
xr = xl + 1;
// <20><><EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD>ص<EFBFBD>ֵ
signed short int DNlt = *(pDemPic->pDEMData + yt * pDemPic->iXSize + xl);
signed short int DNrt = *(pDemPic->pDEMData + yt * pDemPic->iXSize + xr);
signed short int DNlb = *(pDemPic->pDEMData + yb * pDemPic->iXSize + xl);
signed short int DNrb = *(pDemPic->pDEMData + yb * pDemPic->iXSize + xr);
// <20><><EFBFBD><EFBFBD>
double h, v;
h = py - yt;
v = px - xl;
fElevator = float(DNlt * (1 - h) * (1 - v) + DNlb * h * (1 - v)+ DNrt * (1 - h) * v + DNrb * h * v);
}
}
else
{
// <20><>Ե<EFBFBD><D4B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڷ<EFBFBD>ȡֵ
if ((px < 0) || (px > pDemPic->iXSize - 1) ||
(py < 0) || ( py > pDemPic->iYSize - 1))
{
signed short DNlt = *(pDemPic->pDEMData + ipy * pDemPic->iXSize + ipx);
fElevator = DNlt;
}
else
{
return false;
}
}
return true;
}
// <20><>ȡ<EFBFBD>߳<EFBFBD>
bool GetDem( float& fElevator, double dLonDeg, double dLatDeg, string sDir, bool bNearst)
{
// tif<69>ļ<EFBFBD> <20><><EFBFBD><EFBFBD>
string strName = GetDemName(dLonDeg, dLatDeg);
if (strName == "")
{
return false;
}
// tif<69>ļ<EFBFBD> ·<><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
string strPathName;
if (sDir != "")
{
strPathName = sDir + "\\" + strName;
}
else
{
return false;
}
// <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ڼ<EFBFBD>¼<EFBFBD><C2BC><EFBFBD>ҵ<EFBFBD>
int idx = FindDemDataSet(strPathName);
if (idx >= 0)
{
return GetDem(fElevator, dLonDeg, dLatDeg, g_DemPicList[idx], bNearst);;
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݼ<EFBFBD><DDBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD><D2B2><EFBFBD> <20><>ȡ<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD>ݼ<EFBFBD>
// ע<><D7A2>
GDALAllRegister();
CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
GDALDataset* tempdata = NULL;
tempdata = (GDALDataset*) GDALOpen(strPathName.c_str(), GA_ReadOnly);
if ( tempdata != NULL )
{
GDALDataType dataType = tempdata->GetRasterBand(1)->GetRasterDataType();
int iXSzie = tempdata->GetRasterBand(1)->GetXSize();
int iYSzie = tempdata->GetRasterBand(1)->GetYSize();
struDemPic* temp = new struDemPic();
temp->iXSize = iXSzie;
temp->iYSize = iYSzie;
tempdata->GetGeoTransform(temp->adfGeoTransform);
temp->pDEMData = new signed short int[iXSzie * iYSzie];
memset((void*) temp->pDEMData, 0, iXSzie * iYSzie * sizeof(signed short int));
tempdata->GetRasterBand(1)->RasterIO( GF_Read, 0, 0, iXSzie, iYSzie, temp->pDEMData, iXSzie, iYSzie, GDT_Int16, 0, 0 );
GDALClose(tempdata);
tempdata = NULL;
// <20><><EFBFBD><EFBFBD> <20>
temp->strDemName = strPathName;
g_DemPicList.push_back(temp);
if (g_DemPicList.size() > 9) // ͨ<><CDA8>һ<EFBFBD>η<EFBFBD><CEB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>򲻻ᳬ<F2B2BBBB><E1B3AC>3*3<><33>
{
delete [] g_DemPicList[0]->pDEMData;
g_DemPicList[0]->pDEMData = nullptr;
delete g_DemPicList[0];
g_DemPicList[0] = nullptr;
g_DemPicList.pop_front();
}
return GetDem(fElevator, dLonDeg, dLatDeg, g_DemPicList[g_DemPicList.size() - 1], bNearst);;
}
else
{
return false;
}
}