You are here: > ESRI Forums > arcgis desktop discussion forums > Thread Replies

ArcGIS Desktop Discussion Forums

ArcGIS Desktop - General Concepts forum

Calculating nearest neigbours within a poly...   Chris Hassall Apr 03, 2007
Re: Calculating nearest neigbours within a...   Miles Hitchen Apr 03, 2007
Re: Calculating nearest neigbours within a...   Chris Hassall Apr 03, 2007
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Calculating nearest neigbours within a polygon shapefile 
Author Chris Hassall 
Date Apr 03, 2007 
Message Dear all,

I have a shapefile containing a large number of polygons and I would like to know, for each of those polygons, how far it is to the nearest neighbouring polygon. It seems this question has been asked a couple of times but I haven't been able to find an answer.

Many thanks,

Chris 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Calculating nearest neigbours within a polygon shapefile 
Author Miles Hitchen 
Date Apr 03, 2007 
Message Hi Chris,

You can use the advanced calculator script below. It will populate a field (long integer) with the ObjectID of the nearest polygon. If you're not using a shapefile then you'll need to change [FID] to [ObjectID] in the code, before using it.

Hope it helps,
Miles.
 
 
Static pMxDoc As IMxDocument
Static pFtrLyr As IFeatureLayer
Static pFtrCls As IFeatureClass
Static lID As Long
Static dDis As Double
Static lThisOID As Long
Static pQryFltr As IQueryFilter
Dim pFtrCsr As IFeatureCursor
Dim pFtrIdx As IFeatureIndex
Dim pIdxQry As IIndexQuery

    lThisOID = [FID]

    If pMxDoc Is Nothing Then
        Set pMxDoc = ThisDocument
        Set pFtrLyr = pMxDoc.FocusMap.Layer(0)
        Set pFtrCls = pFtrLyr.FeatureClass
        Set pQryFltr = New QueryFilter
    End If

    pQryFltr.WhereClause = pFtrCls.OIDFieldName + " <> " & lThisOID
    Set pFtrCsr = pFtrCls.Search(pQryFltr, False)

    Set pFtrIdx = New FeatureIndex
    Set pFtrIdx.FeatureClass = pFtrCls
    Set pFtrIdx.FeatureCursor = pFtrCsr
    pFtrIdx.Index Nothing, Nothing

    Set pIdxQry = pFtrIdx
    pIdxQry.NearestFeature pFtrCls.GetFeature(lThisOID).Shape, lID, dDis

    Set pIdxQry = Nothing
    Set pFtrIdx = Nothing
'
' Put lID in the box below the Advaned Window
'
 
  Miles Hitchen
Software Engineer
Geospatial Team
Ordnance Survey
UK
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Calculating nearest neigbours within a polygon shapefile 
Author Chris Hassall 
Date Apr 03, 2007 
Message Miles,

Thanks, that did the trick!

Chris