Question

Ok, so I am still new to asking questions on Stack Overflow, so please be nice :)

I have an issue that I have been trying to solve for a month now with failed results time and time again. I have found various questions somewhat similar to this, but nothing that has struck me as the definitive answer.

The closest that I have found so far is this: Convert GPS coordinates to coordinate plane.

In short, what I need is to know is if a GPS Coordinate is either on or inside an ellipse.

KNOWNS:

  • Width of Ellipse in meters
  • Height of Ellipse in meters
  • GPS coordinate of Ellipse center (Presumably [0,0] on a coordinate system)
  • GPS coordinate of test point
  • Angle of point to test
  • Distance to test point

UNKNOWNS / NEEDS:

  • GPS Coordinate of point on ellipse with test point angle
  • Distance to point on ellipse with test point angle

Please assist as for some reason, I just cannot wrap my brain around this math.


UPDATE: To add more information, the values are all quite small in the grand scheme of things.

As an example: If a user wants to know if another user has entered some park/field/geofenced area or some other type of physical area. The physical area in this case is designed as an Ellipse.

As an addition, this is written in Objective-C. Below, you will see a random "+90" degrees, this is there as the mechanisms underlying see 0 degrees as North (Navigation) when I want it to be the Unit-Circle "Normal" .

Associated Code with Discussion:

- (BOOL)isLocation:(CLLocation *)location withinEllipse:(Ellipse *)ellipse
{
    BOOL locationIsWithinEllipse = NO;
    double ellipseWidth = ellipse.width;
    double ellipseHeight = ellipse.height;

    CLLocationDegrees locationAngleDegrees = 360 - ((int)([self headingBetweenCoordinate1:location.coordinate coordinate2:ellipse.locationCenter.coordinate] + 90) % 360);//Invert Direction
    double xOffsetInMeters = (ellipseWidth/2) * COS(DEGREES_TO_RADIANS(locationAngleDegrees));
    double yOffsetInMeters = (ellipseHeight/2) * SIN(DEGREES_TO_RADIANS(locationAngleDegrees));



    // The logic below will currently grab the Top-Right point as if it were a box, not the point on the Ellipse,
    // This is where things are broken. I need this to be the GPS Coordinate of the Point on the Ellipse with angle (locationAngleDegrees)

    // Grab the Coordinate on the Ellipse in the heading of the Test Point
    CLLocationDegrees pointLat = [ellipse.locationCenter addToLocationDistanceInMeters:yOffsetInMeters withBearingInDegrees:0].coordinate.latitude;
    CLLocationDegrees pointLong = [ellipse.locationCenter addToLocationDistanceInMeters:xOffsetInMeters withBearingInDegrees:90].coordinate.longitude;
    CLLocation * testPointOnEllipseLocation = [[CLLocation alloc] initWithLatitude:pointLat longitude:pointLong];




    // Just check if the Test Point is closer than the Distance of the Ellipse Point
    if(ABS([location distanceFromLocation:ellipse.locationCenter]) <= ABS([testPointOnEllipseLocation distanceFromLocation:ellipse.locationCenter]))
    {
        locationIsWithinEllipse = YES;
    }

    return locationIsWithinEllipse;
}


UPDATE:

I am still trying to get this math correct. I understand how to get it done with "school math", but how can I apply that to my code in the example? Also, I seriously don't understand all of this rotation stuff as I have written all of my code to be agnostic of any rotation. I believe that is all handled in Apple's low-level location stuff.

Can anyone please assist?

Was it helpful?

Solution 2

I have solved this by using some mathematical calculations from Google and guidance from the (very helpful) following question: How to determine if a latitude & longitude is within an ellipse

I also used this reference for the Foci calculations: http://www.mathopenref.com/ellipsefoci.html

To quote, from Cody in the link, the primary logic: "My ellipse is relatively small so I assumed it was a true (flat) ellipse. I was able to locate the lat lon of the foci of the ellipse then if the sum of the distances from the point of interest to each focus is less than 2a (the major axis radius), then it is within the ellipse."

I do appreciate the suggestions presented! Thank You!

OTHER TIPS

One method to solve such tasks always work:
1) transform lat,lon to a meter based cartesian coordinate system (4-5) lines of code.
In your case I would use the ellipse center as "center" in the transformation code (e.g the cylindrical equidistant projection needs a center latitude)

2) then use school mathematics, to solve the task: use the ellipse equation.

That all asumes that the ellipse is axe parallel to aequator. If not update your question.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top