[Math] Check if latitude & longitude coordinates are inside a specific range of a latitude longitude polygon

polygons

Since I'm not a mathematician I came here to ask what the most efficient way is to check if latitude and longitude coordinates are inside a range (for example 50 meters) of multiple latitude and longitude points (polygon).

I have a list of these longitude and latitude points:

[6.38537265,51.87721088],[6.38542453,51.87737201],[6.38523252,51.87739419],[6.38477205,51.87745015],[6.38426164,51.87751088],[6.38391099,51.87755068],[6.38386033,51.87738808],[6.38380232,51.87720004],[6.38376297,51.87708017],[6.38375183,51.87704018],[6.38373055,51.8769829],[6.38390723,51.87695904],[6.38389144,51.87691388],[6.38403002,51.87690001],[6.38440124,51.8768538],[6.38493939,51.87678787],[6.38522535,51.87675316],[6.38529885,51.87697928],[6.38537265,51.87721088]

And would like to check if these coordinates are in the range:

51.877368,6.383818

Here's a sketch of my question to visualize what I mean.
Arrows represent a given latitude and longitude combination.

Click here for the image of my sketch

I was planning to use the haversine formula but I do not know how I could use that in a list of multiple points.

Thanks in advance!

Best Answer

This is the start of a general answer, but not yet complete. Still, I'm going to save it and continue work later, in part because I believe that the careful reformulation of the problem may well be the largest contribution I can make.


Presumably you're doing this in software. So I'm gonna write pseudocode in a matlab-like language. It's not pretty, but it'll do.

Inputs: 
  r: The radius of the spherical approximation of earth, in some units
  d: the distance that the test point needs to be within to produce "true"; same units. 
  p: an n x 2 array of points on the sphere, in lat/lon coordinates, with 
     90 degrees being the north pole, -90 being the south pole, etc. 
  q: a 1 x 2 array consisting of the lat and lon of the "user" or "test point"

Assumptions: 
  1. The earth is spherical enough, and the terrain flat enough, that lat-lon 
  distance between points is a good approximation of absolute distance. A 
  failure-case for this would be at half-dome in Yosemite, where two people could be 
  100 yards apart when radially  projected onto the spherical approximation of the
  earth, but many hundreds of yards apart in reality, one at the top of half-
  dome, the other standing on the ground below. 

  2. The polygon defined by the points in the array p is defined so that 
  traversing the polygon edges in the order p1, p2, p3, ..., the interior
  of the polygon is always to our left. There are no duplicated points,
  and the polygon forms a simple closed curve on the surface (i.e., there are
  no self-intersections). 

  3. No two points of the polygon are more than 90 degrees distant on the 
  sphere (I'm not certain I'll use this, but just in case...); the entire 
  polygon is contained within some hemisphere (also not sure i need this,
  but...)

  4. The test-point is within a distance of 90 degrees of all the polygon 
  vertices. 

  5. If the input points are written in degrees, then the cosine and sine and 
  other trig functions work with degrees; if the inputs are in radians, then 
  trig functions work with radians. 

Output: TRUE if either one of the following two conditions holds:
    A. the test point q is within the polygon defined by the points p OR
    B. q is within spherical distance d of one of the polygon edges
  and FALSE otherwise.

// ---- A few helper functions ----

// convert lat-lon coordinates into an xyz triple representing the 
// corresponding point on the unit sphere. 
function [x, y, z] = rect_from_polar(lat, lon)
   y = sin(lat); 
   x = cos(lat) * cos(lon); 
   z = cos(lat) * sin(lon); 

function [lat, lon] = polar_from_rect(x, y, z)
  lat = arcsin(y); 
  if (y == 1) or (y == -1) {
     lon = 0; 
  }
  else {
     lon = atan2(z, x);
  }