3

I am trying to figure out if a latitude/longitude point is contained within a polygon defined by vertexes that represent points on the earth (also lat/lon's, in clockwise order). This is trivial for polygons that can be mapped to the 2D lat/lon space.

Where this becomes increasingly difficult is circle's (now switching back to 3D) that may go from pole to pole covering half the earth. The translation to lat/lon looks like a sine wave. The 2D point in polygon test no longer applies to this case. Is there an algorithm that exists that solves this problem?

================== Clarifications on comments below: =================== The polygon is defined as (lon, lat) pairs in degrees, i.e., (60, 90), (60, 110), (-30, 110), (-30, 90).

I do have code that implements the ray casting algorithm, and that works. however, certain polygons on the surface of the earth do not translate to closed polygons in the 2D space.

j0k
  • 22,600
  • 28
  • 79
  • 90
BigBrownBear00
  • 1,378
  • 2
  • 14
  • 24
  • Can you give an example of how you're defining your polygon, your point, and the expected result? Also, do you have any code that you've tried? (edit your question to include this information if you can) – Wilduck Nov 18 '11 at 19:30
  • Your definition of clockwise labeled points defines which side of the polygon is the is inside. – Zak Nov 18 '11 at 19:55
  • This question is language-independant -- python tag removed. – John Machin Nov 18 '11 at 20:50

3 Answers3

1

As stated by denniston.t, if you are only interested in circles, and you have a radius, you can simply check if the Great Circle Distance between the center point and the point is less than the radius. To find the great circle distance you typically use the Haversine Formula. The following is my implementation in python:

from math import radians, sin, cos, asin, sqrt

def haversine(point1, point2):
    """Gives the distance between two points on earth.

    The haversine formula, given two sets of latitude and longitude,
    returns the distance along the surface of the earth in miles,
    ignoring potential changes in elevation. The points must be in
    decimal degrees.
    """
    earth_radius_miles = 3956
    lat1, lon1 = (radians(coord) for coord in point1)
    lat2, lon2 = (radians(coord) for coord in point2)
    dlat, dlon = (lat2 - lat1, lon2 - lon1)
    a = sin(dlat/2.0)**2 + cos(lat1) * cos(lat2) * sin(dlon/2.0)**2
    great_circle_distance = 2 * asin(min(1,sqrt(a)))
    d = earth_radius_miles * great_circle_distance
    return d
Wilduck
  • 13,822
  • 10
  • 58
  • 90
0

If you have the center point and radius of your circle drawn on the surface of the sphere, calculate the Great-circle distance between the center point and target point. If it's less than the radius of the circle, the target point lies in the circle.

This will not generalize to arbitrary polygons drawn on your sphere, but you only asked about circles, so I don't know if it matters to you.

tdenniston
  • 3,389
  • 2
  • 21
  • 29
  • Thanks for the reply. Is there a general solution for any sort of polygon? I am not necessarily going to be bound to circles for the polygon.. – BigBrownBear00 Nov 20 '11 at 13:21
-1
containsLocation(point:LatLng, polygon:Polygon)
Mohit Bumb
  • 2,466
  • 5
  • 33
  • 52