0

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?

Community
  • 1
  • 1
jhthorp
  • 151
  • 1
  • 2
  • 9
  • GPS coordinates will presumably be in Latitude and Longitude? And presumably your ellipse centre will be in Latitude and Longitude as well? Hence the assertion that the ellipse centre is at [0,0] seems strange to me. Also, knowing the height and width of the ellipse isn't sufficient, because you also need how its rotated relative to the latitude and longitude co-ordinate system. For example, the width might correspond to latitude, and the height might correspond to longitude, but you could have an ellipse rotated at e.g. 45 degrees from that, so you need to specify. – Stochastically Apr 15 '13 at 08:43
  • Yes, the Ellipse center is in GPS Coordinate (Lat/Lon) as well as the Test Point Coordinate. My presumption of a (0,0) center was for the math related. I have always seen unit circles, etc based from a (0,0) point for simple calculations, that is why I wrote that. Currently, I sync up Width with Lat and Height with Lon for other calculations. I don not understand what you mean by relative rotation? – jhthorp Apr 15 '13 at 14:56
  • I have updated my question, can you please take a look? – jhthorp Apr 15 '13 at 15:26
  • Also, I apologize for the bad typing above. I believe that my wireless keyboard batteries are dying... – jhthorp Apr 15 '13 at 15:33
  • For the relative rotation, what I mean is does the long axis of the ellipse point North-South or NorthEast-SouthWest or NorthWest-SouthEast, or East-West, or something in between one of those? – Stochastically Apr 15 '13 at 18:18
  • It should be "Normal". Here is documentation for the coordinate system that I am using: http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/LocationAwarenessPG/MapKit/MapKit.html (See section on "Understanding Map Geometry") It uses "Mercator map projection" and considers the world map "Flat". – jhthorp Apr 15 '13 at 18:32
  • about rotation, of course you must reference to a relative geographic rotation angle, that is clockwise, not marhematic rotation angle which is counter clockwise. – AlexWien Apr 17 '13 at 09:12
  • do you this task for yourself, or for a company who pays for that task? – AlexWien Apr 17 '13 at 09:13
  • I don't see the relevance to your question, but this project is just for fun. There is no real end goal besides experimentation with Geofencing. – jhthorp Apr 17 '13 at 14:18

2 Answers2

0

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.

AlexWien
  • 28,470
  • 6
  • 53
  • 83
  • I would say that you are right in line with my current thinking. I am just struggling on the conversion of the Lat/Lon to a Meter-based system in order to perform the school math. I have updated my question, can you please take a look? – jhthorp Apr 15 '13 at 15:25
  • Use cYlindrical equidistant projection to transform lat lon to x,y in meters. first try without rotated elipses. – AlexWien Apr 16 '13 at 00:35
0

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!

Community
  • 1
  • 1
jhthorp
  • 151
  • 1
  • 2
  • 9