I have a shapefile called "household_points" of ~2,000 points, which are divided into about 100 sites. The shapefile has a field in the attribute table called "SNAME" and in the field each point is assigned to a site. Using Median Center, I have created a new shapefile, called "household_points_median" for the median point for all the sites. Now, I want to calculate the distance from each point in each site in "household_points" to its corresponding median point in "household_points_median". I know I can select the appropriate records for each site in the two shapefiles and use the Point Distance Tool, but as there are about 100 sites this would be laborious. I feel like it can be done iteratively, using a for loop in python or something, but I'm not really sure how to loop over field in an attribute table. Or maybe there's just a better tool?

Thanks in advance!

  • Is 'SNAME' a field which is present in both your "household_points" and "household_points_median" shapefile to uniquely identify each site?
    – PolyGeo
    Oct 15, 2013 at 5:33
  • What license of ArcGIS are you using? (Editor/Standard, Info/Advanced, etc) Oct 15, 2013 at 12:08

1 Answer 1


The following python code should help you calculate the distances from points to their corresponding median point. The script creates a new field in your point file and stores the distance to the median point. This script assumes that the point file and median point file have a common field that relates the points to the median points. If you generate the median points using ArcGIS's 'Median Center' tool, then the identifier field names should match between the point file and median point file. Just edit the top four variables to match the location of your data and name of fields in your shapefile. Also, I wrote this on my machine with ArcMap 10.0, but it should work with 10.1+. If not, just let me know and I'll fix it!

BTW: This script calculates Euclidean distance instead of great circle distance, so your data should be in a projected coordinate system.

import arcpy
import math

# Change these first four variables to suit your data
# 1. points is the raw point file
# 2. medianPoints is a point file of median points using ArcMap's 'Median Center' tool
# 3. sharedNameField is the name of the field that both points and medianPoints share
# 4. distanceFieldName is the name of the field that will be created to store distances

points = r'C:\Users\Administrator\Desktop\median points\points_identity.shp'
medianPoints = r'C:\Users\Administrator\Desktop\median points\points_median_center.shp'
sharedNameField = 'SNAME'
distanceFieldName = "pointDist"

# add field to point dataset that will store distances corresponding to median point
arcpy.AddField_management(points, distanceFieldName, "DOUBLE")

# loop through median points, get x,y coords and field value
medianPointCursor = arcpy.SearchCursor(medianPoints)
for medianPoint in medianPointCursor:
    medPointXY = medianPoint.getValue('Shape').getPart()
    x1,y1 = medPointXY.X, medPointXY.Y
    fieldValue = medianPoint.getValue(sharedNameField)

    # query the point file for only points that match the sharedNameField value
    query = "{0} = '{1}'".format(sharedNameField, fieldValue)
    pointCursor = arcpy.UpdateCursor(points, query)
    for point in pointCursor:
        pointXY = point.getValue('Shape').getPart()
        x2,y2 = pointXY.X, pointXY.Y
        distanceToMedianPt = math.hypot(x2 - x1, y2 - y1)
        point.setValue(distanceFieldName, distanceToMedianPt)

    del point, pointCursor
del medianPoint, medianPointCursor

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.