Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Latitude, Longitude, polygons, and points.

Posted on 2008-06-20
Medium Priority
Last Modified: 2009-11-12

I am trying to figure out a way to determine whether a give point on a map (literally anywhere on earth) falls within the bounds of a polygon drawn from a list of ordinal lat/lon points.  The polygon could have any number of sides, as long as none intersect.

I am guessing that starting by converting the lat/lon values (which also have a N, S, E, or W associated with them) into a totally numerical representation is how I should start.  If that is correct, what is the best way to do it?

For the record, I already have my lat/lon values converted to a decimal number rather than minutes and seconds, etc.

Does anyone have a good algorithm to determine if the point is inside a polygon that is mapped on the surface of the earth?  I am looking for a very precise way to do this, as the precision is important in this case.
Question by:Perplexity
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +4

Accepted Solution

JayeshKitukale earned 600 total points
ID: 21832613
You need to use one of the algos presented here:
LVL 23

Expert Comment

by:Christopher Kile
ID: 21834976
You have a special condition here that is not taken into account by the algorithms in the page to which JayeshKitukale's excellent link points:  since you are working on a closed surface (the flattened sphere of Earth), some sets of your points might form polygons which overlap themselves.  Are your polygons liable to do that?  

Author Comment

ID: 21835412
Thanks for the responses thus far...if I understand the question correctly, the answer is that there should be no self-overlapping polygons.  The points that comprise each polygon are kept in order and no lines can intersect another line in a given polygon.

With that assumption, are those the algorithms to make the determination whether or not a set of coordinates falls within the bounds of a polygon formed by a given ordered set of coordinates?

Thanks again for the guidance.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Assisted Solution

JayeshKitukale earned 600 total points
ID: 21836216
Yes these alogorithms are appropriate for your purpose. Use the 2D alogorithms and not the 3D ones for this purpose. The flattened map of earth is actually 2 dimensional and subject to all 2D calculations.
LVL 27

Expert Comment

ID: 21836535
"The points that comprise each polygon are kept in order and no lines can intersect another line in a given polygon"
But every adjacent pair of lines intersect ie have a common point. You probably mean that they do not cross.
LVL 84

Expert Comment

ID: 21836784
If the lines on your map follow great circles, they may not follow straight lines along a cylindrical equidistant projection.
If this makes a difference, you may want to use a gnomonic projection to do your 2-d  point in polygoin test
LVL 22

Expert Comment

ID: 21847042
The easy way is to take the problem into 3D.

Take the lat/lons of the vertices of the polygon and find their (x,y,z).

Move the polygon vertices away from the center of the earth until they all can see each other over the surface of the earth.  

Create a 3d polytope from the aforesaid vertices and the center of the earth.

To test if a point is in the polygon, just take it to 3d and see if it is in the polytope.
LVL 23

Expert Comment

by:Christopher Kile
ID: 21847753
From what Perplexity has told us, "the point is inside a polygon that is mapped on the surface of the earth."  JayeshKitukale's page of algorithms tell us when a point lies within the polygon on a plane, and whether a point falls within a polyhedron.  


How accurate do you need this to be?  I ask because I used to get problems from a teacher, who developed models for predictive purposes in her day job.  She gave us a problem once where there was no symbolic solution, but which could be approximate by either circular or sinusoidal approximations (she chose the former, simpler calculation).  In your case, do you need to have your algorithm tell you that a point lies on a polygon on an approximated Earth's surface? The planet is somewhat pear-shaped, but even by simply assuming Earth is a sphere, there can be quite a distance between the surface of a flat polygon described by a plane cutting through each vertex plotted on the Earth's surface as opposed to the actual position on the surface - this sort of thing is sometimes called the approximation error.  As the size of the polygons decreases, the approximation error decreases.  What are the acceptable levels of approximation error for the problem with which you are faced?

LVL 84

Assisted Solution

ozo earned 300 total points
ID: 21848922
A gnomonic projection would map great circles onto straight lines, so if your polygon edges follow great circles, a 2D point in polygon algorithm on the mapped points may suffice for you.
It your points span more than a hemisphere, it may be hard to do a gnomonic projection,
(but it could also be problematic to decide which side of the polygon is meant to be the inside)
LVL 23

Assisted Solution

by:Christopher Kile
Christopher Kile earned 300 total points
ID: 21849036
Gnomonic projects don't of themselves answer the problem because there is no guarantee that more than one edge of a polygon will lie along a "great circle."  I also suggest that if you go down this track, you will have to compose a Dymaxion Map (http://en.wikipedia.org/wiki/Dymaxion_Map) which is not a trivial task.

I resubmit my question to Perplexity:  How accurate does this need to be?


Assisted Solution

wthengineering earned 300 total points
ID: 21904757
Here is an algorithm that I use to do exactly what you are describe.  This works for any clean 2D polygons including complex doughnut shaped boundaries.

General: Imagine a ray starting at the point in question and extending due north to infinity.  Count the number of boundary line segments that this ray intersects.  If the number is odd then the point is inside the boundary, otherwise it is outside the boundary.    

Foreach boundary segment
    If Pt.x is between segment.x1 and segment.x2
        Calculate the y value where ray intersects this segment
        If y is north of Pt.y
            Increment IntersectionCount
If IntersectionCount is Odd
    Point IS in boundary
    Point is NOT in boundary

Note that the above summary does not go into the more complex situation where one or more boundary points have the EXACT same x value as the x value of the point in question.
LVL 84

Expert Comment

ID: 21905089
(unless your polygon includes the north pole) due north is a good direction to pick, since a north-south great circle easily maps to latitude longitude coordinates,
however, care is still needed to do the
   Calculate the y value where ray intersects this segment
step properly

LVL 84

Expert Comment

ID: 21905101
> there is no guarantee that more than one edge of a polygon will lie along a "great circle."
So one edge is guaranteed but the other edges are not?
How are the edges defined if not by great circles?  are they loxodromes? some other curve?

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is designed to assist GIS (Geographic Information System) and GPS (Global Positioning System) developers using ESRI ArcGIS and other spatial information management systems.   For the uninitiated the concept of projectio…
This article seeks to propel the full implementation of geothermal power plants in Mexico as a renewable energy source.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question