By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,527 Members | 1,184 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,527 IT Pros & Developers. It's quick & easy.

scanning an array

Claus Mygind
100+
P: 571
I have two arrays each contains two columns. lon and lat.

I want to use the lon and lat values from the one array to look and find the nearest matching values in the 2nd array.

Here is some sample data of longitude and latitude.

lat="39.7553670" lon="-88.8309830"
lat="39.7874000" lon="-89.1040170"
lat="39.8309670" lon="-89.0850330"
lat="40.1745330" lon="-88.2416830"

I will never have exact matching values, so I just want to find the best fit. The best fit in the 2nd array may be either a slightly higher or lower value than the search value.

I want to extract a value from a third column from the 2nd array and place it into a similar column of the first array.

Any suggestions on how that might be coded?

Expand|Select|Wrap|Line Numbers
  1.       aFoundCache = new Array(1,4)
  2.       aFoundCache[1,1] = "WayPoint"
  3.     aFoundCache[1,2] = "Lat"
  4.     aFoundCache[1,1] = "Lon"
  5.       aFoundCache[1,1] = "Time"
  6.  
  7.       aTrackPoint = new Array(1,4)
  8.       aTrackPoint[1,1] = "Lat"
  9.       aTrackPoint[1,2] = "Lon"
  10.       aTrackPoint[1,3] = "Date"
  11.       aTrackPoint[1,4] = "Time"
  12.  
Oct 9 '09 #1
Share this Question
Share on Google+
4 Replies


Dormilich
Expert Mod 5K+
P: 8,639
how exactly are the lon/lat arrays defined?

Expand|Select|Wrap|Line Numbers
  1. // pseudo code
  2. // using x, y as search values
  3. // using lon, lat as reference values
  4.  
  5. // using least squares
  6. for_each (// reference array) {
  7.   stddev.push((lon - x)*(lon-x)+(lat-y)*(lat-y))
  8. }
  9.  
  10. // find smallest value
  11. for_each (// stddev) {
  12.   if (stddev[i]<stddev[i-1])
  13.     var min = i;
  14. }
  15.  
  16. // et voilà, reference[min] is your closest match
Oct 9 '09 #2

Claus Mygind
100+
P: 571
Actually I made a mistake. The code I am using here is in my server side app. But the fundamentals you describe should work just the same. So I should be able to apply your excellent code. Thank very much.
Oct 9 '09 #3

Dormilich
Expert Mod 5K+
P: 8,639
your server side app may have a method to find the smallest value easier (something like e.g. min() …)
Oct 9 '09 #4

Claus Mygind
100+
P: 571
Nope what you provided was exactly what I needed.I would have had to write to disk in a table. I thank you very much. I was familiar with the formular so it was very easy to implement. You saved me a bunch of time.

I was even able to add a sort to put the points in proper order

If you like here is my server sided code which I derived from your example.

Expand|Select|Wrap|Line Numbers
  1.    //Add time stamp of time found to cache  from nearest track point
  2.          //start at row 2 because row 1 is a header
  3.          for nE = 2 to ALEN( aFoundCache,1 )
  4.  
  5.  
  6.             aStd = new Array(1,2)
  7.             aStd[1,1] = "Std Val"
  8.             aStd[1,2] = "Time"
  9.  
  10.             x = aFoundCache[nE,2]
  11.             y = aFoundCache[nE,3]
  12.  
  13.             //start at row 2 because row 1 is a header
  14.             for nT = 2 to ALEN( aTrackPoint,1 )
  15.                lat = aTrackPoint[nT,1]
  16.                lon = aTrackPoint[nT,2]
  17.                tim = aTrackPoint[nT,4]
  18.  
  19.  
  20.                aStd.grow(1)
  21.                aStd[ALEN( aStd,1 ),1] = (lon - x)*(lon-x)+(lat-y)*(lat-y)
  22.                aStd[ALEN( aStd,1 ),2] = tim
  23.  
  24.             endfor
  25.  
  26.             //start at the 2nd row, first row is a header
  27.             min = aStd[2,1]
  28.  
  29.             for nS = 2 to ALEN( aStd,1 )
  30.                // find smallest value
  31.                min = iif(aStd[nS,1] < min, aStd[nS,1], min  )
  32.             endfor
  33.  
  34.             cRow = aStd.subscript(aStd.scan(min),1)
  35.             aFoundCache[nE,4] = aStd[cRow,2]
  36.  
  37.          endfor
  38.  
  39.          aFoundCache.sort(4)
  40.  
Oct 10 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.