Home All Groups Group Topic Archive Search About

Find Coords within X Radius

Author
24 Feb 2007 10:00 AM
Zairay
Hi All,

I need to run through a recordset with Lat Lon coordinates and find
out if they are within X radius from a certain coord.

The coords are build up like this: 321600 (degrees, minutes and
seconds).
The centergrid could be for example: 321600, 063312 and the radius 15
km.

Is there a formula to do this?

Regards

Marco

Author
24 Feb 2007 1:48 PM
Larry Serflaten
<Zai***@gmail.com> wrote

> I need to run through a recordset with Lat Lon coordinates and find
> out if they are within X radius from a certain coord.
>
> The coords are build up like this: 321600 (degrees, minutes and
> seconds).
> The centergrid could be for example: 321600, 063312 and the radius 15
> km.
>
> Is there a formula to do this?

The formula to use is Pythagoras' theorem.

Convert all measurements to a single scale (such as Seconds) and find the
difference in Lon and Lat (or X and Y) then use Pythagoras' theorem to see
if it falls within the desired radius.  Of course that says nothing about the
complexity added when also considering the curvature of the earth.  In short,
such a solution would be next to useless at or near the poles....

If the earth's curvature is not taken into account, the solution is simple as the
short demo below shows. For details on the more accurate formula pertaining
to a sphere, check out this link: http://www.reference.com/browse/wiki/Great-circle_distance

LFS

Option Explicit
Dim CX As Single
Dim CY As Single
Dim Radius As Single

Private Sub Form_Load()
    AutoRedraw = True
    ScaleMode = vbPixels
    CX = 100
    CY = 100
    Radius = 50
    Circle (CX, CX), Radius
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim DX As Single
Dim DY As Single

  DX = Abs(X - CX)
  DY = Abs(Y - CY)
  If Sqr(DX * DX + DY * DY) < Radius Then
    Caption = "Inside"
  Else
    Caption = "Outside"
  End If
End Sub
Author
24 Feb 2007 4:43 PM
Zairay
Show quote Hide quote
On 24 feb, 17:48, "Larry Serflaten" <serfla***@usinternet.com> wrote:
> <Zai***@gmail.com> wrote
>
> > I need to run through a recordset with Lat Lon coordinates and find
> > out if they are within X radius from a certain coord.
>
> > The coords are build up like this: 321600 (degrees, minutes and
> > seconds).
> > The centergrid could be for example: 321600, 063312 and the radius 15
> > km.
>
> > Is there a formula to do this?
>
> The formula to use is Pythagoras' theorem.
>
> Convert all measurements to a single scale (such as Seconds) and find the
> difference in Lon and Lat (or X and Y) then use Pythagoras' theorem to see
> if it falls within the desired radius.  Of course that says nothing about the
> complexity added when also considering the curvature of the earth.  In short,
> such a solution would be next to useless at or near the poles....
>
> If the earth's curvature is not taken into account, the solution is simple as the
> short demo below shows. For details on the more accurate formula pertaining
> to a sphere, check out this link:http://www.reference.com/browse/wiki/Great-circle_distance
>
> LFS
>
> Option Explicit
> Dim CX As Single
> Dim CY As Single
> Dim Radius As Single
>
> Private Sub Form_Load()
>     AutoRedraw = True
>     ScaleMode = vbPixels
>     CX = 100
>     CY = 100
>     Radius = 50
>     Circle (CX, CX), Radius
> End Sub
>
> Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
> Dim DX As Single
> Dim DY As Single
>
>   DX = Abs(X - CX)
>   DY = Abs(Y - CY)
>   If Sqr(DX * DX + DY * DY) < Radius Then
>     Caption = "Inside"
>   Else
>     Caption = "Outside"
>   End If
> End Sub

I do need to use the earth's curvature. What kind of formula do I need
for that.

Marco
Author
24 Feb 2007 5:07 PM
Larry Serflaten
<Zai***@gmail.com> wrote

> > > I need to run through a recordset with Lat Lon coordinates and find
> > > out if they are within X radius from a certain coord.
<...>
> > > Is there a formula to do this?

> > The formula to use is Pythagoras' theorem.
> >
> > If the earth's curvature is not taken into account, the solution is simple as the
> > short demo below shows. For details on the more accurate formula pertaining
> > to a sphere, check out this link: http://www.reference.com/browse/wiki/Great-circle_distance

> I do need to use the earth's curvature. What kind of formula do I need
> for that.

Check out what others have used.  Search the archives....
Here is one place to start:
http://groups.google.com/groups?as_q=+%22Great+Circle%22+Distance&num=50&scoring=r&hl=en&as_ugroup=*.vb.*&safe=off

LFS
Author
26 Feb 2007 11:54 PM
Karl E. Peterson
Zai***@gmail.com wrote:
> I need to run through a recordset with Lat Lon coordinates and find
> out if they are within X radius from a certain coord.
>
> The coords are build up like this: 321600 (degrees, minutes and
> seconds).
> The centergrid could be for example: 321600, 063312 and the radius 15
> km.
>
> Is there a formula to do this?

You're looking for a "Great Circle" algorithm.  Here's one that uses decimal
degrees:

   Public Function Distance() As Double
      Dim L1 As Double           ' Start Point Latitude in radians
      Dim L2 As Double           ' End Point Latitude in radians
      Dim N1 As Double           ' Start Point Longitude in radians
      Dim N2 As Double           ' End Point Longitude in radians
      Dim C As Double            ' Cosine of the angle subtended by the
                                 ' segment of the great circle path
                                 ' between the two points
      Dim A As Double            ' Angle derived from C
      Dim R As Double            ' Radius of the Earth

      ' Set radius to user's choice
      Select Case m_Units
         Case Kilometers
            R = 6378
         Case NauticalMiles
            R = 3444
         Case StatuteMiles
            R = 3963
      End Select

      ' Convert start/end points to radians
      L1 = m_Pt(Start).Latitude.Decimal * (Pi / 180)
      N1 = m_Pt(Start).Longitude.Decimal * (Pi / 180)
      L2 = m_Pt(Finish).Latitude.Decimal * (Pi / 180)
      N2 = m_Pt(Finish).Longitude.Decimal * (Pi / 180)

      ' Calculate C and A
      C = (Sin(L1) * Sin(L2)) + (Cos(L1) * Cos(L2) * Cos(N2 - N1))
      A = Atn(Sqr(1 - (C * C)) / C) + Pi * (C - Abs(C)) / (2 * C)

      ' Return A multiplied by the radius of the Earth
      Distance = A * R
   End Function


Just substitute your own radius, and convert your DMS strings to decimal degrees.
--
..NET: It's About Trust!
http://vfred.mvps.org