📄 demdata.cpp
字号:
// DemData.cpp: implementation of the CDem class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Dem.h"
#include "DemData.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CDem::CDem( CProgressCtrl* pProgress )
{
m_X0 = 0 ;
m_Y0 = 0 ;
m_deltaX = 0 ;
m_deltaY = 0 ;
m_nRow = 0 ;
m_nColumn = 0 ;
m_nHZoom = 1 ;
m_pHList = NULL;
m_pProgress = pProgress;
}
CDem::~CDem()
{
FreeHList();
}
void CDem::ImportFromDemFile( const CString& strDemFile)
{
CStdioFile DemFile;
DemFile.Open( strDemFile, CFile::modeRead | CFile::typeText );
if(!ReadFileHead( DemFile ))
{
AfxMessageBox( "读取DEM文件头失败!" );
return;
}
if( m_pProgress )
{
m_pProgress->SetRange( 0,m_nRow );
//m_pProgress->SetStep(100);
}
FreeHList();
m_pHList = new (int(*[m_nRow]));
CString strBuf;
//每行10个值
int nCount = 0;
CStringArray strArray;
for(int i = 0; i < m_nRow ; i++ )
{
m_pHList[i] = new (int[m_nColumn]);
if( m_pProgress )
m_pProgress->SetPos( i );
for(int j=0 ; j < m_nColumn ; j++)
{
if( nCount == 0 )
{
DemFile.ReadString( strBuf );
nCount = DivideString(strBuf,strArray);
}
#ifdef _DEBUG
int nTemp = atoi(strArray[strArray.GetSize() - nCount]);
#endif
m_pHList[i][j] = atoi(strArray[strArray.GetSize() - nCount]);
nCount--;
}
}
DemFile.Close();
}
int CDem::DivideString(const CString& strBuf,CStringArray& strArray)
{
if( strArray.GetSize() )
strArray.RemoveAll();
CString strTempBuf = strBuf;
strTempBuf.TrimLeft();
strTempBuf.TrimRight();
int nLength = strTempBuf.GetLength();
CString strTemp;
for( int i = 0 ; i < nLength ; i++ )
{
if( strTempBuf[i] == ' ')
{
if( !strTemp.IsEmpty() )//去处连续空格
strArray.Add(strTemp);
strTemp = "" ;
}
else
strTemp += strTempBuf[i] ;
}
if( !strTemp.IsEmpty() )
strArray.Add(strTemp);
return strArray.GetSize();
}
void CDem::FreeHList()
{
if( !m_pHList ) return;
for (int i = 0 ; i < m_nRow ; i ++)
delete [](m_pHList[i]);
delete []m_pHList;
m_pHList = NULL;
}
void CDem::ExportToStrFile( const CString& strStrFile,int nFactor)
{
CStdioFile StrFile;
if( !StrFile.Open( strStrFile, CFile::modeCreate|CFile::modeWrite, NULL) )
{
AfxMessageBox( "str文件打开失败!");
return;
}
CString strBuf;
strBuf = "Created from DEM file\n";
StrFile.WriteString(strBuf);
strBuf = "3 VARIABLES\n";
StrFile.WriteString(strBuf);
strBuf = "East N 10 3\n";
StrFile.WriteString(strBuf);
strBuf = "North N 11 3\n";
StrFile.WriteString(strBuf);
strBuf = "RL N 8 2\n";
StrFile.WriteString(strBuf);
if( m_pProgress )
{
m_pProgress->SetRange( 0,m_nRow );
//m_pProgress->SetStep(100);
}
double dEast,dNorth;
// for(int i = 0; i < m_nRow ; i++ )
for(int i = 0; i < m_nRow ; i += nFactor )
{
if( m_pProgress )
m_pProgress->SetPos( i );
// for(int j=0 ; j < m_nColumn ; j++)
for(int j=0 ; j < m_nColumn ; j += nFactor )
{
dEast = m_X0 + j * m_deltaX ;
dNorth = m_Y0 - i * m_deltaY ;
strBuf.Format("%10.3lf%11.3lf%8.2lf\n",dEast ,dNorth,(float)m_pHList[i][j]/(float)m_nHZoom);
StrFile.WriteString(strBuf);
}
}
StrFile.Close();
}
BOOL CDem::ReadFileHead( CStdioFile& DemFile )
{
int nFlag = 0;
CString strBuf;
while (DemFile.ReadString(strBuf))
{
strBuf.MakeUpper();
int nIndex = strBuf.Find("X0:") ;
if( nIndex >= 0 )
{
strBuf = strBuf.Right(strBuf.GetLength() - nIndex - 3);
m_X0 = atof( strBuf );
nFlag++;
continue;
}
nIndex = strBuf.Find("Y0:") ;
if( nIndex >= 0 )
{
strBuf = strBuf.Right(strBuf.GetLength() - nIndex - 3);
m_Y0 = atof( strBuf );
nFlag++;
continue;
}
nIndex = strBuf.Find("DX:") ;
if( nIndex >= 0 )
{
strBuf = strBuf.Right(strBuf.GetLength() - nIndex - 3);
m_deltaX = (float)atof( strBuf );
nFlag++;
continue;
}
nIndex = strBuf.Find("DY:") ;
if( nIndex >= 0 )
{
strBuf = strBuf.Right(strBuf.GetLength() - nIndex - 3);
m_deltaY = (float)atof( strBuf );
nFlag++;
continue;
}
nIndex = strBuf.Find("ROW:") ;
if( nIndex >= 0 )
{
strBuf = strBuf.Right(strBuf.GetLength() - nIndex - 4);
m_nRow = atol( strBuf );
nFlag++;
continue;
}
nIndex = strBuf.Find("COLUMN:") ;
if( nIndex >= 0 )
{
strBuf = strBuf.Right(strBuf.GetLength() - nIndex - 7);
m_nColumn = atol( strBuf );
nFlag++;
continue;
}
nIndex = strBuf.Find("HZOOM:") ;
if( nIndex >= 0 )
{
strBuf = strBuf.Right(strBuf.GetLength() - nIndex - 6);
m_nHZoom = atol( strBuf );
nFlag++;
continue;
}
nIndex = strBuf.Find("MAXV:") ;
if( nIndex >= 0 ) break;
}
if( nFlag == 7 )
return TRUE;
else
return FALSE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -