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

ArcGIS Desktop Discussion Forums

ArcGIS Desktop - Extension - Spatial Analyst forum

calculating distance between polygons withi...   Jason Trook Aug 18, 2008
Re: calculating distance between polygons w...   Lander Bral Aug 21, 2008
Re: calculating distance between polygons w...   darren johnson Jun 24, 2009
Re: calculating distance between polygons w...   William Huber Jun 24, 2009
Re: calculating distance between polygons w...   dave timmons May 28, 2010
Re: calculating distance between polygons w...   Jason Trook Jun 30, 2009
Re: calculating distance between polygons w...   Abri de Buys Aug 11, 2009
Re: calculating distance between polygons w...   Hugo Larocque Aug 24, 2009
Re: calculating distance between polygons w...   Dianne Yunker Aug 27, 2009
Re: calculating distance between polygons w...   Abri de Buys Aug 28, 2009
Re: calculating distance between polygons w...   Hugo Larocque Oct 21, 2009
Re: calculating distance between polygons w...   Orimoloye Tunde May 27, 2010
Re: calculating distance between polygons w...   Orimoloye Tunde May 27, 2010
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject calculating distance between polygons within the same feature layer 
Author Jason Trook 
Date Aug 18, 2008 
Message Hello,

I am trying to calculate the distance between polygons within the same feature layer. More specifically I have 100+ polygons and I want to find the nearest neighbor (closest polygon) for every polygon and its respective distance. Because the size and shape of the polygons vary creating a centroid point for each polygon did not work. Does anyone know how to find the closest distance between edges? Thanks in advance for your help.

-- Jason 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: calculating distance between polygons within the same feature layer 
Author Lander Bral 
Date Aug 21, 2008 
Message I have the same question. I have to calculate the distance between polygons approximately on the same position but in another layer. 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: calculating distance between polygons within the same feature layer 
Author darren johnson 
Date Jun 24, 2009 
Message Did you ever find a solution to this? 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: calculating distance between polygons within the same feature layer 
Author William Huber 
Date Jun 24, 2009 
Message I recall providing a solution in detail sometime last year: consider searching these forums. The method is based on performing a Euclidean allocation of the polygon grid, identifying the boundaries of the resulting regions by means of a focal variety calculation, and computing zonal minima of the Euclidean distance grid along the boundaries. Doubling this estimates the distance to the nearest polygon. The identity of the nearest polygon can be found with similar processing to select boundary points where minimum distances are obtained and obtaining the max and min of polygon identifiers within their immediate neighborhoods. 
  --Bill Huber
Quantitative Decisions (http://www.quantdec.com )
More GIS Q&A at http://gis.stackexchange.com/q/3083/664 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: calculating distance between polygons within the same feature layer 
Author dave timmons 
Date May 28, 2010 
Message I used Bill's idea, but with a simpler method. There are two steps:

1) With the polygons of interest as the feature source data, use the Euclidean allocation tool to create an output allocation raster and an output distance raster.

2) Use the zonal fill tool, with the allocation raster from step #1 as the input zone raster and the distance raster from step #1 as the input weight raster. Since the zonal fill tool identifies the minimum value (in this case, a distance) on the border of the zone, the output represents half the distance to the nearest polygon. Double this for the full distance.

I believe this works well, but would be interested to know if anybody sees problems with this method.

Dave 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: calculating distance between polygons within the same feature layer 
Author Jason Trook 
Date Jun 30, 2009 
Message I found/modified some code that worked for me. Again it calculates closest edge to edge distance between polygons. I'm attaching what I could find leftover from that project. Results can be viewed in the debug window. Debug window limits the number of entries to 200 or so. If you have more records than this I would export to a text file. 
 
Sub findNearestFeature()
    Dim mxDoc As IMxDocument
    Dim featureLayer As IFeatureLayer
    Dim featureCursor As IFeatureCursor
    Dim feature As IFeature
    Dim queryFilter As IQueryFilter
    Dim tempCursor As IFeatureCursor
    Dim featureIndex As IFeatureIndex
    Dim indexQuery As IIndexQuery
    Dim trackCancel As ITrackCancel
    Dim geoDataset As IGeoDataset
    Dim oid As Long
    Dim distance As Double
    
    ' Get the first layer in the map.
    Set mxDoc = ThisDocument
    Set featureLayer = mxDoc.FocusMap.Layer(0)
    
    ' Get all features in the layer.
    Set featureCursor = featureLayer.FeatureClass.Search(Nothing, False)
    
    ' Process all features in the layer.
    Set feature = featureCursor.NextFeature
    Do While Not feature Is Nothing
        ' Build a query that will get all features that share the attribute value with this feature.
        ' Since we are querying the same layer this feature comes from, we need to omit this feature from the results.
        ' The field SUBTYPEID is the attribute field we are using in this example.
        Set queryFilter = New queryFilter

        ' Execute the query.
        Set tempCursor = featureLayer.FeatureClass.Search(queryFilter, False)
        
        ' Build the feature index.
        Set featureIndex = New featureIndex
        Set featureIndex.FeatureClass = featureLayer.FeatureClass
        Set featureIndex.featureCursor = tempCursor
        Set geoDataset = featureLayer.FeatureClass
        Set trackCancel = New CancelTracker
        featureIndex.Index trackCancel, geoDataset.Extent
        
        ' Find the nearest feature.
        Set indexQuery = featureIndex
        indexQuery.NearestFeature feature.ShapeCopy, oid, distance
        
        ' Print results to debug window.
        Debug.Print "Feature:  " & feature.oid & vbTab & "Closest feature:  " & oid & vbTab & "Distance:  " & distance
        
        ' Release the objects used within this loop (very important).
        Set tempCursor = Nothing
        Set featureIndex = Nothing
        Set indexQuery = Nothing
        Set feature = featureCursor.NextFeature
    Loop
