导航

求最近距离

Posted on 2008-09-19 18:00  HFlying  阅读(447)  评论(1)    收藏  举报

关于计算最短距离arcEngine可以两个接口IProximityOperator和ihittest,一开始用ihittest,但有时候返回的点为空,用IProximityOperator的ReturnNearestPoint函数可以实现。代码如下:

'**********************************************************

'【函  数】:GetNearestDistValue
'【功  能】:线和实体对象的最近距离计算
'【参  数】:pPolyline 线实体 pGeometry实体
'【返回值】:最近距离,最近点
'**********************************************************
Private Function GetNearestDistValue(ByVal pPolyline As IPolyline, ByVal pGeometry As IGeometry, pNear As IPoint) As Double
 'On Error GoTo myErr
    Dim pHit As IPoint
    Dim p1 As IPoint
    Dim p2 As IPoint
    Dim dDist As Double, dDist1 As Double, dDist2 As Double
    Dim pPntCol As IPointCollection
    dDist1 = 1000
    dDist2 = 1000
    Dim i As Integer
    Dim pTopo As ITopologicalOperator
    Dim pGeo As IGeometry
    Set pTopo = pGeometry
    '//相交判断
    If pGeometry.GeometryType = esriGeometryPolygon Then
        Set pGeo = pTopo.Intersect(pPolyline, esriGeometry1Dimension)
    Else
        Set pGeo = pTopo.Intersect(pPolyline, esriGeometry0Dimension)
    End If
    If Not pGeo.IsEmpty Then
        GetNearestDistValue= 0
        Exit Function
    End If
    '//计算最短距离
    Set pPntCol = pGeometry
    For i = 0 To pPntCol.PointCount - 1
        dDist = GetNearDist(pPolyline, pPntCol.Point(i), pHit)
        If dDist < dDist1 Then
            dDist1 = dDist
            Set p1 = pPntCol.Point(i)
        End If
    Next

    Set pPntCol = pPolyline
    For i = 0 To pPntCol.PointCount - 1
        dDist = GetNearDist(pGeometry, pPntCol.Point(i), pHit)
        If dDist < dDist2 Then
            dDist2 = dDist
            Set p2 = pHit
        End If
    Next
   
    If dDist1 < dDist2 Then
       GetNearestDistValue= dDist1
       Set pNear = p1
    Else
       GetNearestDistValue= dDist2
       Set pNear = p2
    End If
End Function
Private Function GetNearDist(ByVal pGeo As IGeometry, ByVal pPoint As IPoint, pHit As IPoint) As Double
    Dim ppp As IProximityOperator
    Set ppp = pGeo
    GetNearDist = ppp.ReturnDistance(pPoint)
    Set pHit = ppp.ReturnNearestPoint(pPoint, 0)
End Function