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

📄 environment.cpp

📁 程序编写环境为Visual Studio.NET 2002
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	}
	catch (CDaoException* e)
	{
		DisplayDaoException(e);
		delete tmpDB;
		e->Delete();
		return FALSE;
	}
    
    if(tmpDB)
	{
		if(tmpDB->IsOpen())
		{
			tmpDB->Close();
		}

		delete tmpDB;
		tmpDB = NULL;
	}

	return TRUE;
}
//-----------------------------------------------------------------------------------------
// 功能:得到给定的车站名在给定的公交线路上的车站顺序(如第2站)
// 参数:CString szLineName 公交线路名
//		 CString szStationName 公交车站名
// 返回值:> 0 车站顺序,否则失败
//-----------------------------------------------------------------------------------------
int CEnvironment::GetStationOrder(CString szLineName, CString szStationName)
{
	CDaoDatabase* tmpDB = new CDaoDatabase;
	try
	{
		tmpDB->Open(m_szDBName);
	}
	catch (CDaoException* e)
	{
		DisplayDaoException(e);
		delete tmpDB;
		e->Delete();
		return -1;
	}

	int nResult = -1;
	CDaoRecordset rs(tmpDB);
	try
	{
		CString szSQL;
		szSQL = "Select * From 公交车站路线 Where 线路名 ='" + szLineName + "' And 站名 ='" + szStationName + " '";
		rs.Open(dbOpenDynaset, szSQL);
        
		if (0 == rs.GetRecordCount())
			nResult = -1;
		else
		{
			COleVariant var = rs.GetFieldValue("顺序");
			nResult = atoi(CCrack::strVARIANT(var));
		}
		
		rs.Close();
	}
	catch (CDaoException* e)
	{
		DisplayDaoException(e);
		delete tmpDB;
		e->Delete();
		return -1;
	}
    
    if(tmpDB)
	{
		if(tmpDB->IsOpen())
		{
			tmpDB->Close();
		}

		delete tmpDB;
		tmpDB = NULL;
	}

	return nResult;
}
//-----------------------------------------------------------------------------------------
// 功能:得到给定的车站名的地理坐标
// 参数:CString szLineName 公交线路名
//		 MPoint pt 公交车的地理坐标
// 返回值:FALSE 失败
BOOL CEnvironment::GetStationPt(CString szStationName, MPoint* pt)
{
	CString szName = szStationName;
	CString szLayer = GetLayerName(szName,"地名索引");   
	CString szTable = GetTableName(szName,"地名索引");
    
	int nIndex =  GetLayerIndexByName(szTable); 
	if (nIndex < 0)
		return FALSE;
    
	CMoRecordset rs;
	rs = m_layerInfos[nIndex].layer.SearchExpression("名称 like '"+szName +"'");
	if (rs)
	{
		rs.MoveFirst();
        
		if (!rs.GetEof()) 
		{
			CMoFields fields(rs.GetFields());
			CMoField  shapeField(fields.Item(COleVariant(TEXT("Shape"))));
			CMoPoint pt1(shapeField.GetValue().pdispVal);
			pt->x = pt1.GetX();
			pt->y = pt1.GetY();
            
			return TRUE;
		}
	}    
	
	return FALSE;
}
//-----------------------------------------------------------------------------------------
// 将地图坐标中的点换算为屏幕坐标的点
MPoint CEnvironment::FromMapPoint(CMap1* map, double x,double y)
{
	MPoint pt;
	double dW = fabs(map->GetExtent().GetRight() - map->GetExtent().GetLeft());
	double dH = fabs(map->GetExtent().GetTop() - map->GetExtent().GetBottom())  ;
    
	double dRatio = 1.0;
	double dOrgX=0;
	double dOrgY=0;

	CRect* lpRect = new CRect();
	map->GetWindowRect(lpRect);    
	if(lpRect->Width()/dW > lpRect->Height()/dH) //横向居中
	{
		dRatio = lpRect->Height() /dH;
		dOrgX = (lpRect->Width() - dW * dRatio)/2;
	}
	else if(lpRect->Width() / dW < lpRect->Height() / dH) //纵向居中
	{
		dRatio = lpRect->Width() /dW;
		dOrgY = (lpRect->Height() - dH * dRatio)/2;
	}
    
	pt.x  = (x - map->GetExtent().GetLeft()) * dRatio + dOrgX;
	pt.y  = lpRect->Height() - (y - map->GetExtent().GetBottom()) * dRatio + dOrgY;
    
	delete lpRect;
	return pt;
}
//-----------------------------------------------------------------------------------------
CMoLine CEnvironment::GetLine(CString szName)
{
	CMoLine line;
    CString szLayer = GetLayerName(szName,"地名索引");   
	CString szTable = GetTableName(szName,"地名索引");
    
	if ("" == szTable)
	{
		return NULL;
	}
	int nIndex =  GetLayerIndexByName(szTable); 

	if (nIndex < 0)
		return NULL;
	
	CMoRecordset rs;
	rs = m_layerInfos[nIndex].layer.SearchExpression("名称 like '" + szName + "'");
    
	if (rs)
	{
		rs.MoveFirst();
        
		if (!rs.GetEof()) 
		{
			switch (m_layerInfos[nIndex].layer.GetShapeType())
			{
			case moShapeTypePoint:
				return line;
			case moShapeTypeLine:
				line = rs.GetFields().Item(COleVariant("shape")).GetValue().pdispVal;
				break;
			case moShapeTypePolygon:
				return line;
			default:
				break;
			} 
            
		}
	}
    
	return line;
}
//-----------------------------------------------------------------------------------------
CMoPoint CEnvironment::GetPoint(CString szName)
{
	CMoPoint pt;
	CString szLayer = GetLayerName(szName,"地名索引");   
	CString szTable = GetTableName(szName,"地名索引");
    
	if ("" == szTable)
	{
		return NULL;
	}
	int nIndex =  GetLayerIndexByName(szTable); 
    
	if (nIndex < 0)
		return NULL;
	
	CMoRecordset rs;
	rs = m_layerInfos[nIndex].layer.SearchExpression("名称 like '" + szName + "'");

	if (rs)
	{
		rs.MoveFirst();
        
		if (!rs.GetEof())
		{
			switch (m_layerInfos[nIndex].layer.GetShapeType())
			{
			case moShapeTypePoint:
				{
					pt = rs.GetFields().Item(COleVariant("shape")).GetValue().pdispVal;
				}
				break;
			case moShapeTypeLine:
				{
					CMoFields fields(rs.GetFields());
					CMoField  shapeField(fields.Item(COleVariant(TEXT("Shape"))));
					CMoLine line(shapeField.GetValue().pdispVal);
					pt = line.GetExtent().GetCenter();  
				}
				break;
			case moShapeTypePolygon:
				{
					CMoFields fields(rs.GetFields());
					CMoField  shapeField(fields.Item(COleVariant(TEXT("Shape"))));
					CMoPolygon  poly(shapeField.GetValue().pdispVal);
					pt = poly.GetExtent().GetCenter();  
				}
				break;
			} 
		}
	}
    
	return pt;
}
//-----------------------------------------------------------------------------------------
MLine* CEnvironment::CreateLine(CMoLine moline)
{
	MLine* line = new MLine();
	CMoPoints pts = moline.GetParts().Item(COleVariant(long(0), VT_I4));
    
	line->nPointNumber = pts.GetCount();
	line->pPoint = new MPoint[line->nPointNumber]; 
    
	for (long i = 0; i<line->nPointNumber; i++)
	{
		CMoPoint pt = pts.Item(COleVariant(i, VT_I4)); 
        line->pPoint[i].x = pt.GetX();
		line->pPoint[i].y = pt.GetY();
	}
	
	return line;
}
//-----------------------------------------------------------------------------------------
// 判断由参数szName指定的字符串是否是一有效的公交线路名称
BOOL CEnvironment::IsBusLine(CString szName)
{
	int i;
	for (i = 0; i<m_nLayerNum; i ++)
	{
		if (m_layerInfos[i].szName == "公交线路")
			break;
	}

	if (i == m_nLayerNum)
		return FALSE;

	CMoMapLayer ly = m_layerInfos[i].layer;  
	CMoRecordset rs = ly.SearchExpression("名称 like '"+szName +"'");;
			
	if (!rs)
		return FALSE;

	rs.MoveFirst(); 
	if (rs.GetEof())
		return FALSE;

	return TRUE;
}
//-----------------------------------------------------------------------------------------
// 根据名称得到图层
CMoMapLayer CEnvironment::GetLayerByName(CString szName)
{
	int i;
	for (i = 0; i<m_nLayerNum - 1; i ++)
	{
		if (m_layerInfos[i].szName == szName)
			break;
	}

	if (i == m_nLayerNum)
		return NULL;
	return m_layerInfos[i].layer; 
}
//-----------------------------------------------------------------------------------------
// 判断由参数szName指定的字符串是否是一有效的站点名称
BOOL CEnvironment::IsStation(CString szName)
{
	int i;
	for (i = 0; i<m_nLayerNum ; i ++)
	{
		if (m_layerInfos[i].szName == "公交车站")
			break;
	}

	if (i == m_nLayerNum)
		return FALSE;
	CMoMapLayer ly = m_layerInfos[i].layer; 
	CMoRecordset rs = ly.SearchExpression("名称 like '"+szName +"'");;
			
	if (!rs)
		return FALSE;

	rs.MoveFirst(); 
	if (rs.GetEof())
		return FALSE;

	return TRUE;
}
//-----------------------------------------------------------------------------------------
CString CEnvironment::GetFieldName(CString szName)
{
	CString szTable = GetTableName(szName,"地名索引");
	CString szFieldName = "单位名称";

	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 "+ m_mapInfos[m_nCurrMapIndex].szMetaTable 
			    + " Where 属性表名 ='"+szTable+"'";
        
		rs.Open(dbOpenDynaset, szSQL);
		if (0 == rs.GetRecordCount())
			return szFieldName;

		COleVariant var;
		var = rs.GetFieldValue("字段名");
		szFieldName = CCrack::strVARIANT(var);
		if(szFieldName == "NULL")
			szFieldName = "";

		rs.Close(); // 关闭数据表
	}
	catch (CDaoException* e)
	{
		DisplayDaoException(e);
		delete tmpDB;
		e->Delete();
		return "";
	}
    
    if(tmpDB)
	{
		if(tmpDB->IsOpen())
		{
			tmpDB->Close();
		}

		delete tmpDB;
		tmpDB = NULL;
	}
	
	return szFieldName;
}
//-----------------------------------------------------------------------------------------
double CEnvironment::GetLineLength(CMoLine line)
{
	MPoint* pt;
	int nPtCount;
	CMoPoints pts;    
	pts = line.GetParts().Item(COleVariant(long(0), VT_I4));
	nPtCount = pts.GetCount();
    
	if (nPtCount < 2)
		return 0.0;
    
	pt = new MPoint[nPtCount];   
	for (long i = 0; i < nPtCount; i ++)
	{
		CMoPoint point(pts.Item(COleVariant(i, VT_I4)));
		pt[i].x = point.GetX();
		pt[i].y = point.GetY(); 
	}
    
	double length = CalcLenght(pt, nPtCount);
	delete pt;
	pt = NULL;

	return length;
}
//-----------------------------------------------------------------------------------------
double CEnvironment::GetPolygonLength(CMoPolygon poly)
{
	MPoint* pt;
	int nPtCount;			
	CMoPoints pts;
	pts = poly.GetParts().Item(COleVariant(long(0), VT_I4));
	nPtCount = pts.GetCount();
    
	if (nPtCount < 2)
		return 0.0;
    
	pt = new MPoint[nPtCount+1];
	for (long i=0; i<nPtCount; i ++)
	{
		CMoPoint point(pts.Item(COleVariant(i, VT_I4)));
		pt[i].x = point.GetX();
		pt[i].y = point.GetY(); 
	}
    
	pt[nPtCount].x = pt[0].x;
	pt[nPtCount].y = pt[0].y;
    
	double dLength = CalcLenght(pt, nPtCount+1);
	delete pt;
	pt = NULL;

	return dLength;
}
//-----------------------------------------------------------------------------------------
double CEnvironment::GetPolygonArea(CMoPolygon poly)
{
	MPoint* pt;
	int nPtCount;
	CMoPoints pts;
    pts = poly.GetParts().Item(COleVariant(long(0), VT_I4));
	nPtCount = pts.GetCount();
    
	if (nPtCount < 3)
		return 0.0;
    
	pt = new MPoint[nPtCount+1];
    for (long i=0; i<nPtCount; i ++)
	{
		CMoPoint point(pts.Item(COleVariant(i, VT_I4)));
		pt[i].x = point.GetX();
		pt[i].y = point.GetY(); 
	}
	
	pt[nPtCount].x = pt[0].x;
	pt[nPtCount].y = pt[0].y;
    
	double dArea = CalcArea(pt, nPtCount+1);
	delete pt;
	pt = NULL;

	return dArea;
}
//-----------------------------------------------------------------------------------------
double CEnvironment::CalcArea(MPoint* pt,int nSize)
{
	double dArea = 0;
	double x1=0, x2=0, y1=0, y2=0;
	int nCenterL = ((int)(pt[0].x) / 6 + 1 ) * 6 - 3; 
    
	for(int i=0; i<nSize-1; i++)
	{
		CalGuassFromLB(pt[i].x, pt[i].y, &x1, &y1, nCenterL);
		CalGuassFromLB(pt[i+1].x, pt[i+1].y, &x2, &y2, nCenterL);
		dArea += (x2 - x1 ) * ( y1 + y2) / 2;
	}
    
	if (dArea < 0)
		dArea = 0 - dArea; 
	return dArea;
}
//-----------------------------------------------------------------------------------------

⌨️ 快捷键说明

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