English

How To: Calculate distance to many points in a shapefile from a single point in another shapefile

Summary

Instructions provided describe how to calculate the distances from all points in a shapefile to a single point in another shapefile.

Note:
Both shapefiles must be in the same Projected Coordinate System, using linear units, e.g., feet or meters.

Procedure

The Visual Basic for Applications (VBA) script below calculates the distances from all points in a shapefile to a single point in another shapefile.

  1. Copy the script below into Notepad, save the script with the name 'calculate_many_distances.txt', and minimize the Notepad window on the taskbar.

    Code:
    Dim pMxDoc As IMxDocument
    Set pMxDoc = ThisDocument
    Dim pMap As IMap
    Set pMap = pMxDoc.FocusMap
    Dim pLayer as Ilayer
    Set pLayer = pMap.Layer(0)
    Dim pFLayer as IFeatureLayer
    Set pFLayer = pLayer
    Dim pFClass as IFeatureClass
    Set pFClass = pFLayer.FeatureClass
    Dim pFeature as IFeature
    Set pFeature = pFClass.GetFeature(0)
    Dim pPointOne As IPoint
    Set pPointOne = pFeature.shape
    Dim dblX1 as Double
    Dim dblY1 as Double
    dblX1 = pPointOne.X
    dblY1 = pPointOne.Y

    Dim pGeometry As IGeometry
    Set pGeometry = [Shape]
    Dim dblDistance as Double
    Dim dblX2 as Double
    Dim dblY2 as Double
    If (Not IsNull([Shape])) Then
    Set pGeometry = [Shape]
    If (Not pGeometry.IsEmpty) Then
    Dim pPointMany As IPoint
    Set pPointMany = [Shape]
    dblX2 = pPointMany.X
    dblY2 = pPointMany.Y
    dblDistance = SQR( (dblX1-dblX2)^2 + (dblY1-dblY2)^2 )
    End If
    End If

  2. Add both sets of point data to a new, empty ArcMap session.
  3. Right-click the name of the layer that contains many points > Selection > Select All.
  4. Open the attribute table for the shapefile that contains many points, and add a field to the table named DISTANCE, defined as a double field.
  5. In the attribute table, right-click the field name DISTANCE > Field Calculator.
  6. In the Field Calculator, check the 'Advanced' box. Copy the lines from Notepad into the Pre-Logic VBA Script Code window.
  7. In the 'Distance =' box , type in dblDistance. Click OK.
  8. The distances from the single point are calculated into the DISTANCE field in the attribute table of the shapefile containing many points.
  9. The DISTANCE is calculated in the units of the projection; feet or meters. If other distance units such as miles are needed, add a new field to the attribute table, such as MILES, and calculate the units from the DISTANCE field into the new field.

    Note:
    To convert U. S. Survey feet to miles, multiply DISTANCE*0.0001893939 OR divide DISTANCE/5280
    To convert meters to miles, multiply DISTANCE*0.0006213711922