⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 geodataengine.cpp

📁 使用VC++程序制作的COM组件是地理信息系统的一个实例
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// GeoDataEngine.cpp: implementation of the CGeoDataEngine class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include <dbdao.h>							//DAO Classes
#include <dbdaoerr.h>						//DAO errors
#include "ComGIS.h"
#include "GeoDataEngine.h"

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

#define PointFsNum  9
const FIELDDEF PointFs[PointFsNum] =
{
	{_T("ID#"), dbLong, -1},
	{_T("byType#"), dbByte, -1},
	{_T("ptPositionX#"), dbDouble, -1},
	{_T("ptPositionY#"), dbDouble, -1},
	{_T("dElevation#"), dbDouble, -1},
	{_T("ptDirectionX#"), dbDouble, -1},
	{_T("ptDirectionY#"), dbDouble, -1},
	{_T("arrayLineID#"), dbLongBinary, -1},
	{_T("lAttriCode"), dbLong, -1}
};
#define LineFsNum  13
const FIELDDEF LineFs[LineFsNum] =
{
	{_T("ID#"), dbLong, -1},
	{_T("byType#"), dbByte, -1},
	{_T("lStartNodeID#"), dbLong, -1},
	{_T("lEndNodeID#"), dbLong, -1},
	{_T("lLeftPolygonID#"), dbLong, -1},
	{_T("lRightPolygonID#"),	dbLong,	-1},
	{_T("arrayVertex#"),	dbLongBinary, -1},
	{_T("arrayElevation#"), dbLongBinary, -1},
	{_T("rectBoundX1#"),	dbDouble, -1},
	{_T("rectBoundY1#"),	dbDouble, -1},
	{_T("rectBoundX2#"),	dbDouble, -1},
	{_T("rectBoundY2#"),	dbDouble, -1},
	{_T("lAttriCode"), dbDouble, -1}
};
#define PolygonFsNum  12
const FIELDDEF PolygonFs[PolygonFsNum] = 
{
	{_T("ID#"), dbLong, -1},
	{_T("byType#"), dbByte, -1},
	{_T("arrayLineID#"), dbLongBinary, -1},
	{_T("arrayIslandID#"), dbLongBinary, -1},
	{_T("lParentID#"), dbLong, -1},
	{_T("ptCentroidX#"), dbDouble, -1},
	{_T("ptCentroidY#"), dbDouble, -1},
	{_T("rectBoundX1#"), dbDouble, -1},
	{_T("rectBoundY1#"), dbDouble, -1},
	{_T("rectBoundX2#"), dbDouble, -1},
	{_T("rectBoundY2#"), dbDouble, -1},
	{_T("lAttriCode"), dbLong, -1}
};
#define TextFsNum  14
const FIELDDEF TextFs[TextFsNum] = 
{
	{_T("ID"), dbLong ,-1},
	{_T("ptPositionX"), dbDouble, -1},
	{_T("ptPositionY"), dbDouble, -1},
	{_T("csContent"), dbText, -1},
	{_T("Alignment"), dbByte, -1},
	{_T("dVerticalSpace"), dbDouble, -1},
	{_T("csFaceName"), dbText, 16},
	{_T("lEffect"), dbLong, -1},
	{_T("sizeCX"), dbLong, -1},
	{_T("sizeCY"), dbLong, -1},
	{_T("lCharExtra"), dbLong, -1},
	{_T("dAngle"), dbDouble, -1},
	{_T("rgbFore"), dbLong, -1},
	{_T("rgbBack"), dbLong, -1}
};
#define LabelFsNum  13
const FIELDDEF LabelFs[LabelFsNum] = 
{
//	{_T("ID"), dbLong, -1},
//	{_T("byType"), dbByte, -1},
	{_T("lGeoObjID"), dbLong, -1},
	{_T("csFieldName"), dbText, 32},
	{_T("csContent"), dbText, -1},
	{_T("arrayLocation"), dbLongBinary, -1},
	{_T("byPlaceStyle"), dbByte, -1},
	{_T("csFaceName"), dbText, 16},
	{_T("lEffect"), dbLong, -1},
	{_T("sizeCX"), dbLong, -1},
	{_T("sizeCY"), dbLong, -1},
	{_T("lCharExtra"), dbLong, -1},
	{_T("dAngle"), dbDouble, -1},
	{_T("rgbFore"), dbLong, -1},
	{_T("rgbBack"), dbLong, -1}
};
#define SymbolFsNum  6
const FIELDDEF SymbolFs[SymbolFsNum] = 
{
//	{_T("ID"), dbLong, -1},
//	{_T("byType"), dbByte, -1},
	{_T("lGeoObjID"), dbLong, -1},
	{_T("lCode"), dbLong, -1},
	{_T("dSize"), dbDouble, -1},
	{_T("rgbFore"), dbLong, -1},
	{_T("rgbColor"), dbLong, -1},
	{_T("dDistance"), dbDouble, -1}
};
#define LayerFsNum  8
const FIELDDEF LayerFs[LayerFsNum] = 
{
	{_T("csName"), dbText, 32},
	{_T("byType"), dbByte, -1},
	{_T("ptLocationX"), dbDouble, -1},
	{_T("ptLocationY"), dbDouble, -1},
	{_T("lScale"), dbLong, -1},
	{_T("dMinView"), dbDouble, -1},
	{_T("dMaxView"), dbDouble, -1},
	{_T("byVisible"), dbByte, -1},
};
#define WorkspaceFsNum  17
FIELDDEF WorkspaceFs[WorkspaceFsNum] = 
{
	{_T("csName"), dbText, 32},
	{_T("dtDate"), dbDate, -1},
	{_T("csLevel"), dbText, 5},
	{_T("dLongitude1"), dbDouble, -1},
	{_T("dLatitude1"), dbDouble, -1},
	{_T("dLongitude2"), dbDouble, -1},
	{_T("dLatitude2"), dbDouble, -1},
	{_T("x1"), dbDouble, -1},
	{_T("y1"), dbDouble, -1},
	{_T("x2"), dbDouble, -1},
	{_T("y2"), dbDouble, -1},
	{_T("byXYSystem"), dbByte, -1},
	{_T("byZSystem"), dbByte, -1},
	{_T("byProjection"), dbByte, -1},
	{_T("byUnit"), dbByte, -1},
	{_T("byStyle"), dbByte, -1},
	{_T("byDisplayProjection"), dbByte, -1}
};

