用VC写读取DEM数据的程序,并画出图,不能读出数据,其中定义的变量 在调试的过程中没有看到其正确的赋值,请各位帮帮忙,很急,非常感谢

emily_korea 2012-12-17 03:00:51
//MyTestView.h :[/color] interface of the CMyTestView class
//
/////////////////////////////////////////////////////////////////////////////

#if !defined(AFX_MYTESTVIEW_H__70C5DCC2_42A2_48AB_88A9_565CDF18CC90__INCLUDED_)
#define AFX_MYTESTVIEW_H__70C5DCC2_42A2_48AB_88A9_565CDF18CC90__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000


class CMyTestView : public CView
{
protected: // create from serialization only
CMyTestView();
DECLARE_DYNCREATE(CMyTestView)

// Attributes
public:
CMyTestDoc* GetDocument();

// Operations
public:

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMyTestView)
public:
virtual void OnDraw(CDC* pDC); // overridden to draw this view
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
virtual void OnPrepareDC(CDC* pDC, CPrintInfo* pInfo = NULL);
protected:
virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
//}}AFX_VIRTUAL

// Implementation
public:
void DrawDEM(CDC*pDC);

BOOL m_bDrawDem;//是否绘制DEM
float m_fLeftTopX;//DEM左上角x坐标
float m_fLeftTopY;//DEM左上角y坐标
float m_fGridX; //DEM x方向网格边长
float m_fGridY; //DEM y方向网格边长
int m_nRow;//DEM y方向网格数(行数)
int m_nCol;//DEM x方向网格数(列数)
int m_nScale;//DEM z值放大倍数
int* m_pEle;//高程矩阵指针
int i,j,k;
long nRowPos;
int nCharRowofCol;
int nCurZ[10];
int nFields;
int q,m;
char s;



char message[300];

BOOL OpenFile(char* szFile);
BOOL m_bPress;
int myFunction(int a,int b,float c);
virtual ~CMyTestView();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif

protected:

// Generated message map functions
protected:
//{{AFX_MSG(CMyTestView)
afx_msg void OnDrawLine();
afx_msg void OnReadDem();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

#ifndef _DEBUG // debug version in MyTestView.cpp
inline CMyTestDoc* CMyTestView::GetDocument()
{ return (CMyTestDoc*)m_pDocument; }
#endif

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_MYTESTVIEW_H__70C5DCC2_42A2_48AB_88A9_565CDF18CC90__INCLUDED_)




// MyTestView.cpp : implementation of the CMyTestView class
//

#include "stdafx.h"
#include "MyTest.h"

#include "MyTestDoc.h"
#include "MyTestView.h"

#include <fstream.h>
#include <math.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMyTestView

IMPLEMENT_DYNCREATE(CMyTestView, CView)

BEGIN_MESSAGE_MAP(CMyTestView, CView)
//{{AFX_MSG_MAP(CMyTestView)
ON_COMMAND(ID_DRAW_LINE, OnDrawLine)
ON_COMMAND(ID_READ_DEM, OnReadDem)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyTestView construction/destruction

CMyTestView::CMyTestView()
{
// TODO: add construction code here
m_bPress=FALSE;

m_pEle=NULL;//高程指针初始化

m_bDrawDem=FALSE;//程序运行的最初不绘制,当打开dem文件后再绘制
}

//退出程序时被地动调用
CMyTestView::~CMyTestView()
{
if(m_pEle)//释放指针空间
{
delete[] m_pEle;
m_pEle=NULL;
}
}

BOOL CMyTestView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs

return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMyTestView drawing

void CMyTestView::OnDraw(CDC* pDC)
{
CMyTestDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
if(m_bPress)
{
pDC->MoveTo (0,0);
pDC->LineTo(10000,18000);
}

if(m_bDrawDem)
DrawDEM(pDC);
}

/////////////////////////////////////////////////////////////////////////////
// CMyTestView printing

BOOL CMyTestView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}

void CMyTestView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}

void CMyTestView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CMyTestView diagnostics

#ifdef _DEBUG
void CMyTestView::AssertValid() const
{
CView::AssertValid();
}

void CMyTestView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}

CMyTestDoc* CMyTestView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyTestDoc)));
return (CMyTestDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMyTestView message handlers

void CMyTestView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo)
{
// TODO: Add your specialized code here and/or call the base class
CRect rc;
GetClientRect(&rc); //获得视口矩形
int nWidth = 10000; //窗口大小
int nHeight = 18000;
pDC->SetMapMode(MM_ANISOTROPIC); //映射模式
pDC->SetViewportOrg(rc.left,rc.bottom); //视口原点
pDC->SetWindowOrg(0,0); //窗口原点
pDC->SetViewportExt(rc.Width(),-rc.Height()); //视口宽、高
pDC->SetWindowExt(nWidth,nHeight); //窗口宽高

CView::OnPrepareDC(pDC, pInfo);
}