End Sub
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: calculating distance between polygons within the same feature layer 
Author Abri de Buys 
Date Aug 11, 2009 
Message Has anyone used this code with success? 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: calculating distance between polygons within the same feature layer 
Author Hugo Larocque 
Date Aug 24, 2009 
Message Hi,

The only problem with the code was that there was a need for a query filter in order to remove the current feature. Therefore, it would not choose itself as the nearest feature.

Hugo 
 
Sub findNearestFeature()
    Dim mxDoc As IMxDocument
    Dim featureLayer As IFeatureLayer
    Dim featureCursor As IFeatureCursor
    Dim feature As IFeature
    Dim queryFilter As IQueryFilter
    Dim tempCursor As IFeatureCursor
    Dim featureIndex As IFeatureIndex
    Dim indexQuery As IIndexQuery
    Dim trackCancel As ITrackCancel
    Dim geoDataset As IGeoDataset
    Dim oid As Long
    Dim distance As Double
    
    ' Get the first layer in the map.
    Set mxDoc = ThisDocument
    Set featureLayer = mxDoc.FocusMap.Layer(0)
    
    ' Get all features in the layer.
    Set featureCursor = featureLayer.FeatureClass.Search(Nothing, False)
    
    ' Process all features in the layer.
    Set feature = featureCursor.NextFeature
    Do While Not feature Is Nothing
        ' Build a query that will get all features that share the attribute value with this feature.
        ' Since we are querying the same layer this feature comes from, we need to omit this feature from the results.
        ' The field SUBTYPEID is the attribute field we are using in this example.
        Set queryFilter = New queryFilter
        'Needs a query filter
        Dim Qry As String
        Qry = """FID"" <> " + Str(feature.Value(feature.Fields.FindField("FID")))
        queryFilter.WhereClause = Qry
                

        ' Execute the query.
        Set tempCursor = featureLayer.FeatureClass.Search(queryFilter, False)
        
        ' Build the feature index.
        Set featureIndex = New featureIndex
        Set featureIndex.FeatureClass = featureLayer.FeatureClass
        Set featureIndex.featureCursor = tempCursor
        Set geoDataset = featureLayer.FeatureClass
        Set trackCancel = New CancelTracker
        featureIndex.Index trackCancel, geoDataset.Extent
        
        ' Find the nearest feature.
        Set indexQuery = featureIndex
        indexQuery.NearestFeature feature.ShapeCopy, oid, distance
        
        ' Print results to debug window.
        Debug.Print "Feature:  " & feature.oid & vbTab & "Closest feature:  " & oid & vbTab & "Distance:  " & distance
        
        ' Release the objects used within this loop (very important).
        Set tempCursor = Nothing
        Set featureIndex = Nothing
        Set indexQuery = Nothing
        Set feature = featureCursor.NextFeature
    Loop
End Sub

 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: calculating distance between polygons within the same feature layer 
Author Dianne Yunker 
Date Aug 27, 2009 
Message If I try this code It doesn't work, goes to the debugger and highlights this row:

Qry = """FID"" <> " + Str(feature.Value(feature.Fields.FindField("FID"))) 
 
Qry = """FID"" <> " + Str(feature.Value(feature.Fields.FindField("FID")))
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: calculating distance between polygons within the same feature layer 
Author Abri de Buys 
Date Aug 28, 2009 
Message Dianne is right. I have the same problem! 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: calculating distance between polygons within the same feature layer 
Author Hugo Larocque 
Date Oct 21, 2009 
Message Hi,

Try changing FID by OBJECTID.
I am assuming you are probably working with a geodatabase (I was working with a shapefile) and in that case the query syntax is a bit different also. So, you could try something like this and play a bit with the syntax if it doesn't work

Qry = "[OBJECTID] <> " + Str(feature.Value(feature.Fields.FindField("OBJECTID")))

Hope it works for you.
Hugo 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: calculating distance between polygons within the same feature layer 
Author Orimoloye Tunde 
Date May 27, 2010 
Message Please Hugo, I am having problem selecting the query filter for the code. Please can you describe how to do this for the code to work. I have been able to overcome the OBJECTID/FID section.
Please I would appreciate any help please. 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: calculating distance between polygons within the same feature layer 
Author Orimoloye Tunde 
Date May 27, 2010 
Message Please Hugo, I am having problem selecting the query filter for the code. Please can you describe how to do this for the code to work. I have been able to overcome the OBJECTID/FID section.
Please I would appreciate any help please.