Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Distance 1

Status
Not open for further replies.

mariaor

Technical User
Jul 19, 2010
6
MX
Hello,
Now I need to calculate a distance between two points.
I have the next information:
Id Longitude Latitude
1 99.80286 16.92828
2 99.8032 16.9281
3 99.80405 16.92768

I have 2,500 records and need to generate a program in Visual Fox Pro 8 to calculate the distance between point 1 and 2, point 2 and 3, point 3 and 4...
The formula is:
6378.8*arccos(sen(latitude1)*sen(latitude2)+cos(latitude1)*cos(latitude2)*cos(longitude2- longitude1))

Any suggestions?
Tahnks
 
My problem is I don´t know how to use it in Visual Fox Pro since I have to use the record with id=1 (latitude1) and id=2 (latitude2) How can I do that?
Thanks.
 

Code:
USE MyTable
SELECT MyTable  && Assumption Index on ID Already Exists
* --- Acquire Long & Lat #1 ---
SEEK 1
nLat1 = MyTable.Latitude
nLong1 = MyTable.Longitude

* --- Acquire Long & Lat #2 ---
SEEK 2
nLat2 = MyTable.Latitude
nLong2 = MyTable.Longitude

* --- Now calculate the difference using the Memory Variables ---
nDistance = 378.8*arccos(sen(nLat1)*sen(nLat2)+cos(nLat1)*cos(nLat2)*cos(nLong2- nLong1))

You can either write a program to sequence through the various geographical points or develop a utility to allow the user to pick Points 1 & 2 and then display the result.

Good Luck,
JRB-Bldr
 
NOTE - the imperial distance calculation (I am not using metric) that I use is slightly different than yours.

Miles = (ACOS((SIN(nLat1) * SIN(DTOR(VAL(nLat2)))) + ;
(COS(nLat1) * COS(DTOR(VAL(nLat2))) * COS(DTOR(VAL(nLong2)) - nLong1))) * 3963)

Good Luck,
JRB-Bldr
 
If you need high sensitivity:
Code:
Create Cursor geoInfo (Id i,     Longitude b(6),     Latitude b(6))
Insert Into geoInfo Values (1,        99.80286,    16.92828)
Insert Into geoInfo Values (2,       99.8032,        16.9281)
Insert Into geoInfo Values (3,       99.80405,     16.92768)

Select t1.Id As fromID, t2.Id As toID, ;
  t1.Longitude As fromLong, ;
  t1.Latitude As fromLat, ;
  t2.Longitude As toLong, ;
  t2.Latitude As toLat, ;
  GeoDistanceInKm(t1.Latitude, t1.Longitude, t2.Latitude, t2.Longitude) As km ;
  FROM geoInfo t1 ;
  INNER Join geoInfo t2 On ;
  t1.Id < t2.Id

Procedure GeoDistanceInKm(tnLat1, tnLong1, tnLat2, tnLong2)
  #Define EquatorialRadius 6378 && Kilometers
  #Define PolarRadius 6357 && Kilometers
  #Define Eccentricity Sqrt(1 - PolarRadius^2/EquatorialRadius^2)

  #Define RadiusOfEarth1 EquatorialRadius * Sqrt(1 - Eccentricity^2) / (1 - Eccentricity^2 * (Sin(m.tnLat1))^2)
  #Define RadiusOfEarth2 EquatorialRadius * Sqrt(1 - Eccentricity^2) / (1 - Eccentricity^2 * (Sin(m.tnLat2))^2)
  #Define RadiusOfEarth (RadiusOfEarth1+RadiusOfEarth2) / 2
  Return 2 * RadiusOfEarth *  ;
    asin( Min(1, ;
    sqrt((Sin((Dtor(m.tnLat2) - Dtor(m.tnLat1))/2))^2 + ;
    cos(Dtor(m.tnLat1)) * Cos(Dtor(m.tnLat2)) * ;
    (Sin((Dtor(m.tnLong2) - Dtor(m.tnLong1))/2))^2)))
ENDPROC

For 2500 records this would take under 30 seconds on an average computer. If you need a ready library and faster results then I suggest you to install and use SQL Server 2008 R2 Express. It is available for free + it contains spatial data type and related GIS methods (OGC standards included).

Cetin Basoz
MS Foxpro MVP, MCP
 
Of course these functions assume your are a bird, not a car. It does not calculate the land distance folowing actual roads.

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
ReFox XI (www.mcrgsoftware.com)
 
Mike,
If you have GIS data plus some tool like Esri MapObjects (or any other GIS packages) you would get address to address on land distances and directions:) (and probably one who needs it, could get all the information from a map service on the internet)


Cetin Basoz
MS Foxpro MVP, MCP
 
Cetin,

Automating MapPoint would also give you a more acurate answer, optimizing is something it does well.

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
ReFox XI (www.mcrgsoftware.com)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top