MATLAB Answers

How to identify points that are located in a polygon when this polygon passes through the meridian?

11 views (last 30 days)
Hemming
Hemming on 4 Jan 2019
Edited: Are Mjaavatten on 6 Jan 2019
I have a polygon presented by a series of longitude and latitude degrees. The longitude of this polygon is presented by degrees of [0 360] and it passes through the meridian. Also, I have two points and I need to check whether these points are located in this polygon.
For example, the longitude and latitude of this polygon is
PGlon = [5 5 355 355 5]; PGlat = [30 40 40 30 30];
and the points are
Xlon = [0 10]; Xlat = [35 45];
The positions of the polygon and points are as this figure (the first point is located inside this polygon).
I know that the function "inpolygon" may help to solve this problem, and I do
in_flag = inpolygon(Xlat,Xlon,PGlat,PGlon);
But this function cannot recognize whether the polygon crosses the meridian, so both points are identified to be outside the polygon.
Is there any useful way to handle this problem? Thanks!

  0 Comments

Sign in to comment.

Answers (1)

Are Mjaavatten
Are Mjaavatten on 5 Jan 2019
Edited: Are Mjaavatten on 5 Jan 2019
It seems that you are using the mapping toolbox, Then you can use ingeoquad:
ingeoquad([35,45],[0,10],[30 40], [355 5])
Your call to inpolygon checks if the points are inside a band spanning 350 degrees, which is not what you want. Use negative values for longitudes > 180:
inpolygon([35 45],[0 10],[30 40 40 30],[5 5 -5 -5])

  2 Comments

Hemming
Hemming on 6 Jan 2019
Thanks for your answer. This is an excellent choice when the polygon is a quadrangle. But what if the polygon is not a quadrangle (for example, the irregular boundary of a city)? Have you got any ideas? Thanks.
Are Mjaavatten
Are Mjaavatten on 6 Jan 2019
Just create a polygon from the border points. inpolygon will work for any polygon, even if it is not convex. Example:
xb = [0 1 1 0.5 0];
yb = [0 0 1 0.5 1];
figure;
patch(xb,yb,[1,1,1]*0.9);
inside = inpolygon(x,y,xb,yb);
hold on
plot(x(inside),y(inside),'*r')
plot(x(~inside),y(~inside),'ok')

Sign in to comment.

Sign in to answer this question.