Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Sweetpotato
Message
General information
Forum:
Visual FoxPro
Category:
Other
Title:
Miscellaneous
Thread ID:
01395961
Message ID:
01396063
Views:
297
This message has been marked as the solution to the initial question of the thread.
My friend Dave Bernard gave me the code for this. I called it distance.prg
*FUNCTION distance
*SCATTER memvar
*SELECT * FROM MAPWANTS WHERE 

*----------------------------------------------------------------------*
* Given a pair of lattitude and longitudes, return the approximate
* distance in nautical miles between points A and B.
*
* P_FUNC  1 = DD:MM:SS format, 2 = DD.MMMM format
* P_LT1   Lattitude of point A
* P_LG1   Longitude of point A
* P_LT1   Lattitude of point B
* P_LG2   Longitude of point B
* P_TYPE  0=Nautical Miles, 1=Statute Miles, 2=Kilometers
*
* All lattitude and longitude values are passed as strings in function 1
* and as numbers in function 2.
*
* Assumes N lattitude and W longitude
*
* Examples:
* a = Distance(1, "33:57:00", "118:24:00", "40:38:00", "73:47:00", 2)
* a = Distance(2, 33.95, 118.4, 40.6333, 73.7833, 2)
*
* Test with http:www.indo.com/distance
*
*----------------------------------------------------------------------*
	Function Distance
	Parameters p_func, p_lt1, p_lg1, p_lt2, p_lg2, p_type

	Private p_func, p_lt1, p_lg1, p_lt2, p_lg2, p_svdec, p_pi,;
		p1_degN, p1_minN, p1_secN, p1_degW, p1_minW, p1_secW,;
		p2_degN, p2_minN, p2_secN, p2_degW, p2_minW, p2_secW,;
		p_lat1, p_lon1, p_lat2, p_lon2, p_dist, p_type

	p_svdec = Set("decimals")

	Set Decimals To 9

	If Type("p_type") <> "N"
		p_type = 0
	Endif

	If p_type > 2
		p_type = 0
	Endif

	p_pi = Pi()

	Do Case
	Case p_func = 1
		p_lt1 = p_lt1 + ":00"
		p_lg1 = p_lg1 + ":00"
		p_lt2 = p_lt2 + ":00"
		p_lg2 = p_lg2 + ":00"

		p1_degN = Val(Left(p_lt1, At(":", p_lt1) - 1))
		p_lt1   = Substr(p_lt1, At(":", p_lt1) + 1)
		p1_minN = Val(Left(p_lt1, At(":", p_lt1) - 1))
		p_lt1   = Substr(p_lt1, At(":", p_lt1) + 1)
		p1_secN = Val(p_lt1)

		p1_degW = Val(Left(p_lg1, At(":", p_lg1) - 1))
		p_lg1   = Substr(p_lg1, At(":", p_lg1) + 1)
		p1_minW = Val(Left(p_lg1, At(":", p_lg1) - 1))
		p_lg1   = Substr(p_lg1, At(":", p_lg1) + 1)
		p1_secW = Val(p_lg1)

		p2_degN = Val(Left(p_lt2, At(":", p_lt2) - 1))
		p_lt2   = Substr(p_lt2, At(":", p_lt2) + 1)
		p2_minN = Val(Left(p_lt2, At(":", p_lt2) - 1))
		p_lt2   = Substr(p_lt2, At(":", p_lt2) + 1)
		p2_secN = Val(p_lt2)

		p2_degW = Val(Left(p_lg2, At(":", p_lg2) - 1))
		p_lg2   = Substr(p_lg2, At(":", p_lg2) + 1)
		p2_minW = Val(Left(p_lg2, At(":", p_lg2) - 1))
		p_lg2   = Substr(p_lg2, At(":", p_lg2) + 1)
		p2_secW = Val(p_lg2)

		p_lat1 = (p1_degN + ((p1_minN + (p1_secN / 60)) / 60)) * p_pi / 180
*                    p_pi * (p1_degN + (p1_minN / 60) + (p1_secN / 3600)) / 180
		p_lon1 = (p1_degW + ((p1_minW + (p1_secW / 60)) / 60)) * p_pi / 180
		p_lat2 = (p2_degN + ((p2_minN + (p2_secN / 60)) / 60)) * p_pi / 180
		p_lon2 = (p2_degW + ((p2_minW + (p2_secW / 60)) / 60)) * p_pi / 180

	Case p_func = 2
		p1_degN = p_lt1
		p1_minN = 0
		p1_secN = 0
		p1_degW = p_lg1
		p1_minW = 0
		p1_secW = 0
		p2_degN = p_lt2
		p2_minN = 0
		p2_secN = 0
		p2_degW = p_lg2
		p2_minW = 0
		p2_secW = 0

		p_lat1 = p1_degN * p_pi / 180
		p_lon1 = p1_degW * p_pi / 180
		p_lat2 = p2_degN * p_pi / 180
		p_lon2 = p2_degW * p_pi / 180

	Otherwise
		Set Decimals To &p_svdec
		Return 0
	Endcase

***Method 1
	p_dist = Acos((Sin(p_lat1) * Sin(p_lat2)) + (Cos(p_lat1) * Cos(p_lat2) * Cos(p_lon1 - p_lon2)))
	p_dist = Int((p_dist * 180 * 60 / p_pi) +.5)		&& Distance in Nautical Miles

***Method 2, with conversion to UTM (Mercator) coordinates
	p_er = 6371.315				&& Average radius of the Earth

***Assume N lattitude and W longitude
***  For S lattitude: p_radlat1 = (p_pi / 2) + p_lat1
***  Others remain the same
	p_radlat1 = (p_pi / 2) - p_lat1
	p_radlon1 = (p_pi * 2) - p_lon1
	p_radlat2 = (p_pi / 2) - p_lat2
	p_radlon2 = (p_pi * 2) - p_lon2

***Spherical coordinates: x=r*cos(long)sin(lat), y=r*sin(long)*cos(lat), z=r*cos(lat)
	p_x1 = p_er * Cos(p_radlon1) * Sin(p_radlat1)
	p_y1 = p_er * Sin(p_radlon1) * Sin(p_radlat1)
	p_z1 = p_er * Cos(p_radlat1)

	p_x2 = p_er * Cos(p_radlon2) * Sin(p_radlat2)
	p_y2 = p_er * Sin(p_radlon2) * Sin(p_radlat2)
	p_z2 = p_er * Cos(p_radlat2)

	p_d = Sqrt((p_x1 - p_x2)^2 + (p_y1 - p_y2)^2 + (p_z1 - p_z2)^2)

***Side, side, side, law of cosines and arccos
	p_theta = Acos(((p_er^2 * 2) - (p_d^2)) / (p_er^2 * 2))

***These need to be rounded
	p_dist2 = p_theta * p_er				&& Distance in Kilometers
	p_dist3 = p_dist2 / 1.609344			&& Distance in Miles
	p_dist4 = p_dist2 / 1.852			&& Distance in Nautical Miles

	Set Decimals To &p_svdec

	Do Case
	Case p_type = 0
		Return p_dist
	Case p_type = 1
		Return p_dist3
	Case p_type = 2
		Return p_dist2
	Endcase

	Return p_dist
John Harvey
Shelbynet.com

"I'm addicted to placebos. I could quit, but it wouldn't matter." Stephen Wright
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform