📄 translate.cpp
字号:
// 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());
}
}
}
//初始化环境
bool CTranslate::Init()
{
//首先确保文件系统完成
/*
CFile file;
if (!file.Open("DataConv.mdb",CFile::modeRead))
{
AfxMessageBox("文件DataConv.mdb不存在!");
return false;
}
else
{
file.Close();
}
*/
//清理对象列表
list<CGeoObj*>::iterator item;
for( item = G_GEOList.begin();item!= G_GEOList.end();item++)
{
CGeoObj* pGeoObj = *item;
delete pGeoObj;
}
G_GEOList.clear();
//创建Access数据库连接
if (G_pADOConn==NULL) G_pADOConn= new ADOConn;
if (!G_pADOConn->m_bConnect)
{
if (!G_pADOConn->OnInitADOAcess("DataConv.mdb"))
{
AfxMessageBox("连接Access数据库DatConv.mdb失败!");
return false;
}
}
//得到数据集对象
_bstr_t vSQL = "Select * from SvfToGeoMedia";
m_pSvf = G_pADOConn->GetRecordSet(vSQL);
//全局数据存储服务
G_ptrGSS.CreateInstance(__uuidof(GeometryStorageService));
//初始化地理据库连接
//if (G_ptrDVIConnection!=NULL)
//{
//try
//{
// G_ptrDVIConnection->Disconnect();
//}
//catch (...) {
//}
m_bInit=true;
return true;
}
bool CTranslate::Translate()
{
//若没有初始化先初始化
//if (!Init())
//{
// AfxMessageBox("初始化过程中出现错误!");
// return false;
//}
//读取SVF文件形成地理对象
try
{
m_svfReadFile.ReadFile(m_strSvfFile);
}catch (CException* e)
{
e->Delete();
SendMsg("读"+m_strSvfFile+"文件过程中遇到意外错误!");
AfxMessageBox("读"+m_strSvfFile+"文件过程中遇到意外错误!") ;
return false;
}
//得到所有地理对象地理数据表名
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;
}
}
//检查是否所有地理对象都有对象的地理表
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("地理对象代码为"+strGeoCode+"不存在!");
}
if (geoCodemap.size()>0)
return false;
//检查表是否都存在
if (!CheckTbExists())
{
AfxMessageBox("请先创建地理对象表!");
return false;
}
//连接地理数据库
if (!InitGeoDbConnect())
{
AfxMessageBox("GeoMedia连接数据库失败!");
return false;
}
//把地理对象列表中的对象写到数据库
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;
}
}
//关闭地理数据库连接
G_ptrDVIConnection->Disconnect();
AfxMessageBox("转换数据完成!");
return true;
}
//写地理对象到地理数据库中
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("写地理对象到数据库时遇到错误!");
return false;
}
}
//从数据库中删除地理对象集合
bool CTranslate::DelGeoObj()
{
//关闭地理数据连接
try
{
G_ptrDVIConnection->Disconnect();
}
catch (_com_error e)
{
AfxMessageBox(e.Description());
}
//建立ADOCON数据库连接
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+"数据库失败!");
return false;
}
} else if (m_strDbType=="SQLSERVER")
{
if (!pADOConn->OnInitADOConn(m_strUser,m_strPassWord,m_strDataBase,m_strDB))
{
AfxMessageBox("连接"+m_strDataBase+"数据库失败!");
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("地理数据库没有地理表"+strTbName+"存在!");
}
//清理数据库连接对象
pADOConn->ExitConnect();
return (Tbmap.size()==0);
}
//创建地理数据库连接
bool CTranslate::InitGeoDbConnect()
{
//初始化地理据库连接
m_strDbType.MakeUpper();
try
{
if (G_ptrDVIConnection==NULL)
{
G_ptrDVIConnection.CreateInstance(__uuidof(Connection));
}
//Access数据库连接
if (m_strDbType == "ACCESS")
{
//设置数据仓库路径
G_ptrDVIConnection->put_Location(_bstr_t(m_strAccess));
//数据仓库类型
G_ptrDVIConnection->put_Type(L"Access.GDatabase");
G_ptrDVIConnection->Connect();
//Oracle数据库连接
}else if (m_strDbType == "ORACLE")
{
//"用户名/密码@数据库服务名"
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数据库连接
}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;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -