QGISDoc.cpp
资源名称:QGIS.rar [点击查看]
上传用户:oybseng
上传日期:2015-04-27
资源大小:7831k
文件大小:11k
源码类别:
GDI/图象编程
开发平台:
Visual C++
- // QGISDoc.cpp : implementation of the CQGISDoc class
- //
- #include "stdafx.h"
- #include "QGIS.h"
- #include "QGISDoc.h"
- #include "MainFrm.h"
- #include "ChildFrm.h"
- #include "QGISView.h"
- #include "QObjectsincludeQMapObj.h"
- #include "QObjectsincludeQLayerObj.h"
- #include "QObjectsincludeQCoordSys.h"
- #include "QObjectsincludeQPointObj.h"
- #include "QObjectsincludeQLineObj.h"
- #include "QObjectsincludeQGlobalObj.h"
- #include "QObjectsincludeResource.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- extern CQMapObj * g_pCurMap;
- /*extern CQGlobalObj g_QObj;*/
- /////////////////////////////////////////////////////////////////////////////
- // CQGISDoc
- IMPLEMENT_DYNCREATE(CQGISDoc, CDocument)
- BEGIN_MESSAGE_MAP(CQGISDoc, CDocument)
- //{{AFX_MSG_MAP(CQGISDoc)
- ON_COMMAND(ID_FILE_SAVE, OnFileSave)
- ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
- ON_COMMAND(IDM_MENU_EXPORT_POINTS_QGIS, OnMenuExportPointsQgis)
- ON_COMMAND(IDM_MENU_IMPORT_POINTS_QGIS, OnMenuImportPointsQgis)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CQGISDoc construction/destruction
- CQGISDoc::CQGISDoc()
- {
- // TODO: add one-time construction code here
- m_pMap = new CQMapObj;
- CQLayerObj * pLayer = new CQLayerObj;
- m_pMap->AddLayer(pLayer);
- m_pMap->SetCurLayer(pLayer);
- m_pMap->SetModifiedFlag(0);
- CQCoordSys * pSys = m_pMap->GetCoordSysPointer();
- pSys->SetMapScale(500.0);
- pSys->SetUnitType(0);
- m_pMap->GetBoundary()->m_fMinX = 0;
- m_pMap->GetBoundary()->m_fMinY = 0;
- m_pMap->GetBoundary()->m_fMaxX = 800.0;
- m_pMap->GetBoundary()->m_fMaxY = 600.0;
- int nPixelWidth = 5;
- double fL = 0;
- fL = m_pMap->GetCoordSysPointer()->LLtoWL(nPixelWidth);
- g_QObj.g_fEffectiveDistance = fL;
- g_QObj.g_pCoordSysObj = m_pMap->GetCoordSysPointer();
- }
- CQGISDoc::~CQGISDoc()
- {
- if(m_pMap)
- delete m_pMap;
- m_pMap = 0;
- }
- BOOL CQGISDoc::OnNewDocument()
- {
- if (!CDocument::OnNewDocument())
- return FALSE;
- // TODO: add reinitialization code here
- // (SDI documents will reuse this document)
- return TRUE;
- }
- /////////////////////////////////////////////////////////////////////////////
- // CQGISDoc serialization
- void CQGISDoc::Serialize(CArchive& ar)
- {
- if (ar.IsStoring())
- {
- // TODO: add storing code here
- }
- else
- {
- // TODO: add loading code here
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- // CQGISDoc diagnostics
- #ifdef _DEBUG
- void CQGISDoc::AssertValid() const
- {
- CDocument::AssertValid();
- }
- void CQGISDoc::Dump(CDumpContext& dc) const
- {
- CDocument::Dump(dc);
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CQGISDoc commands
- void CQGISDoc::OnFileSave()
- {
- // TODO: Add your command handler code here
- char szExt[] = "QGIS FILE (*.qgis)|*.qgis|All Files (*.*)|*.*||";
- char szFileName[] ="Map";
- CFileDialog fileDlg(FALSE,"*.qgis",szFileName,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,szExt);
- if(fileDlg.DoModal() == IDOK)
- {
- if(m_pMap)
- {
- CString szFilePath = fileDlg.GetFileName();
- m_pMap->Save((const char *)szFilePath);
- }
- }
- }
- void CQGISDoc::OnFileOpen()
- {
- // TODO: Add your command handler code here
- char szExt[] = "QGIS FILE (*.qgis)|*.qgis|All Files (*.*)|*.*||";
- char szFileName[] ="Map";
- CFileDialog fileDlg(TRUE,"*.qgis",szFileName,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,szExt);
- if(fileDlg.DoModal() == IDOK)
- {
- if(m_pMap)
- {
- CString szFilePath = fileDlg.GetFileName();
- m_pMap->Open((const char *)szFilePath);
- GetActiveView();
- g_QObj.g_pCoordSysObj = m_pMap->GetCoordSysPointer();
- if(m_pView)
- m_pView->InvalidateRect(NULL,TRUE);
- }
- }
- }
- CQGISView * CQGISDoc::GetActiveView()
- {
- CMainFrame * pFrame = (CMainFrame *)AfxGetApp()->m_pMainWnd;
- CChildFrame * pchild = (CChildFrame *)pFrame->GetActiveFrame();
- m_pView = (CQGISView *)pchild->GetActiveView();
- return m_pView;
- }
- //导出散点数据
- void CQGISDoc::OnMenuExportPointsQgis()
- {
- if(!m_pMap)
- {
- AfxMessageBox("地图不存在!");
- return;
- }
- CFileDialog dlg(FALSE,"xyz","*.xyz",OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
- "xyz Files (*.xyz)|*.xyz|All Files (*.*)|*.*||",NULL);
- if(dlg.DoModal()!=IDOK) return ;
- CString szFileName=CString(dlg.GetFileName());
- CStdioFile * pFile=new CStdioFile(szFileName,CFile::modeCreate | CFile::modeWrite|CFile::typeText);
- long lPtNum = 0;
- CString szPtNum;
- CString szPoint;
- szPtNum.Format("%10ld",lPtNum);
- pFile->WriteString(szPtNum);
- pFile->WriteString("n");
- int nLayerCount = m_pMap->GetLayerCount();
- for(int i=0;i<nLayerCount;i++)
- {
- CQLayerObj * pLayer = m_pMap->GetLayer(i);
- if(pLayer && !pLayer->GetDeleteFlag() && pLayer->GetShowFlag())
- {
- int nObjCount = pLayer->GetObjCount();
- for(int j=0;j<nObjCount;j++)
- {
- CQBaseObj * pObj = pLayer->GetObj(j);
- if(pObj && !pObj->GetObjDeleted() && !pObj->GetObjHided())
- {
- if(pObj->GetObjType() == QGIS_OBJ_POINT)
- {
- CQPointObj * pPoint = (CQPointObj *)pObj;
- double dx,dy;
- pPoint->GetPointPos(dx,dy);
- double fEle = pPoint->GetPtElevation();
- szPoint.Format("%10.4f,%10.4f,%10.4f",dx,dy,fEle);
- pFile->WriteString(szPoint);
- pFile->WriteString(CString("n"));
- lPtNum++;
- }
- else if(pObj->GetObjType() == QGIS_OBJ_LINE)
- {
- CQLineObj * pLine = (CQLineObj *)pObj;
- double fEle = pLine->GetElevation();
- int nPtNum = pLine->m_PtList.GetSize();
- if(nPtNum<=1)continue;
- lPtNum += nPtNum;
- for(int k=0;k<nPtNum;k++)
- {
- double dx = pLine->m_PtList.GetPoint(k)->GetX();
- double dy = pLine->m_PtList.GetPoint(k)->GetY();
- szPoint.Format("%10.4f,%10.4f,%10.4f",dx,dy,fEle);
- pFile->WriteString(szPoint);
- pFile->WriteString(CString("n"));
- }
- }
- }
- }
- }
- }
- pFile->SeekToBegin();
- szPtNum.Format("%10ld",lPtNum);
- pFile->WriteString(szPtNum);
- pFile->Close();
- delete pFile;
- }
- void CQGISDoc::OnMenuImportPointsQgis()
- {
- // TODO: Add your command handler code here
- //需要的元素 图幅指针,计算边界矩形,以保证显示,以默认方式创建创建Point对象
- //CStdioFile,CString等 现存问题:加入大批量数据后,无法显示
- static int nFileName = 1; //图层名递增 避免重名
- CString szLayerName;
- szLayerName.Format("导入散点层%d",nFileName++);
- if(!m_pMap)
- {
- AfxMessageBox("地图对象不存在!");
- return;
- }
- CQLayerObj * pLayer = new CQLayerObj;
- pLayer->SetLayerName(szLayerName); //新建图层 用于将读入的散点数据装入其中
- CQCoordSys * pCoordSys = m_pMap->GetCoordSysPointer(); //定义坐标系统对象,用户坐标转换
- ASSERT(pCoordSys);
- CFileDialog dlg(TRUE,"xyz","*.xyz",OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, //打开文件对话框
- "xyz Files (*.xyz)|*.xyz|All Files (*.*)|*.*||",NULL);
- if(dlg.DoModal()!=IDOK) return ;
- CString szFileName = CString(dlg.GetFileName()); //获取文件名
- BOOL bFinished = FALSE;
- CBoundaryRect bRect;
- long lLayerId = m_pMap->GetCurLayer()->GetLayerID();
- struct XYZ //自定义结构体,用于存储散点数据
- {
- double fX; //x坐标
- double fY; //y坐标
- double fH; //h高程
- };
- try
- { //为读入数据打开文件
- CStdioFile * pFile = new CStdioFile(szFileName,CFile::modeRead | CFile::typeText);
- if(!pFile)
- {
- throw new CFileException();
- }
- long lPtNum = 0; //初始化节点总数
- CString szPtNum = _T("");
- pFile->ReadString(szPtNum);
- szPtNum.TrimLeft(); //去除左边的所有前导空格
- if(szPtNum.IsEmpty()) lPtNum = 0;
- else if(szPtNum.Find(' ',0) > 0) lPtNum = 0;
- else lPtNum = atol((LPCTSTR)szPtNum); //这一步可以去掉读取的若干空格
- long lPointNum = 0;
- if(lPtNum == 0)
- {
- //如果读取的点数为零,不排除输出文件时没有正确导出点数的问题,所以可以计算一下.
- CString szTemp = _T("");
- pFile->SeekToBegin();
- while(pFile->ReadString(szTemp) != FALSE)
- {
- szTemp.TrimLeft(' ');
- if(!szTemp.IsEmpty())
- lPointNum++; //假如没有给出散点的数量则进行计算
- }
- lPtNum = lPointNum;
- pFile->SeekToBegin();
- }
- CWaitCursor wait;
- XYZ * tmXyz = new XYZ[lPtNum];
- CString szPtCoord = _T("");
- //将文件中的点位坐标全部读入临时结构数组
- if(lPointNum == 0 && lPtNum == 0)
- {
- AfxMessageBox("您选择的文件中没有任何数据,请确认!");
- return;
- }
- else
- {
- for(long i=0;i<lPtNum;i++)
- {
- if(FALSE == pFile->ReadString(szPtCoord)) throw new CFileException(); //若文件没有读取完全或读取成功则抛出异常
- CString szX = _T(""),szY = _T(""),szH = _T("");
- if(szPtCoord.GetAt(0) != ' ') //若读入的散点数据在外部被编辑过,则要进行相关的处理,
- {
- szPtCoord.TrimLeft();
- int nFirstPos = szPtCoord.Find(' ',0); //顺序查找第一个空格的位置
- int nSecendPos = szPtCoord.Find(' ',nFirstPos + 1); //顺序查找第二个空格的位置
- szX = szPtCoord.Left(nFirstPos);
- szY = szPtCoord.Mid(nFirstPos + 1,nSecendPos - nFirstPos);
- szPtCoord.Delete(0,nSecendPos + 1);
- szH = szPtCoord;
- }
- else
- {
- szX = szPtCoord.Left(10); //从最左端读取20个字符
- szY = szPtCoord.Mid(11,10); //从第21个位置开始读取20个字符
- szH = szPtCoord.Right(10); //从最右端读取20个字符
- }
- szX.TrimLeft(); //去除前导空格
- szY.TrimLeft();
- szH.TrimLeft();
- tmXyz[i].fX = atof((LPCTSTR)szX); //字符串转换为double型
- tmXyz[i].fY = atof((LPCTSTR)szY);
- tmXyz[i].fH = atof((LPCTSTR)szH);
- }
- CQPointObj * pPoint = NULL; //临时子图点对象,用于存放读入的点信息
- pLayer->CalculateBoundary(NULL); //计算图层的边界矩形
- for(long j = 0;j<lPtNum;j++)
- {
- CQPoint ptTemp(tmXyz[j].fX,tmXyz[j].fY);
- pPoint = new CQPointObj(ptTemp); //放大倍数为5倍
- pPoint->SetObjMapID(m_pMap->GetMapID());
- pPoint->SetObjLayerID(pLayer->GetLayerID());
- pPoint->SetPtElevation(tmXyz[j].fH);
- pPoint->CalculateBoundary(NULL); //计算点对象的边界矩形
- pLayer->AddObject(pPoint); //将点对象添加入图层
- }
- pLayer->CalculateBoundary(NULL);
- m_pMap->AddLayer(pLayer); //新图层加入图幅,并且予以显示
- m_pMap->SetCurLayer(pLayer);
- bFinished = TRUE;
- }
- delete tmXyz;
- pFile->SeekToBegin(); //文件指针回位
- pFile->Close();
- delete pFile;
- }
- catch (CFileException * e) //错误提示
- {
- UINT nMaxError = 100;
- TCHAR * pChError = new TCHAR[nMaxError];
- e->GetErrorMessage(pChError,nMaxError);
- CString szError = "读取文件过程中出现错误,可能是文件内容有问题或是其他原因造成:";
- szError += CString(pChError);
- AfxMessageBox(szError,MB_OK | MB_ICONWARNING);
- delete []pChError;
- delete pLayer;
- }
- if(bFinished)
- {
- m_pMap->SetModifiedFlag(1);
- GetActiveView();
- m_pMap->DisplayAllInView(pCoordSys,m_pView);
- }
- }