int CMyTestView::myFunction(int a, int b, float c)
{
int x,y;
return 0;
}

void CMyTestView::OnDrawLine()
{
// TODO: Add your command handler code here
m_bPress=TRUE;
Invalidate();
}

void CMyTestView::OnReadDem()
{
// TODO: Add your command handler code here
//OpenFile("D:\\40803.DEM");

m_bDrawDem=OpenFile("D:\\40803.DEM");
if(m_bDrawDem)
Invalidate();
}

BOOL CMyTestView::OpenFile(char *szFile)
{
ifstream is(szFile);
if(is.bad())
return FALSE;

is.seekg(0,ios::beg);
for (;;)
{
char message[200];
is.getline(message,sizeof(message)-1,'\n');

CString s=message;
s.TrimLeft();
s.TrimRight();
int nPos=s.Find(":");
int nLen=s.GetLength();
if (nPos==-1)
continue;

CString s1=s.Left(nPos);
CString s2=s.Right(nLen-nPos-1);
if (s1.CompareNoCase("DataMark")==0)
{
if (s2.CompareNoCase("CNSDTF-DEM")!=0)
{
AfxMessageBox("当前DEM文件非国家空间数据交换格式!");
return FALSE;
}
}
else if(s1.CompareNoCase("Version")==0)//版本号判别
{

}
else if (s1.CompareNoCase("Unit")==0)//坐标单位
{

}
else if (s1.CompareNoCase("Alpha")==0)
{

}
else if(s1.CompareNoCase("Compress")==0)
{

}
else if (s1.CompareNoCase("X0")==0)//左上角x坐标
m_fLeftTopX=atof(s2);
else if (s1.CompareNoCase("Y0")==0)//y坐标
m_fLeftTopY=atof(s2);
else if (s1.CompareNoCase("DX")==0)
m_fGridX=atof(s2);
else if (s1.CompareNoCase("DY")==0)
m_fGridY=atof(s2);
else if (s1.CompareNoCase("Row")==0)//dem行数
m_nRow=atoi(s2);
else if (s1.CompareNoCase("Col")==0)
m_nCol=atoi(s2);
else if(s1.CompareNoCase("ValueType")==0)//高程值类型
{

}
else if(s1.CompareNoCase("Hzoom")==0)
{
m_nScale=atoi(s2);
break;
}
}

if(fabs(m_fGridX-m_fGridY)>0.001)
{
AfxMessageBox("系统要求DEM为正方形网格!");
return FALSE;
}

///////////////////////////////////////////////////////
//以下为高程指针m_pEle分配空间,并读取高程值
m_pEle=new int[m_nRow*m_nCol];
int i,j,k;
for (i=0;i<m_nRow;i++)
{
//按行循环,DEM从左上角起算,所以行数从上到下增加
LONG nRowPos=i*m_nCol;//二维格网所对应的一维序号(From 0)
//计算DEM中的一行在文件中的存储行数 nCharRowofCol
int nCharRowofCol;
if (m_nCol%10==0)
nCharRowofCol=m_nCol/10;
else
nCharRowofCol=m_nCol/10+1;
int nCurZ[10];
for (j=0;j<nCharRowofCol;j++)
{
is.getline(message,sizeof(message)-1,'\n');//HEADER
int nFields=sscanf(message,"%d%d%d%d%d%d%d%d%d%d",
&nCurZ[0],&nCurZ[1],&nCurZ[2],&nCurZ[3],&nCurZ[4],
&nCurZ[5],&nCurZ[6],&nCurZ[7],&nCurZ[8],&nCurZ[9]);
int k;
for (k=0;k<nFields;k++)
{
int nPos_of_Point;//当前点在整个DEM网格中的位置
nPos_of_Point=nRowPos+j*10+k;
m_pEle[nPos_of_Point]=nCurZ[k];
}
}

}

///////////////////////////////////////////////////////

//is.getline(message,sizeof(message)-1,'\n');

/*LONG nCurZ[10];
int nFields=sscanf(message,"%d%d%d%d%d%d%d%d%d%d",
&nCurZ[0],&nCurZ[1],&nCurZ[2],&nCurZ[3],&nCurZ[4],
&nCurZ[5],&nCurZ[6],&nCurZ[7],&nCurZ[8],&nCurZ[9]);*/



is.close();
return TRUE;
}



void CMyTestView::DrawDEM(CDC*pDC)
{
//这里写绘制DEM的程序,我还没写,但是之前调试了,结果是没办法读DEM数据,请各位帮帮忙
}
...全文
257 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
u010313217 2014-01-11
  • 打赏
  • 举报
回复
同喜同喜!!
annahome2013 2013-04-27
  • 打赏
  • 举报
回复
我也正在弄这个是用VC++来 DEM
emily_korea 2012-12-17
  • 打赏
  • 举报
回复
这是调试的时候出现的
emily_korea 2012-12-17
  • 打赏
  • 举报
回复
补充一下DEM文件:

64,281

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