AnsweredAssumed Answered

Python .cal for calculating distance between two lat lon

Question asked by JerryChase on May 26, 2015
Latest reply on May 28, 2015 by curtvprice

Can anyone give me some help on this Python field calculation? I'm passing two sets of Latitude and Longitude in order to calculate the distance between the two values.I get a general error when I run this. I have tried experimenting with a few things, but I haven't been able to get it to work. Any suggestions would be much appreciated!

 

Pre-Logic Script Code: [Dist_MemCh1 is the field name that is being calculated, Y_0, Y_1, X_0, X_1 are the fields that are being used to calculate the distance

Here is my code:

 

def Dist_MemCh1(Lat0, Lat1, Lon0, Lon1):

dlat = Lat0 - Lat1

dlon = Lon0 - Lon1

a = (math.sin(math.radians(dlat)/2 )*math.sin(math.radians(dlat)/2 )) +

math.cos(math.radians(Lat1) )*math.cos(math.radians( Lat0 )

)*(math.sin(math.radians(dlon)/2 )*math.sin(math.radians(dlon)/2 ))

c = 2*math.atan2( math.sqrt(a ),math.sqrt(1-a ))

H = c * 3958.756

return H

 

[Dist_MemCh1=this is the name of the field being calculated]

Dist_MemCh1( !Y_0!, !Y_1!, !X_0!, !X_1!)

Outcomes