📄 利用vc+mo最短路径算法--gis开发.htm
字号:
)<BR> continue;<BR> szTemp
= szAngle.Left(nPos );<BR> dAngle =
atof(szTemp );<BR> szAngle =
szAngle.Right(szAngle.GetLength() - nPos - 1
);<BR> node.Add( nLink, dAngle
);<BR> }<BR> <BR> m_arrNodes.AddTail(
node );<BR> rs.MoveNext();<BR> }</P>
<P> rs.Close();<BR> }<BR> catch (CDaoException*
e)<BR> {<BR> DisplayDaoException(e);<BR> delete
tmpDB;<BR> e->Delete();<BR> return
FALSE;<BR> }<BR> <BR>
if(tmpDB)<BR> {<BR> if(tmpDB->IsOpen())<BR> {<BR> tmpDB->Close();<BR> }</P>
<P> delete tmpDB;<BR> tmpDB =
NULL;<BR> }</P>
<P> return
TRUE;<BR>}<BR>//-----------------------------------------------------------------------------------------<BR>BOOL
CNetLayer::PathAnalysis(double x1, double y1, double x2, double y2,
<BR> CList<double,
double&>* path )<BR>{<BR> NetPoint pt1( x1, y1
);<BR> NetPoint pt2( x2, y2 );</P>
<P> CList<NetPoint, NetPoint&>
points;<BR> points.AddTail(pt1);<BR> points.AddTail(pt2);</P>
<P> CList<int, int&> stops; <BR> if (
!LoadStops(&points, &stops ) )<BR> return
FALSE;<BR> if ( stops.GetCount() != 2 )<BR> return
FALSE;</P>
<P> CList<int, int&> nodes;<BR> double
dDistance;<BR> // 得到起点<BR> int nFirst =
stops.GetAt(stops.FindIndex(0));<BR> // 得到终点<BR> int
nSecnd = stops.GetAt(stops.FindIndex(1));<BR> //
计算距离<BR> dDistance = Path(nFirst, nSecnd, &nodes,
FALSE);<BR> if ( dDistance < 0 )<BR> return
FALSE;</P>
<P> NetLine* line;<BR> line = new
NetLine(m_layer);<BR> // 根据节点创建线对象<BR> if (
!CreateResultPath(&nodes, line, FALSE) )<BR> return
FALSE;</P>
<P> for ( int i=0; i<line->m_pCoords.GetCount(); i++
)<BR> {<BR> NetPoint np =
line->m_pCoords.GetAt(line->m_pCoords.FindIndex(i));<BR> path->AddTail(np.x
);<BR> path->AddTail(np.y );<BR> }</P>
<P> //
释放资源<BR> UnloadStops();<BR> if(line)<BR> {<BR> delete
line;<BR> line = NULL;<BR> }</P>
<P> return
true;<BR>}<BR>//-----------------------------------------------------------------------------------------<BR>BOOL
CNetLayer::LoadStops(CList<NetPoint, NetPoint&>* pPoints,
<BR> CList<int,
int&>* pNodes )<BR>{ <BR> int nLineID;<BR> int
i, nNewNode;<BR> NetPoint* ptNearest = NULL;<BR> ptNearest
= new NetPoint();<BR> double dRatio; </P>
<P> // 先清空站点表 <BR> int nNum =
pPoints->GetCount();<BR> for ( i = 0; i < nNum; i++
)<BR> {<BR> // 计算距离该点最近的线<BR> NetLine
line(m_layer);<BR> NetPoint np =
pPoints->GetAt(pPoints->FindIndex(i));<BR> nLineID
= line.GetNearestLineData( np.x, np.y);<BR> if ( nLineID
== -1
)<BR> { <BR> return
false;<BR> }</P>
<P> /* 计算该点分裂该线的位置, 并不实际分裂该线, <BR> 只是计算分裂的比例,
用于更改弧段表和结点表 */<BR> dRatio =
0;<BR> line.GetSplitRatioByNearestPoint (np, ptNearest,
&dRatio );</P>
<P> // 更新弧段表和结点表<BR> UpdateLinkNodeTable(
nLineID, *ptNearest, dRatio, &nNewNode ); </P>
<P> if ( pNodes->GetCount() > 0
)<BR> {<BR> int nCount =
pNodes->GetCount() - 1;<BR> if
(pNodes->GetAt(pNodes->FindIndex(nCount)) == nNewNode
)<BR> continue;<BR> }<BR> pNodes->AddTail(nNewNode
);<BR> }</P>
<P> // 填充需要返回的结点数组<BR> if ( pNodes->GetCount() == 0
)<BR> {<BR> pNodes = NULL;<BR> return
FALSE;<BR> }</P>
<P> if(ptNearest)<BR> {<BR> delete
ptNearest;<BR> ptNearest =
NULL;<BR> }<BR> return
TRUE;<BR>}<BR>//-----------------------------------------------------------------------------------------<BR>BOOL
CNetLayer::UpdateLinkNodeTable(int nLineID, NetPoint ptNearest,
<BR> double
dRatio, int* nNewNode )<BR>{<BR> int i, j;<BR> bool
bFound;<BR> double dRatio2;<BR> int nCurLink,
nNewLink;<BR> int nCurFNode,
nCurTNode; <BR> *nNewNode = -1;</P>
<P> // 与某条弧段的首点或者位点重合, 不需要更改弧段表和结点表<BR> if ( fabs(dRatio)
< 0.00000001 )<BR> {<BR> //
首点<BR> nCurLink = GetNode( nLineID, &nCurFNode,
&nCurTNode );<BR> *nNewNode =
nCurFNode;<BR> return TRUE;<BR> }<BR> else if (
fabs(1-dRatio) < 0.00000001 )<BR> {<BR> //
尾点<BR> nCurLink = GetNode( nLineID, &nCurFNode,
&nCurTNode );<BR> *nNewNode =
nCurTNode;<BR> return TRUE;<BR> }</P>
<P> bFound = FALSE;<BR> for ( i=0;
i<m_arrLinkBackups.GetCount(); i++
)<BR> {<BR> NetLinkBackup* nl =
&(m_arrLinkBackups.GetAt(<BR>
m_arrLinkBackups.FindIndex(i)));<BR> if (
nl->m_Link.m_GeoID == nLineID
)<BR> {<BR> bFound =
TRUE;<BR> break;<BR> }<BR> }</P>
<P> if ( bFound )<BR> {<BR> NetLinkBackup* nl =
&(m_arrLinkBackups.GetAt(<BR>
m_arrLinkBackups.FindIndex(i)));<BR> for ( j = 0; j <
nl->m_arrSegs.GetCount(); j++
)<BR> {<BR> // 如果新点与原有的点重合,
则直接返回原来的点<BR> NetLinkSeg nlS =
nl->m_arrSegs.GetAt(nl->m_arrSegs.FindIndex(j));<BR> double
r = nlS.dRatio;<BR> if ( fabs( r - dRatio) <
0.00000001 )<BR> {<BR> if (
j == 0
)<BR> {<BR> nCurLink
= GetNode( nLineID, &nCurFNode, &nCurTNode
);<BR> *nNewNode =
nCurTNode;<BR> return
true;<BR> }<BR> else<BR> {<BR> NetLinkSeg
nlSPre =
nl->m_arrSegs.GetAt(<BR>
nl->m_arrSegs.FindIndex(j-1));<BR> nCurLink
= nlSPre.nSegID;<BR> *nNewNode =
m_arrLinks.GetAt(<BR>
m_arrLinks.FindIndex(nCurLink)).m_nTNode;<BR> return
TRUE;<BR> }<BR> }</P>
<P> // 没有重合的点<BR> r =
nlS.dRatio;<BR> if ( dRatio < r
)<BR> break;<BR> }<BR> if
( j == 0 ) <BR> { <BR> //
第一段<BR> nCurLink = GetNode( nLineID,
&nCurFNode, &nCurTNode );<BR> if ( nCurLink
== -1 )<BR> return false;</P>
<P> // 更新结点表<BR> nNewLink =
m_arrLinks.GetCount();<BR> NetNode pNode(
ptNearest.x, ptNearest.y );<BR> pNode.Add(
nNewLink, -1 ); <BR> pNode.Add( nCurLink, -1
);<BR> m_arrNodes.AddTail( pNode );</P>
<P> NetNode* nd =
&(m_arrNodes.GetAt(m_arrNodes.FindIndex(nCurTNode)));<BR> nd->Remove(
nCurLink );<BR> nd->Add( nNewLink, -1 );</P>
<P> // 更新弧段表<BR> *nNewNode =
m_arrNodes.GetCount() - 1;<BR> NetLinkSeg nlS =
nl->m_arrSegs.GetAt(nl->m_arrSegs.FindIndex(0));<BR> dRatio2
= nlS.dRatio;<BR> NetLink
pLink;<BR> pLink.m_GeoID =
nLineID;<BR> pLink.m_nFNode =
*nNewNode;<BR> pLink.m_nTNode =
m_arrLinks.GetAt(<BR>
m_arrLinks.FindIndex(nlS.nSegID)).m_nFNode;<BR> NetLink*
oldLink =
&(m_arrLinks.GetAt(<BR>
m_arrLinks.FindIndex(nCurLink)));<BR> pLink.m_fLength
= (float)(oldLink->m_fLength * ( dRatio2 - dRatio
));<BR> pLink.m_fFromImp =
(float)(oldLink->m_fFromImp * ( dRatio2 - dRatio
));<BR> pLink.m_fToImp =
(float)(oldLink->m_fToImp * ( dRatio2 - dRatio
));<BR> m_arrLinks.AddTail( pLink );</P>
<P> oldLink->m_nTNode =
*nNewNode;<BR> oldLink->m_fLength = (float)
(oldLink->m_fLength *
dRatio);<BR> oldLink->m_fFromImp = (float)
(oldLink->m_fFromImp *
dRatio);<BR> oldLink->m_fToImp = (float)
(oldLink->m_fToImp* dRatio);</P>
<P> nl->Add( nNewLink, dRatio
);<BR> }<BR> else if ( j ==
nl->m_arrSegs.GetCount()) <BR> { <BR> //
最后一段<BR> NetLinkSeg nlS =
nl->m_arrSegs.GetAt(nl->m_arrSegs.FindIndex(j-1));<BR> nCurLink
= nlS.nSegID;<BR> nCurFNode =
m_arrLinks.GetAt(<BR>
m_arrLinks.FindIndex(nCurLink)).m_nFNode;<BR> nCurTNode
=
m_arrLinks.GetAt(<BR>
m_arrLinks.FindIndex(nCurLink)).m_nTNode;</P>
<P> // 更新结点表<BR> nNewLink =
m_arrLinks.GetCount();<BR> NetNode pNode(
ptNearest.x, ptNearest.y );<BR> pNode.Add(
nNewLink, -1 ); <BR> pNode.Add( nCurLink, -1
);<BR> m_arrNodes.AddTail( pNode );</P>
<P> NetNode* netNode =
&(m_arrNodes.GetAt(<BR>
m_arrNodes.FindIndex(nCurTNode)));<BR> //
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -