Reputation: 1648
We are working on one map solution. Here user has drawn one polygon on the map and for a given point we need to find if point is residing in or out of the polygon.
We are using one SQL technique as per http://www.sql-statements.com/point-in-polygon.html to do this task till now but during the time we found out that its not worked for some areas.
Does anyone has the proper solution on this? We can also try the solution in C# if its working fine
Thank you.
Upvotes: 1
Views: 2648
Reputation: 23173
If you are using SQL Server 2008 then you can use STIntersection (geography Data Type) - SQL Server will do everything for you.
I'd recommend SQL Server 2008 in this case because it provides native support for geographical data. Before you use it "STIntersect giving incorrect result for geography Datatype" might be worth of reading. Example from that site:
declare @point geometry
declare @polygon geometry
set @point = geometry::STGeomFromText('POINT (-88.22 41.50000001)', 4326)
set @polygon = geometry::STGeomFromText('POLYGON ((-88.2 41.5, -88.2 41.6, -88.3 41.6, -88.3 41.5, -88.2 41.5))', 4326)--124
Select @point.STIntersection(@polygon).ToString()
Upvotes: 4
Reputation: 2572
IMHO the basic explanation of the PIP solutions are missing half of the important stuff, i.e. how to determine what lines of the polygon to actually test for having been crossed. Just in case of Michał Powaga's solution not working for you.
Point P(x, y) is your point. Points P0(x0, y0) and P1(x1, y1) form a line. The imaginary line we draw to see how many poly-lines we cross is horizontal.
1) First determine what lines are actually crossable (lines that are parallel to the imaginary line you draw or lines that are above or below the line would obviously not be crossed):
For each line of the polygon, compute weather P would be able to cross it.
If ((x0 < x < x1) OR (x0 > x > x1)) add line to some list.
2) Determine which of the remaining lines (those in the list) are actually crossed:
For each line in list, compute
result = (y - y0) * (x1- x0) - (x - x0) * (y1 - y0)
If (result < 0) the line was crossed, increment a counter.
If (result == 0) the point is ON the line, increment a counter if thats supposed
to count as the point having crossed the line, else don't ...
If (result > 0) the line was not crossed, so just continue with the loop.
[Note: double check weather I got the sides right ...]
3) Now, if the counter is an odd number your point is inside, if it is even or zero it is outside of the polygon.
Upvotes: 1
Reputation: 5459
Paraphrased from http://en.wikipedia.org/wiki/Point_in_polygon
The easiest way to do this is draw an imaginary line from your point in one direction and count the number of lines it crosses. If it's odd, the point is inside, even the point is outside.
Basically iterate through each point pair, find where it crosses the horizontal line at your point, if it crosses to the right, increment the counter, if it crosses to the left or doesn't cross at all, ignore it. Horizonal lines right at your point should not be counted either (boundary condition).
Upvotes: 1