WORKSPACEFEATURE comWorkspaceDefault;
LAYERFEATURE comLayerDefault;

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CGeoDataEngine::CGeoDataEngine()
{
	m_dbWorkspace = m_dbEngine.Workspaces[0L];
}

CGeoDataEngine::~CGeoDataEngine()
{
}
//---------------------------------------------------------------------------
//基本数据库操作函数
BOOL CGeoDataEngine::OpenDatabase(CString filename, CGeoDatabase* db)
//打开或建立数据库
//CString filename -- 数据库文件名
//CGeoDatabase& db  -- 返回的数据库对象
{
	CdbProperty proper;
	COleVariant v;
	CdbRecordset rs;
	CString dbp;

	try
	{
		*db = m_dbWorkspace.OpenDatabase(filename);
		v = (*db).Properties[_T("DatabaseProperty")].GetValue();
		dbp = (LPCTSTR)v.bstrVal;
		if (dbp != comDatabaseProperty)		//非ComGIS数据库文件
		{
			(*db).Close();
			return(FALSE);
		}
	}
	catch (CdbException e)
	{
		if (e.m_hr == E_DAO_FileNotFound)
		{
			//建立一个新的数据库并输入ComGIS数据库属性标志
			*db = m_dbWorkspace.CreateDatabase(filename, dbLangGeneral);
			proper = (*db).CreateProperty(_T("DatabaseProperty"), dbText,
									&COleVariant(comDatabaseProperty, VT_BSTRT));
			(*db).Properties.Append(proper);

			CreateWorkspaceTable(*db, comWorkspaceTableName);

			rs = (*db).TableDefs[comWorkspaceTableName].OpenRecordset(dbOpenTable);
			comWorkspaceDefault.csName = filename;
			comWorkspaceDefault.dtDate = COleDateTime::GetCurrentTime();
			comWorkspaceDefault.csLevel = _T("公开");
			comWorkspaceDefault.dLongitude1 = 0.0;	
			comWorkspaceDefault.dLatitude1 = 0.0;
			comWorkspaceDefault.dLongitude2 = 180.0;
			comWorkspaceDefault.dLatitude2 = 90.0;
			comWorkspaceDefault.x1 = 0.0;
			comWorkspaceDefault.y1 = 0.0;
			comWorkspaceDefault.x2 = 2147483647.0;
			comWorkspaceDefault.y2 = 2147483647.0;
			comWorkspaceDefault.byXYSystem = 2;
			comWorkspaceDefault.byZSystem = 1;	
			comWorkspaceDefault.byProjection = 0;
			comWorkspaceDefault.byUnit = 1;
			comWorkspaceDefault.byStyle = 0;
			comWorkspaceDefault.byDisplayProjection = 0;
			SetWorkspaceData(rs, &comWorkspaceDefault);
			rs.Update();
			rs.Close();
			//建立一个缺省要素层	
			CreateLayerTable(*db, comLayerTableName);
			comLayerDefault.csName = _T("要素层1");
			comLayerDefault.byType = 0;
			comLayerDefault.byVisible = 0;
			comLayerDefault.dMaxView = 0.0;
			comLayerDefault.dMinView = 0.0;
			comLayerDefault.lScale = 0;
			comLayerDefault.ptLocation.x = 0.0;
			comLayerDefault.ptLocation.y = 0.0;
			CreateNewLayer(*db, &comLayerDefault);
		}
		else
		{
			//错误处理
			CdbLastOLEError err;

			TRACE(_T("建立或打开数据库错误: %s\n"), (LPCTSTR)err.GetDescription());
		    return(FALSE);
		}
	}
	return(TRUE);
}//---------------------------------------------------------------------------
BOOL CGeoDataEngine::CreatePointTable(CGeoDatabase& db, CString tablename)
//建立点要素数据库表及空间数据字段
//CGeoDatabase& db    --	数据库对象
//CString tablename	--	数据库表名
{
	CdbTableDef td;
	CdbProperty proper;
	CdbField    fd;

	try
	{
		td = db.CreateTableDef(tablename);
		for (int i=0; i<PointFsNum; i++)
		{
			fd = td.CreateField(PointFs[i].csName, PointFs[i].lType, PointFs[i].lSize);
			if (i==0)  fd.SetAttributes(dbAutoIncrField);
			td.Fields.Append(fd);
		}
		db.TableDefs.Append(td);

		proper = td.CreateProperty(_T("TableType"), dbLong,
			                       &COleVariant((long)comPointTable));
		td.Properties.Append(proper);

		proper = td.CreateProperty(_T("SpatialFieldNum"), dbLong,
		                           &COleVariant(td.Fields.GetCount()));
		td.Properties.Append(proper);
	}
	catch (CdbException)
	{
		CdbLastOLEError err;

		TRACE(_T("建立点要素数据库表错误: %s\n"), (LPCTSTR)err.GetDescription());
		return(FALSE);
	}
	return(TRUE);
}

