?? environment.cpp
字號:
//-----------------------------------------------------------------------------------------
// 根據地圖名稱得到當前地圖在地圖集合中的索引
int CEnvironment::GetMapIndex(CString szMapName)
{
int nIndex = -1;
for (int i = 0; i<m_nMapNum; i ++)
{
if (szMapName == m_mapInfos[i].szName)
{
// 如果地圖名稱等于輸入的參數,則表示找到
nIndex = i;
// 退出循環
break;
}
}
return nIndex;
}
//-----------------------------------------------------------------------------------------
// 功能:計算地圖比例尺
// 返回值:地圖比例尺
double CEnvironment::CalcScale(CMap1* map, CMoRectangle extent)
{
HWND hWnd = (HWND)map->GetHWnd();
HDC hDC = GetDC(hWnd);
double dpix = GetDeviceCaps(hDC, LOGPIXELSX);
MPoint* pts = new MPoint[2];
pts[0].x = extent.GetLeft();
pts[0].y = extent.GetTop();
pts[1].x = extent.GetRight();
pts[1].y = extent.GetTop();
CRect rect;
map->GetWindowRect(&rect);
double dLen1 = CalcLenght(pts,2);
double dLen2 = rect.Width() / dpix * 2.54 /100;
delete pts;
pts = NULL;
return dLen1 / dLen2;
}
//-----------------------------------------------------------------------------------------
void CEnvironment::ExecuteSpatialByPoint(CMap1* map, CMoPoint shape)
{
for (int i = 0; i<m_nLayerNum; i ++)
{
// 先計算地圖顯示比例尺
double dScale = CalcScale(map);
if (dScale > 8000)
{
dScale = dScale/10000;
dScale = dScale / 5000;
}
else
{
dScale = dScale/10000;
dScale = dScale / 2500;
}
if (m_layerInfos[i].layer.GetVisible() && m_layerInfos[i].bCanSelected)
m_layerInfos[i].rsSel = m_layerInfos[i].layer.SearchByDistance(shape.m_lpDispatch, dScale,"");
else
m_layerInfos[i].rsSel = NULL;
}
}
//-----------------------------------------------------------------------------------------
void CEnvironment::ExecuteSpatialByRect(CMoRectangle shape, int sMode)
{
for (int i = 0; i<m_nLayerNum; i ++)
{
if (m_layerInfos[i].layer.GetVisible() && m_layerInfos[i].bCanSelected)
m_layerInfos[i].rsSel = m_layerInfos[i].layer.SearchShape(shape.m_lpDispatch, sMode,"");
else
m_layerInfos[i].rsSel = NULL;
}
}
//-----------------------------------------------------------------------------------------
void CEnvironment::ExecuteSpatialByPolygon(CMoPolygon shape, int sMode)
{
for (int i = 0; i<m_nLayerNum; i ++)
{
if (m_layerInfos[i].layer.GetVisible() && m_layerInfos[i].bCanSelected)
m_layerInfos[i].rsSel = m_layerInfos[i].layer.SearchShape(shape.m_lpDispatch, sMode,"");
else
m_layerInfos[i].rsSel = NULL;
}
}
//-----------------------------------------------------------------------------------------
// 高亮度繪制選擇的地物
void CEnvironment::DrawRecordset(CMap1* map)
{
for (int i = 0; i < m_nLayerNum; i ++)
{
if (m_layerInfos[i].layer.GetVisible())
{
if (m_layerInfos[i].rsSel != NULL)
{
// 創建一符號對象
CMoSymbol sym;
sym.CreateDispatch(_T("MapObjects2.Symbol"));
sym.SetSymbolType(m_layerInfos[i].layer.GetSymbol().GetSymbolType());
sym.SetStyle(m_layerInfos[i].layer.GetSymbol().GetStyle());
sym.SetSize(m_layerInfos[i].layer.GetSymbol().GetSize());
sym.SetColor(0xff);
if (m_layerInfos[i].nCharacterIndex >= 0 &&
m_layerInfos[i].layer.GetShapeType() == moShapeTypePoint)
{
sym.SetSymbolType(moPointSymbol);
sym.GetFont().SetName(m_layerInfos[i].szFontName);
sym.SetStyle(4);
sym.SetSize(m_layerInfos[i].layer.GetSymbol().GetSize());
sym.SetCharacterIndex(m_layerInfos[i].nCharacterIndex);
}
// 移動到第一條記錄
m_layerInfos[i].rsSel.MoveFirst();
while (!m_layerInfos[i].rsSel.GetEof())
{
CMoField tempField = m_layerInfos[i].rsSel.GetFields().Item(COleVariant("Shape"));
// 用構建的符號對象繪制地物
map->DrawShape(tempField.GetValue().pdispVal,sym);
// 移動到下一條記錄
m_layerInfos[i].rsSel.MoveNext();
}
}
}
}
}
//-----------------------------------------------------------------------------------------
void CEnvironment::ClearSelRsts()
{
for (int i = 0; i < m_nLayerNum; i ++)
{
m_layerInfos[i].rsSel = NULL;
}
}
//-----------------------------------------------------------------------------------------
void CEnvironment::DrawSelectedShape(CMap1* map)
{
CMainFrame* pMainWnd = (CMainFrame*)AfxGetMainWnd();
CNameGisView* pMapView = (CNameGisView*)(pMainWnd->m_wndSplitter.GetPane(0,0));
CMoSymbol sym;
sym.CreateDispatch(_T("MapObjects2.Symbol"));
sym.SetColor(0xff);
if (m_selectedSymbol == NULL)
map->DrawShape(m_selectedFeature,sym );
else
{
double dScale = CalcScale(map);
m_selectedSymbol.SetSize(pMapView->ReCalcFontSize(m_selectedSymbolSize, dScale));
map->DrawShape(m_selectedFeature, m_selectedSymbol );
}
}
//-----------------------------------------------------------------------------------------
// 從指定表中得到指定的地名對應的數據表
// 參數szName指需要查找其對應數據表的地名名稱
// 參數szTableName表示從該表中來查找地名對應的數據表名稱
// 返回值為數據表名稱
CString CEnvironment::GetTableName(CString szName, CString szTbleName)
{
CDaoDatabase* tmpDB = new CDaoDatabase;
try
{
tmpDB->Open(m_szDBName);
}
catch (CDaoException* e)
{
DisplayDaoException(e);
delete tmpDB;
e->Delete();
return "";
}
CString szTableName = "";
CDaoRecordset rs(tmpDB);
try
{
CString szSQL;
szSQL = "Select * From " + szTbleName + " Where 名稱 ='"+szName+"'";
rs.Open(dbOpenDynaset, szSQL);
if(rs.GetRecordCount() != 0)
{
COleVariant var;
var = rs.GetFieldValue("屬性表名");
szTableName = CCrack::strVARIANT(var);
}
}
catch (CDaoException* e)
{
DisplayDaoException(e);
tmpDB->Close();
delete tmpDB;
e->Delete();
return "";
}
if(tmpDB)
{
if(tmpDB->IsOpen())
{
tmpDB->Close();
}
delete tmpDB;
tmpDB = NULL;
}
return szTableName;
}
//-----------------------------------------------------------------------------------------
// 根據地名得到地名所在的圖層名
// <param name="szName">string 地名名稱</param>
// <param name="szTblName">string 索引表名稱</param>
// <returns>圖層名稱</returns>
CString CEnvironment::GetLayerName(CString szName, CString szTblName)
{
CDaoDatabase* tmpDB = new CDaoDatabase;
try
{
tmpDB->Open(m_szDBName);
}
catch (CDaoException* e)
{
DisplayDaoException(e);
delete tmpDB;
e->Delete();
return "";
}
CString szTableName = "";
CDaoRecordset rs(tmpDB);
try
{
CString szSQL;
szSQL = "Select * From " + szTblName + " Where 名稱 ='"+szName+"'";
rs.Open(dbOpenDynaset, szSQL);
if(rs.GetRecordCount() != 0)
{
COleVariant var;
var = rs.GetFieldValue("圖層名");
szTableName = CCrack::strVARIANT(var);
}
}
catch (CDaoException* e)
{
DisplayDaoException(e);
tmpDB->Close();
delete tmpDB;
e->Delete();
return "";
}
if(tmpDB)
{
if(tmpDB->IsOpen())
{
tmpDB->Close();
}
delete tmpDB;
tmpDB = NULL;
}
return szTableName;
}
//-----------------------------------------------------------------------------------------
// 查詢某點指定距離范圍內的地物
// 參數dX表示某點的X坐標
// 參數dY表示某點的Y坐標
// 參數dDistance表示查詢半徑
// 參數pListBox表示將查詢得到的地名顯示在該列表框中
// 返回值為查詢得到的地物書目
long CEnvironment::SearchByDistance(double dX, double dY,double dDistance, CListBox* pListBox)
{
bool bClosest = false;
if (dDistance <= 0.0)
{
bClosest = true;
dDistance = 1000000000;
}
pListBox->ResetContent();
for (int i = 0; i < m_nLayerNum; i ++)
{
if (m_layerInfos[i].bSelected
&& m_layerInfos[i].layer.GetShapeType() == moShapeTypePoint)
{
CMoRecordset rs;
CString szPlaceName = "";
double dMinDist = -1.0;
double dDist = 0.0;
rs = m_layerInfos[i].layer.GetRecords();
if (rs)
{
rs.MoveFirst();
while (!rs.GetEof())
{
CMoFields fields(rs.GetFields());
CMoField shapeField(fields.Item(COleVariant(TEXT("Shape"))));
CMoPoint pt(shapeField.GetValue().pdispVal);
MPoint pts[2];
pts[0].x = dX;
pts[0].y = dY;
pts[1].x = pt.GetX();
pts[1].y = pt.GetY();
dDist = CalcLenght(pts, 2);
if (dDistance >= CalcLenght(pts, 2))
{
if (bClosest)
{
//查找最近
CString szTemp = rs.GetFields().Item(COleVariant("名稱")).GetValueAsString();
if (szTemp == "")
continue;
if (((dMinDist < 0) || (dMinDist > dDist))
&& (m_szPlaceName != szTemp))
{
dMinDist = dDist;
szPlaceName = szTemp;
}
}
else
{
pListBox->AddString(rs.GetFields().Item(COleVariant("名稱")).GetValueAsString());
}
}
rs.MoveNext();
}
if (bClosest && (szPlaceName != "")
&& (m_szPlaceName != szPlaceName) )
pListBox->AddString(szPlaceName);
}
}
else
{
m_layerInfos[i].rsSel = NULL;
}
}
return pListBox->GetCount();
}
//-----------------------------------------------------------------------------------------
// 功能:得到公交線路上車站
// 參數:PathNode node 乘車路線結構
// nIndex 第幾次換乘
// Buses buses 車站數組
// int nCount 車站數目
// 返回值:true 成功
BOOL CEnvironment::GetStation(PathNode* node, int nIndex, Buses* buses, int* nCount)
{
PathNode* line = node;
int nOrder1, nOrder2;
nOrder1 = GetStationOrder(line->szRoutineName[nIndex], line->szFromStationName[nIndex]);
nOrder2 = GetStationOrder(line->szRoutineName[nIndex], line->szToStationName[nIndex]);
if ((nOrder1 < 0) || (nOrder2 < 0))
return FALSE;
CDaoDatabase* tmpDB = new CDaoDatabase;
try
{
tmpDB->Open(m_szDBName);
}
catch (CDaoException* e)
{
DisplayDaoException(e);
delete tmpDB;
e->Delete();
return FALSE;
}
CString szTableName = "";
CDaoRecordset rs(tmpDB);
try
{
CString szSQL;
char buf1[20], buf2[20];
itoa(nOrder1, buf1, 10);
itoa(nOrder2, buf2, 10);
szSQL = "Select * From 公交車站路線 Where 線路名 ='" + line->szRoutineName[nIndex] + "' And 順序 Between " + CString(buf1) + " And " + CString(buf2);
rs.Open(dbOpenDynaset, szSQL);
if(rs.GetRecordCount() == 0)
return FALSE;
COleVariant var;
while(!rs.IsEOF())
{
var = rs.GetFieldValue("站名");
CString szStation = CCrack::strVARIANT(var);
if (!GetStationPt(szStation, &(buses->pts[*nCount])))
{
buses->pts[*nCount].x = -1;
buses->pts[*nCount].y = -1;
}
*nCount ++;
rs.MoveNext();
}
rs.Close();// 關閉數據表
}
catch (CDaoException* e)
{
DisplayDaoException(e);
delete tmpDB;
e->Delete();
return FALSE;
}
if(tmpDB)
{
if(tmpDB->IsOpen())
{
tmpDB->Close();
}
delete tmpDB;
tmpDB = NULL;
}
return TRUE;
}
//-----------------------------------------------------------------------------------------
//功能:得到公交線路上車站
//參數:string szLineName 公交線路名
// Buses buses 車站數組
// int nCount 車站數目
//返回值:true 成功
BOOL CEnvironment::GetStation(CString szLineName, Buses* pBuses, int* nCount)
{
CDaoDatabase* tmpDB = new CDaoDatabase;
try
{
tmpDB->Open(m_szDBName);
}
catch (CDaoException* e)
{
DisplayDaoException(e);
delete tmpDB;
e->Delete();
return FALSE;
}
CString szTableName = "";
CDaoRecordset rs(tmpDB);
try
{
CString szSQL;
szSQL = "Select * From 公交車站路線 Where 線路名 ='"+szLineName +"'" ;
rs.Open(dbOpenDynaset, szSQL);
if (0 == rs.GetRecordCount())
return FALSE;
COleVariant var;
while(!rs.IsEOF())
{
var = rs.GetFieldValue("站名");
CString szStation = CCrack::strVARIANT(var);
if (!GetStationPt(szStation, &(pBuses->pts[*nCount])))
{
pBuses->pts[*nCount].x = -1;
pBuses->pts[*nCount].y = -1;
}
(*nCount) ++;
rs.MoveNext();
}
rs.Close(); // 關閉數據表
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -