How to determine whether a specified latitude and longitude point falls in a polygon

Source: Internet
Author: User
Tags abs

1, theoretical support: if from the point of need to judge a ray and the number of focus of the polygon is odd, then the point in this polygon, otherwise the point outside this polygon. (Rays cannot intersect with polygon vertices)

2, Programming Ideas:

The idea of the program is to make a horizontal ray (parallel to the x-axis, to the x-axis) from point A to the left to determine whether there is a focus on each side.

DLon1, DLon2, DLAT1, dLat2 represent the longitude and latitude (x and Y axes) of the edges at the start and end points respectively.

To determine whether a point is at the end of the edge of the D1 and D2 horizontal parallel lines, not there is no intersection point, continue to judge the next side.

It is possible to have a intersection with the Ray to the left of point A, and then the geometric method is used to get the X coordinate of the horizontal line of point A and the intersection of the edge.

Then the x-coordinate of the intersection is judged on the left or right side of point A, the total number of points plus one on the left, and the right side not on the left ray of point A, continuing to judge the next edge.

3, the original code is as follows (Dephi):
Type
Tmypoint = Packed record
x:double;
y:double;
End

{*------------------------------------------------------------------------------
Determines whether the specified latitude and longitude coordinate point falls within the specified polygon region
@param the longitude of the ALon point
@param the latitude of the Alat point
@param apoints Specifies the coordinates of each node of the polygon region
@return True falls within range False is not in range
------------------------------------------------------------------------------*}
function Isptinpoly (ALon, alat:double; Apoints:array of Tmypoint): Boolean;
Var
Isum, icount, Iindex:integer;
DLon1, DLon2, DLAT1, DLAT2, dlon:double;
Begin
Result: = False;
if (Length (apoints) < 3) Then
Begin
Result: = False;
Exit;
End
Isum: = 0;
Icount: = Length (apoints);
For Iindex: =0 to ICount-1 do
Begin
if (Iindex = iCount-1) Then
Begin
DLon1: = Apoints[iindex]. X
DLAT1: = Apoints[iindex]. Y
DLon2: = Apoints[0]. X
DLAT2: = Apoints[0]. Y
End
Else
Begin
DLon1: = Apoints[iindex]. X
DLAT1: = Apoints[iindex]. Y
DLon2: = Apoints[iindex + 1]. X
DLAT2: = Apoints[iindex + 1]. Y
End
The following statement determines whether a point is between horizontal parallel lines at both ends of the edge, where there may be intersections, and begins to determine if the intersection is on the left Ray
if ((Alat >= dLat1) and (Alat < DLAT2)) or ((ALAT&GT;=DLAT2) and (Alat < DLAT1)) then
Begin
if (ABS (DLAT1-DLAT2) > 0) Then
Begin

To get the X coordinate of the intersection of the left Ray and the edge of point A:
Dlon: = DLon1-((dlon1-dlon2) * (Dlat1-alat))/(DLAT1-DLAT2);

If the intersection points to the left of point a (which is to do with the intersection of the Ray and the edge), then the total number of points of the ray and the Edge plus one:
if (Dlon < ALon) Then
INC (isum);
End
End

End
if (isum mod 2 <> 0) Then
Result: = True;
End


(C #)

public bool Isptinpoly (double ALon, double Alat, list<point> apoints)
{
int isum = 0, icount;
Double DLon1, DLon2, DLAT1, DLAT2, Dlon;
if (Apoints.count < 3)
return false;
icount = Apoints.count;
for (int i = 0; i < iCount-1; i++)
{
if (i = = iCount-1)
{
DLon1 = Apoints[i]. X
DLAT1 = Apoints[i]. Y
DLon2 = Apoints[0]. X
DLAT2 = Apoints[0]. Y
}
Else
{
DLon1 = Apoints[i]. X
DLAT1 = Apoints[i]. Y
DLon2 = apoints[i + 1]. X
DLAT2 = apoints[i + 1]. Y
}
The following statement determines whether a point is between horizontal parallel lines at both ends of the edge, where there may be intersections, and begins to determine if the intersection is on the left Ray
if ((Alat >= dLat1) && (Alat < DLAT2)) | | ((Alat >= dLat2) && (Alat < DLAT1))
{
if (Math.Abs (DLAT1-DLAT2) > 0)
{
To get the X coordinate of the intersection of the left Ray and the edge of point A:
Dlon = DLon1-((dlon1-dlon2) * (Dlat1-alat))/(DLAT1-DLAT2);

If the intersection points to the left of point a (which is to do with the intersection of the Ray and the edge), then the total number of points of the ray and the Edge plus one:
if (Dlon < ALon)
isum++;
}
}
}
if (isum% 2!= 0)
return true;
return false;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.