?? translate.cpp
字號(hào):
// Translate.cpp: implementation of the CTranslate class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "SvfToGeoMedia.h"
#include "Translate.h"
#include <map>
using namespace std;
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
extern list<CGeoObj*> G_GEOList;
extern ADOConn* G_pADOConn;
extern DGMConnectionPtr G_ptrDVIConnection;
extern DGMGeometryStorageServicePtr G_ptrGSS;
extern void SendMsg(CString str);
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CTranslate::CTranslate():m_strDbType(""),m_strAccess(""),m_strUser("")
,m_strPassWord(""),m_strDataBase(""),m_strSvfFile(""),m_strDB("")
{
m_pSvf=NULL;
m_pevent= NULL;
}
CTranslate::~CTranslate()
{
if (m_pSvf!=NULL)
{
try
{
if (m_pSvf->GetStatus())
m_pSvf->Close();
}catch (_com_error e)
{
AfxMessageBox(e.Description());
}
}
}
//初始化環(huán)境
bool CTranslate::Init()
{
//首先確保文件系統(tǒng)完成
/*
CFile file;
if (!file.Open("DataConv.mdb",CFile::modeRead))
{
AfxMessageBox("文件DataConv.mdb不存在!");
return false;
}
else
{
file.Close();
}
*/
//清理對(duì)象列表
list<CGeoObj*>::iterator item;
for( item = G_GEOList.begin();item!= G_GEOList.end();item++)
{
CGeoObj* pGeoObj = *item;
delete pGeoObj;
}
G_GEOList.clear();
//創(chuàng)建Access數(shù)據(jù)庫連接
if (G_pADOConn==NULL) G_pADOConn= new ADOConn;
if (!G_pADOConn->m_bConnect)
{
if (!G_pADOConn->OnInitADOAcess("DataConv.mdb"))
{
AfxMessageBox("連接Access數(shù)據(jù)庫DatConv.mdb失敗!");
return false;
}
}
//得到數(shù)據(jù)集對(duì)象
_bstr_t vSQL = "Select * from SvfToGeoMedia";
m_pSvf = G_pADOConn->GetRecordSet(vSQL);
//全局?jǐn)?shù)據(jù)存儲(chǔ)服務(wù)
G_ptrGSS.CreateInstance(__uuidof(GeometryStorageService));
//初始化地理據(jù)庫連接
//if (G_ptrDVIConnection!=NULL)
//{
//try
//{
// G_ptrDVIConnection->Disconnect();
//}
//catch (...) {
//}
m_bInit=true;
return true;
}
bool CTranslate::Translate()
{
//若沒有初始化先初始化
//if (!Init())
//{
// AfxMessageBox("初始化過程中出現(xiàn)錯(cuò)誤!");
// return false;
//}
//讀取SVF文件形成地理對(duì)象
try
{
m_svfReadFile.ReadFile(m_strSvfFile);
}catch (CException* e)
{
e->Delete();
SendMsg("讀"+m_strSvfFile+"文件過程中遇到意外錯(cuò)誤!");
AfxMessageBox("讀"+m_strSvfFile+"文件過程中遇到意外錯(cuò)誤!") ;
return false;
}
//得到所有地理對(duì)象地理數(shù)據(jù)表名
list<CGeoObj*>::iterator item;
for( item = G_GEOList.begin();item!= G_GEOList.end();item++)
{
if (WaitForSingleObject(m_pevent->m_hObject,0)== WAIT_OBJECT_0)
{
return false;
}
CGeoObj* pGeoObj = *item;
try
{
pGeoObj->GetTableName();
}catch(_com_error e)
{
AfxMessageBox(e.Description());
return false;
}
}
//檢查是否所有地理對(duì)象都有對(duì)象的地理表
map<CString,bool> geoCodemap;
for(item = G_GEOList.begin();item!= G_GEOList.end();item++)
{
if (WaitForSingleObject(m_pevent->m_hObject,0)== WAIT_OBJECT_0)
{
return false;
}
CGeoObj* pGeoObj = *item;
if (pGeoObj->m_strTable=="")
{
geoCodemap[pGeoObj->m_strGeoCode.c_str()]=false;
}
}
for(map<CString,bool>::iterator TbItem = geoCodemap.begin();TbItem!= geoCodemap.end();TbItem++)
{
CString strGeoCode = TbItem->first;
SendMsg("地理對(duì)象代碼為"+strGeoCode+"不存在!");
}
if (geoCodemap.size()>0)
return false;
//檢查表是否都存在
if (!CheckTbExists())
{
AfxMessageBox("請(qǐng)先創(chuàng)建地理對(duì)象表!");
return false;
}
//連接地理數(shù)據(jù)庫
if (!InitGeoDbConnect())
{
AfxMessageBox("GeoMedia連接數(shù)據(jù)庫失敗!");
return false;
}
//把地理對(duì)象列表中的對(duì)象寫到數(shù)據(jù)庫
for(item = G_GEOList.begin();item!= G_GEOList.end();item++)
{
if (WaitForSingleObject(m_pevent->m_hObject,0)== WAIT_OBJECT_0)
{
return false;
}
CGeoObj* pGeoObj = *item;
try
{
pGeoObj->WriteGeoObjToDataBase();
}catch (_com_error e)
{
AfxMessageBox(e.Description());
return false;
}
}
//關(guān)閉地理數(shù)據(jù)庫連接
G_ptrDVIConnection->Disconnect();
AfxMessageBox("轉(zhuǎn)換數(shù)據(jù)完成!");
return true;
}
//寫地理對(duì)象到地理數(shù)據(jù)庫中
bool CTranslate::WriteGeoToDataBase()
{
//list<CGeoObj*> G_GEOList;
try
{
for (list<CGeoObj*>::const_iterator item = G_GEOList.begin(); item!= G_GEOList.end();item++)
{
const CGeoObj* pGeoObj = *item;
}
}
catch(CException* e)
{
e->Delete();
AfxMessageBox("寫地理對(duì)象到數(shù)據(jù)庫時(shí)遇到錯(cuò)誤!");
return false;
}
}
//從數(shù)據(jù)庫中刪除地理對(duì)象集合
bool CTranslate::DelGeoObj()
{
//關(guān)閉地理數(shù)據(jù)連接
try
{
G_ptrDVIConnection->Disconnect();
}
catch (_com_error e)
{
AfxMessageBox(e.Description());
}
//建立ADOCON數(shù)據(jù)庫連接
ADOConn* pADOConn = new ADOConn;
m_strDbType.MakeUpper();
if (m_strDbType=="ACCESS")
{
pADOConn->OnInitADOAcess(m_strAccess);
}else if (m_strDbType == "ORACLE")
{
pADOConn->OnInitADOConnOra(m_strUser,m_strPassWord,m_strDataBase);
}else if (m_strDbType == "SQLSERVER")
{
pADOConn->OnInitADOConn(m_strUser,m_strPassWord,m_strDataBase,m_strDB);
}
CString strSQL;
list<CGeoObj*>::iterator item;
for(item = G_GEOList.begin();item!=G_GEOList.end();item++)
{
CGeoObj* pGeoObj = *item;
CString strtable = pGeoObj->m_strTable;
CString strID;
if (pGeoObj->m_nID>0)
{
strID.Format("%d",pGeoObj->m_nID);
strSQL = "Delete from "+ strtable+ "where ID="+strID;
pADOConn->ExecuteSQL(_bstr_t(strSQL));
}
}
return true;
}
//檢查表是否存在
bool CTranslate::CheckTbExists()
{
ADOConn* pADOConn=new ADOConn;
m_strDbType.MakeUpper();
if(m_strDbType=="ACCESS")
{
if (!pADOConn->OnInitADOAcess(m_strAccess))
{
AfxMessageBox("連接"+m_strAccess+"失敗!");
return false;
}
}
else if (m_strDbType=="ORACLE")
{
if (!pADOConn->OnInitADOConnOra(m_strUser,m_strPassWord,m_strDataBase))
{
AfxMessageBox("連接"+m_strDataBase+"數(shù)據(jù)庫失敗!");
return false;
}
} else if (m_strDbType=="SQLSERVER")
{
if (!pADOConn->OnInitADOConn(m_strUser,m_strPassWord,m_strDataBase,m_strDB))
{
AfxMessageBox("連接"+m_strDataBase+"數(shù)據(jù)庫失敗!");
return false;
}
}
map<CString,bool> Tbmap;
list<CGeoObj*>::iterator Item;
for (Item = G_GEOList.begin();Item!= G_GEOList.end();Item++)
{
if (WaitForSingleObject(m_pevent->m_hObject,0)== WAIT_OBJECT_0)
{
return false;
}
CGeoObj* pGeoObj = *Item;
CString strTable = pGeoObj->m_strTable;
_bstr_t vSQL = "select * from GFeatures where FeatureName ='"+ strTable+"'";
ADODB::_RecordsetPtr pDS = pADOConn->GetRecordSet(vSQL);
if (pDS->adoEOF)
{
Tbmap[strTable] = false;
}
pDS->Close();
}
for (map<CString,bool>::iterator item = Tbmap.begin();item!= Tbmap.end();item++)
{
CString strTbName = item->first;
SendMsg("地理數(shù)據(jù)庫沒有地理表"+strTbName+"存在!");
}
//清理數(shù)據(jù)庫連接對(duì)象
pADOConn->ExitConnect();
return (Tbmap.size()==0);
}
//創(chuàng)建地理數(shù)據(jù)庫連接
bool CTranslate::InitGeoDbConnect()
{
//初始化地理據(jù)庫連接
m_strDbType.MakeUpper();
try
{
if (G_ptrDVIConnection==NULL)
{
G_ptrDVIConnection.CreateInstance(__uuidof(Connection));
}
//Access數(shù)據(jù)庫連接
if (m_strDbType == "ACCESS")
{
//設(shè)置數(shù)據(jù)倉庫路徑
G_ptrDVIConnection->put_Location(_bstr_t(m_strAccess));
//數(shù)據(jù)倉庫類型
G_ptrDVIConnection->put_Type(L"Access.GDatabase");
G_ptrDVIConnection->Connect();
//Oracle數(shù)據(jù)庫連接
}else if (m_strDbType == "ORACLE")
{
//"用戶名/密碼@數(shù)據(jù)庫服務(wù)名"
CString strConnectInfo = m_strUser+"/"+m_strPassWord+"@"+m_strDataBase;
G_ptrDVIConnection->put_Location(_bstr_t("Oracle"));
G_ptrDVIConnection->PutType(_bstr_t("OracleORW.GDatabase"));
G_ptrDVIConnection->put_ConnectInfo(_bstr_t(strConnectInfo));
G_ptrDVIConnection->put_Mode(3);
G_ptrDVIConnection->Connect();
//SQLSERVER數(shù)據(jù)庫連接
}else if (m_strDbType == "SQLSERVER")
{
CString strConnectInfo = "Uid="+m_strUser+";Pwd="+m_strPassWord+";Database="+m_strDataBase+"; SERVER="+ m_strDB;
G_ptrDVIConnection->put_Location(_bstr_t("SQL Sever"));
G_ptrDVIConnection->PutType(_bstr_t("SQLServerRW.Gdatabase"));
G_ptrDVIConnection->put_ConnectInfo(_bstr_t(strConnectInfo));
G_ptrDVIConnection->put_Mode(3);
G_ptrDVIConnection->Connect();
}
}catch (_com_error e) {
AfxMessageBox(e.Description());
return false;
}
return true;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -