scanning an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Claus Mygind
    Contributor
    • Mar 2008
    • 571

    scanning an array

    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?

    Code:
          aFoundCache = new Array(1,4)
          aFoundCache[1,1] = "WayPoint"
    	aFoundCache[1,2] = "Lat"
    	aFoundCache[1,1] = "Lon"
      	aFoundCache[1,1] = "Time"
    
          aTrackPoint = new Array(1,4)
          aTrackPoint[1,1] = "Lat"
          aTrackPoint[1,2] = "Lon"
          aTrackPoint[1,3] = "Date"
          aTrackPoint[1,4] = "Time"
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    how exactly are the lon/lat arrays defined?

    Code:
    // pseudo code
    // using x, y as search values
    // using lon, lat as reference values
    
    // using least squares
    for_each (// reference array) {
      stddev.push((lon - x)*(lon-x)+(lat-y)*(lat-y))
    }
    
    // find smallest value
    for_each (// stddev) {
      if (stddev[i]<stddev[i-1])
        var min = i;
    }
    
    // et voilà, reference[min] is your closest match

    Comment

    • Claus Mygind
      Contributor
      • Mar 2008
      • 571

      #3
      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.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        your server side app may have a method to find the smallest value easier (something like e.g. min() …)

        Comment

        • Claus Mygind
          Contributor
          • Mar 2008
          • 571

          #5
          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.

          Code:
             //Add time stamp of time found to cache  from nearest track point
                   //start at row 2 because row 1 is a header
                   for nE = 2 to ALEN( aFoundCache,1 )
          
          
                      aStd = new Array(1,2)
                      aStd[1,1] = "Std Val"
                      aStd[1,2] = "Time"
          
                      x = aFoundCache[nE,2]
                      y = aFoundCache[nE,3]
          
                      //start at row 2 because row 1 is a header
                      for nT = 2 to ALEN( aTrackPoint,1 )
                         lat = aTrackPoint[nT,1]
                         lon = aTrackPoint[nT,2]
                         tim = aTrackPoint[nT,4]
          
                         
                         aStd.grow(1)
                         aStd[ALEN( aStd,1 ),1] = (lon - x)*(lon-x)+(lat-y)*(lat-y)
                         aStd[ALEN( aStd,1 ),2] = tim
          
                      endfor
          
                      //start at the 2nd row, first row is a header
                      min = aStd[2,1]
          
                      for nS = 2 to ALEN( aStd,1 )
                         // find smallest value
                         min = iif(aStd[nS,1] < min, aStd[nS,1], min  )
                      endfor
          
                      cRow = aStd.subscript(aStd.scan(min),1)
                      aFoundCache[nE,4] = aStd[cRow,2]
          
                   endfor
          
                   aFoundCache.sort(4)

          Comment

          Working...