//---------------------------------------------------------------------------
BOOL CGeoDataEngine::CreateLineTable(CGeoDatabase& db, CString tablename)
//建立线要素数据库表及空间数据字段
//CGeoDatabase& db    --	数据库对象
//CString tablename	--	数据库表名
{
	CdbTableDef td;
	CdbProperty proper;
	CdbField    fd;

	try
	{
		td = db.CreateTableDef(tablename);
		for (int i=0; i<LineFsNum; i++)
		{
			fd = td.CreateField(LineFs[i].csName, LineFs[i].lType, LineFs[i].lSize);
			if (i==0)  fd.SetAttributes(dbAutoIncrField);
			td.Fields.Append(fd);
		}
		db.TableDefs.Append(td);

		proper = td.CreateProperty(_T("TableType"), dbLong,
			                       &COleVariant((long)comLineTable));
		td.Properties.Append(proper);

		proper = td.CreateProperty(_T("SpatialFieldNum"), dbLong,
  		                           &COleVariant(td.Fields.GetCount()));
		td.Properties.Append(proper);
	}
	catch (CdbException)
	{
		CdbLastOLEError err;

		TRACE(_T("建立线要素数据库表错误: %s\n"), (LPCTSTR)err.GetDescription());
		return(FALSE);
	}
	return(TRUE);
}
//---------------------------------------------------------------------------
BOOL CGeoDataEngine::CreatePolygonTable(CGeoDatabase& db, CString tablename)
//建立面要素数据库表及空间数据字段
//CGeoDatabase& db    --	数据库对象
//CString tablename	--	数据库表名
{
	CdbTableDef td;
	CdbProperty proper;
	CdbField    fd;

	try
	{
		td = db.CreateTableDef(tablename);
		for (int i=0; i<PolygonFsNum; i++)
		{
			fd = td.CreateField(PolygonFs[i].csName, PolygonFs[i].lType, PolygonFs[i].lSize);
			if (i==0)  fd.SetAttributes(dbAutoIncrField);
			td.Fields.Append(fd);
		}
		db.TableDefs.Append(td);

		proper = td.CreateProperty(_T("TableType"), dbLong,
			                       &COleVariant((long)comPolygonTable));
		td.Properties.Append(proper);

		proper = td.CreateProperty(_T("SpatialFieldNum"), dbLong,
			                       &COleVariant(td.Fields.GetCount()));
		td.Properties.Append(proper);
	}
	catch (CdbException)
	{
		CdbLastOLEError err;

		TRACE(_T("建立面要素数据库表错误: %s\n"), (LPCTSTR)err.GetDescription());
		return(FALSE);
	}
	return(TRUE);
}
//---------------------------------------------------------------------------
BOOL CGeoDataEngine::CreateTextTable(CGeoDatabase& db, CString tablename)
//建立文本要素数据库表
//CGeoDatabase& db    --	数据库对象
//CString tablename	--	数据库表名
{
	CdbTableDef td;
	CdbProperty proper;
	CdbField    fd;

	try
	{
		td = db.CreateTableDef(tablename);
		for (int i=0; i<TextFsNum; i++)
		{
			fd = td.CreateField(TextFs[i].csName, TextFs[i].lType, TextFs[i].lSize);
			if (i==0)  fd.SetAttributes(dbAutoIncrField);
			td.Fields.Append(fd);
		}
		db.TableDefs.Append(td);

		proper = td.CreateProperty(_T("TableType"), dbLong,
			                       &COleVariant((long)comTextTable));
		td.Properties.Append(proper);

	}
	catch (CdbException)
	{
		CdbLastOLEError err;

		TRACE(_T("建立文本要素数据库表错误: %s\n"), (LPCTSTR)err.GetDescription());
		return(FALSE);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -