Question : Help in creating a query/function in Access....

Hi all,
         I am developing a software in VB6 and using MSAccess 2000 as the database. In my database I have a table called WayPoint. It stores the Latitude,Longitude and some other fields of different Waypoints of interest. There are roughly 5,000 records in this table. Now in my VB6 program I need to select the waypoint (record) that is nearest to a location (latitude, longitude) selected by a user. I know how to calculate distance between two pairs of Latitudes and Longitudes.
Now to rackle this problem two ideas came to me. I tested both of them and both are working......

IDEA 1::

This is really the most basic(but not efficient) idea. In my VB6 program I simply do (Algorithm)

Select * from WayPoint
Now check the distance of each record's Latitude and Longitude from the known Coordinates.This will be done by a simple function like
CheckDistance(MyLat,MyLong,WayPoint.Latitude,WayPoint.Longitude)
Select the record with the least distance from the given coordinates

In this technique I have to check the distance of all the 5000 records. So I improved the technique and used the following logic......

In the second logic I select only those records which are within an offset of the given coordinates. Then based upon the No. of records returned by the query I adjust the offset until I get less then 20 records from which calculating the nearest waypoint is not so difficult.......

While (1)
  strsql = "Select NumericWP,PLACENAME,LATITUDE,LONGITUDE from " & CTable & " where (LONGITUDE  between " & (long1 + Offset) & " and " & (long1 - Offset) & ") AND (LATITUDE between " & (lat1 + Offset) & " and " & (lat1 - Offset) & " ) "
  WayRS.Open strsql, ADOConnection, adOpenKeyset, adLockOptimistic
   
        Select Case WayRS.RecordCount
       
            Case 0                              'No record Found
                Offset = Offset + 0.1           'Add 10 Km          
            Case 1                              'Ideal Case
               ' Extract that record
                Exit Sub
            Case 2 To 20                        'Also not bad
           
               'loop through each of these records and then find the closest one using the function
               'CheckDistance(MyLat,MyLong,WayPoint.Latitude,WayPoint.Longitude)
             
              WayRS.Close
              Exit Sub
            Case Is > 20                       'Maybe in a city. So close down the offset.....
                Offset = Offset - 0.01              'Subtract 1 Km from Offset
        End Select
        DoEvents
            WayRS.Close  
    Wend

This technique is also working. But I need a faster technique....

----------------------------------------------------------------------------------------------------------------------------------------
 I need something like this. I need to pass a Lat,Long in a query and get the nearest waypoint (whose distance is least from the passed Lat,Long) directly through a query???? i.e a query that will always return one record and that will be the nearest waypoint. Can I put a distance checking function inside access?? and somehow call it from VB6 to return me the nearest waypoint?
----------------------------------------------------------------------------------------------------------------------------------------
The formula used to calculate distance between two points is
2*arcsin((sqrt(sin(lat0)-sin(lat1))²+(cos(lat0)*sin(lon0)-cos(lat1)*sin(lon1))²+(cos(lat0)*cos(lon0)-cos(lat1)*sin(lon1))²)/2)
but I can simplify this formula (with some loss of accuracy to eliminate arcsin and even eliminate cos and sins etc.

I hope someone will be able to understand my problem and then post a good soultion...... If you cant understand what I have written above then please do post so that I can explain my question more precisely.....

Imran Arshad

Answer : Help in creating a query/function in Access....

Hey, Imran, I remember this...
It has been a bit since I worked on this subject, but I do have a couple of questions:
What is the gain in processing time when you do while...wend before passing the values to the CheckDistance function?

You might gain some speed by not using full precision on your waypoint lat/long and mypoint lat/long.  Depends on how far apart your waypoints are.  Tyypically, we use a double to store the lat long, although a single might be enough, and then trim the value to fewer decimal places.

The function I created was primarily to ease entry of the formula into queries.  Not sure if there was any speed gained, except maybe from compiling.  

I need to go back to the function again to see if anything could be optimized.  
Random Solutions  
 
programming4us programming4us