Question : Determine distance from US border

I have a list of latitudes and longitudes for the US/Canadian border and need to determine if a particular lat/long pair (within Canada) is within 225 miles of the US border.  Obviously this is a fairly complicated line and the 225 miles could be in almost any direction from the point being checked.

This needs to be a generic solution because I need to run it many times over a large number of points.

I have considered two solutions, but have been unable to make either of them work.  Help with either would be greatly appreciated!

1.  Come up with a "new border" which includes all of the area within 225 miles of the border and then see if the point is inside this line.  I know that the code would have to generate a lot of arcs, see where they intersect, and just use the outline of this area to define the new line.  

2.  Calculate a 225 ring around the point and see if it intersects the polyline defining the border at any point.  I'm thinking that this would be easier, but am not sure how to start.

Attached is my excel file containing the lat/long's for the border in both degrees and radians.

Thanks in advance!
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
'This formula will probably come in handy (whether used to calculate points for a circle, or the expanded border):
 
'Destination point given distance and bearing from start point:
Public Function CalculateDistance()
'd is distance traveled in radians - 225 miles in this case
d = 225 / 3963.1676
'lat1 = Latitude of the starting position
lat1 = 0.706009923115762
'lon1 = Longitude of the starting position
lon1 = 1.14667647042346
'brg is the bearing from lat1/lon1 in radians
brng = 180 * Pi / 180
 
lat2 = ArcSin(Sin(lat1) * Cos(d) + Cos(lat1) * Sin(d) * Cos(brng))
lon2 = lon1 + Application.WorksheetFunction.Atan2(Cos(d) - Sin(lat1) * Sin(lat2), Sin(brng) * Sin(d) * Cos(lat1))
 
End Function
 
Function ArcSin(X As Double) As Double
    ArcSin = Atn(X / Sqr(-X * X + 1))
End Function
Open in New Window Select All

Answer : Determine distance from US border

Louis,

Attached is a new file. Enter the latitude and longitude in the blue cells and in the green cells it will tells you the closest distance those coordinates are from the border. It will also tell you the latitude and longitude of that location on the border that is closest.

On my rather old 1.6GHz, 0.5Gb RAM machine, it takes a few seconds to complete the calculations. However on a new faster machine it should give you the answer pretty well instantly - but do please check that INDEX has recalculated in cells J3 and K3.

I have added Conditional Formatting to column F so that the minimum value is highlighted in red. So that you don't have to search for that value I have put its row number in cell L3.

To ensure that you cover the straight-line sections of the border more comprehensively I believe you will need to add interpolated latitude and longitude data - and then edit the formulae that I have used so as to include the new data.

The distance per degree I have taken as the average between the global polar and equatorial circumferences - this makes it accurate to within 204 yards per degree - which should be adequate.

Hope that helps

Patrick
 
 
Random Solutions  
 
programming4us programming4us