Thanks to everyone for their help with finding the nearest point.  I finally
ended up using a function provided by David Haycraft.  Because I was
searching in feet across a short area, I had to modify the function
slightly.  First, I had to change all units to "ft".  Second, in the strsql
statement, I changed the str$ conversions to Format$ conversions.  This was
necessary to preserve the precision on my Lat/Lon coords. The modified
function is below.  It works wonderfully!

Thanks again!

Dietrich


'---------------------------------------------------------
'Function Nearest
'   Returns the name  of the closest object in a search table
'   Input parameters
'      strTable: name of open search table
'      strTargetCol:  name of column containing feature names
'      fltXC,fltYC: Coordinates of where to center the search
'      fltRadiusMin: the initial search radius in ft
'      fltRadiusMax: the limiting search radius in ft
'   Output parameters
'      strItemName: Name of closest target feature
'      fltDist: distance in ft to closest target feature
'      fltXT,fltYT: centroid of closest target feature
'
' By:
'David M Haycraft
'Information Analysis Associates  Pty Ltd
'1 Cumming Place, Wanniassa, 2903
'Aust Capital Territory,  Australia
'A MapInfo Technology Partner
'David M Haycraft [[EMAIL PROTECTED]]
'----------------------------------------------------------
Function Nearest$ ( ByVal strTable as string, ByVal strTargetCol as string,
ByVal fltXC As Float, ByVal fltYC As Float,ByVal fltRadiusMin As Float,
ByVal fltRadiusMax As Float,
strItemName As String, fltDist as float, fltXT as float, fltYT as float) As
Logical

   Dim fltRadius as Float
   Dim fltTemp As Float
   Dim strSql as string
   Dim n as integer
   Dim aliCol as alias

   ' init
   set distance units "ft"
   Set Format NUMBER "9,999.9"
   fltRadius=fltRadiusMin

   ' build ID col ref
   aliCol="~temp."+strTargetCol

   ' Select all of the objects within fltRadius ft of fltXC,fltYC
   ' If there's nothing there, then double the radius and try again.
   ' Repeat until something is found, or radius exceeds limit
   n=0
   while (n=0) and (fltRadius<=fltRadiusMax)
      ' create and execute sql statement to search within a circular buffer
region
      strSql="Select * from " + strTable + " Into ~temp NoSelect " +
         "Where " + strTable +".Obj Within CreateCircle(" +

Format$(fltXC,"###.######")+","+Format$(fltYC,"###.######")+","+str$(fltRadi
us)+") "
      Note "strsql = " + strsql
      run command strSql
      ' get size of result set
      n=TableInfo(~temp,TAB_INFO_NROWS)
      if n=0 then
         ' nothing selected - double the radius for the next search
         fltRadius = fltRadius * 2
      end if
   wend

   ' Test to see if there was anything selected
   If n = 0 Then
      Nearest$ = False
      Exit Function
   End If

   ' Find closest feature in selection collection
   fltDist=Distance(0,0,180,0,"ft")
   Fetch first from ~temp
   while not EOT(~temp)
      ' get the distance to the selected object
      fltTemp = Distance(fltXC, fltYC, CentroidX(~temp.obj),
                        CentroidY(~temp.obj),"ft")
      ' is this closest so far?
      If (fltTemp < fltDist) Then
         ' replace feature details
         fltDist = fltTemp
         strItemName = aliCol
         fltXT=CentroidX(~temp.obj)
         fltYT=CentroidY(~temp.obj)
      End If
      fetch next from ~temp
   wend
   'close table ~temp

   ' return success
   Nearest$ = True

End Function
'---------------------------------------------------------




Dietrich Kastens
(785)843-7789 Home
(785)626-4600 Mobile
[EMAIL PROTECTED]
www.kastensinc.com



_______________________________________________________________________
List hosting provided by Directions Magazine | www.directionsmag.com |
To unsubscribe, send e-mail to [EMAIL PROTECTED] and
put "unsubscribe MapInfo-L" in the message body.

Reply via email to