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

📄 environment.cpp

📁 程序编写环境为Visual Studio.NET 2002
💻 CPP
📖 第 1 页 / 共 3 页
字号:
//-----------------------------------------------------------------------------------------
// 根据地图名称得到当前地图在地图集合中的索引
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 + -