# Distance of shape points from given lat-long

I have a point shapefile, and in its attribute table I have some coordinates (DISP_LON, DISP_LAT) which are slightly different from the actual location of the points. I want to calculate the distance between actual point location and attribute table given co-ordinates, and want to insert in attribute table; preferably in Python, because I don't have ArcObjects SDK.

Initially I was trying to create line from the attribute coordinate and the Shapefile point coordinate. and then calculate line length.

To do some rough work I calculated the point lat-log in two new field name RP_LAT and RP_LONG and tried below code to create line by calculating distance between these two sets of coordinates:

``````import arcpy

in_rows = arcpy.SearchCursor("C:\Temp\PA.shp")
point = arcpy.Point()
array = arcpy.Array()
featureList = []
cursor = arcpy.InsertCursor("C:\Temp\Line.shp",["SHAPE@"])
feat = cursor.newRow()
for in_row in in_rows:
# Set X and Y for start and end points
point.X = in_row.RP_LON
point.Y = in_row.RP_LAT
point.X = float(in_row.DSPL)
point.Y = float(in_row.DSPLT)
# Create a Polyline object based on the array of points
polyline = arcpy.Polyline(array)
# Clear the array for future use
array.removeAll()
# Append to the list of Polyline objects
featureList.append(polyline)
# Insert the feature
feat.shape = polyline
cursor.insertRow(feat)
del feat
del cursor
``````

But the result is unexpected, it created strange long lines and empty records in a line shapefile.

My ultimate goal is find to distance of point coordinates and attribute given coordinates.

• The technique of moving is extremely simple, e.g. gis.stackexchange.com/questions/159206/… or just run arcpy.Point(!disp_lon!,!disp_lat!) on a Shape field. Your fields look like text though – FelixIP Feb 29 '16 at 19:56
• If you get stuck when writing the code just post a code snippet of where you are stuck and our volunteers are usually happy to try and help. – PolyGeo Mar 1 '16 at 7:57

After Some Brain Storming i happened to figure out how it can be done wihtout creating lines between two points. With Havernsine Formula i prepared below script:

``````import arcpy
import math

# defining input
inputFCClass = arcpy.GetParameterAsText(0)
DispLong = arcpy.GetParameterAsText(1)
DispLAT = arcpy.GetParameterAsText(2)
OutputField = "Dist_DP_RP"
to_add = "[" + OutputField + "]"

#Create a list of existing field names and adding new field
fieldList = arcpy.ListFields(inputFCClass)
fieldName = [f.name for f in fieldList]
if not field in fieldName:

#setting up progressor
RowCount  = arcpy.GetCount_management(inputFCClass)
PACnt = int(RowCount.getOutput(0))
arcpy.SetProgressor("step", "Counting Features...",0, PACnt, 1)

# hvernsine formula to calculate gedasic distance between two coordinate
def haversine(lon1, lat1, lon2, lat2):
# convert decimal degrees to radians
lon1, lat1, lon2, lat2 = map(math.radians, [lon1, lat1, lon2, lat2])
# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = math.sin(dlat/2)**2 + math.cos(lat1) * math.cos(lat2) *     math.sin(dlon/2)**2
c = 2 * math.asin(math.sqrt(a))
m = 6367000 * c
return m
rows = arcpy.da.UpdateCursor(inputFCClass,(DispLong ,DispLAT,OutputField, "SHAPE@XY"))
for row in rows:
# If Condition to skip blank records
if  (row<> ' ') or (row<> ' '):

lon1 = float(row)
lat1 = float(row)
lon2 = row
lat2 = row
row = haversine(lon1, lat1, lon2, lat2)
rows.updateRow(row)
arcpy.SetProgressorPosition()
del row, rows

arcpy.SelectLayerByAttribute_management(inputFCClass,"CLEAR_SELECTION")
arcpy.SelectLayerByAttribute_management(inputFCClass,"CLEAR_SELECTION")
arcpy.ResetProgressor()
``````