Check if geo-point is inside or outside of polygon
Here is a possible solution to my problem.
- Geographical coordinates must be stored properly. Example
np.array([[Lon_A, Lat_A], [Lon_B, Lat_B], [Lon_C, Lat_C]])
- Create the polygon
- Create the point to be tested
- Use
polygon.contains(point)
to test if point is inside (True
) or outside (False
) the polygon.
Here is the missing part of the code:
from shapely.geometry import Point
from shapely.geometry.polygon import Polygon
lons_lats_vect = np.column_stack((lons_vect, lats_vect)) # Reshape coordinates
polygon = Polygon(lons_lats_vect) # create polygon
point = Point(y,x) # create point
print(polygon.contains(point)) # check if polygon contains point
print(point.within(polygon)) # check if a point is in the polygon
Note: the polygon does not take into account great circles, therefore it is necessary to split the edges into many segments thus increasing the number of vertices.
Special case: If point lies on borders of Polygon
E.g. print(Polygon([(0, 0), (1, 0), (1, 1)]).contains(Point(0, 0)))
will fail
So one can use
print(polygon.touches(point)) # check if point lies on border of polygon
Checking if a longitude/latitude coordinate resides inside a complex polygon in an embedded device?
Here's an implementation I wrote in C# for a Polygon class that contains a list of vertices. It doesn't consider the curvature of the Earth. Rather, you would pre-process the polygon into smaller segments prior to running this.
The performance of this algorithm is very good. Even for polygons with thousands of edges it completes in around one or two milliseconds on my desktop.
The code has been optimised quite a bit and so isn't that readable as psuedo-code.
public bool Contains(GeoLocation location)
{
if (!Bounds.Contains(location))
return false;
var lastPoint = _vertices[_vertices.Length - 1];
var isInside = false;
var x = location.Longitude;
foreach (var point in _vertices)
{
var x1 = lastPoint.Longitude;
var x2 = point.Longitude;
var dx = x2 - x1;
if (Math.Abs(dx) > 180.0)
{
// we have, most likely, just jumped the dateline (could do further validation to this effect if needed). normalise the numbers.
if (x > 0)
{
while (x1 < 0)
x1 += 360;
while (x2 < 0)
x2 += 360;
}
else
{
while (x1 > 0)
x1 -= 360;
while (x2 > 0)
x2 -= 360;
}
dx = x2 - x1;
}
if ((x1 <= x && x2 > x) || (x1 >= x && x2 < x))
{
var grad = (point.Latitude - lastPoint.Latitude) / dx;
var intersectAtLat = lastPoint.Latitude + ((x - x1) * grad);
if (intersectAtLat > location.Latitude)
isInside = !isInside;
}
lastPoint = point;
}
return isInside;
}
The basic idea is to find all edges of the polygon that span the 'x' position of the point you're testing against. Then you find how many of them intersect the vertical line that extends above your point. If an even number cross above the point, then you're outside the polygon. If an odd number cross above, then you're inside.
How to detect if the current location is inside an area with CoreLocation
Use CLLocations's distanceFromLocation
http://developer.apple.com/library/ios/#DOCUMENTATION/CoreLocation/Reference/CLLocation_Class/CLLocation/CLLocation.html
How to detect that a point is inside a Polygon using Google Maps?
Other solution: Google-Maps-Point-in-Polygon
A Javascript Google Maps v3 extension for the Polygon class to detect whether or not a point resides within it...
Geolocation - Check if a location belongs to an area
One way could be a ray casting algorithm. The theory is to draw a virtual horizontally ray from outside the polygon to your point of interest and count how often it crosses any side of the polygon. If the number is even, your point is outside if it's odd it's inside the polygon.
Look here http://alienryderflex.com/polygon/ for more information and C code sample.
Related Topics
Xml Deserialization of Collection Property with Code Defaults
Jquery Ajax Calls to Web Service Seem to Be Synchronous
Error Making Azure Management Library API Call When Authenticating with Azure Active Directory
How to Group by on 2 Child Entities and Get Total of Both This Child Entities
How to Invoke Scripts Work in Mshtml
How to Create a Friendly Url in ASP.NET MVC
Draw on the Screen Without a Form
How to Write Logs from Within Startup.Cs
Passing Just a Type as a Parameter in C#
Dbarithmeticexpression Arguments Must Have a Numeric Common Type
What's a Good Way to Overwrite Datetime.Now During Testing
Why Are Const Parameters Not Allowed in C#
Viewmodel Validation for a List
Do the New C# 5.0 'Async' and 'Await' Keywords Use Multiple Cores
How to Find the Actual Printable Area? (Printdocument)
Good Introduction to the .Net Reactive Framework