Check if Lat, Lon coordinate fall inside a polyshape Polygon

30 views (last 30 days)
I am attempting to create a for loop that goes through a workbook and plot a polygon for each spreadsheet in the workbook. Each polygon has a limit value. My goal is to create another plot with only points that have a value less than the polygon limit and also that fall inside that same polygon. That process is done for each polygon in each spreadsheet.
The issue I am having is when I check if the point is within the polygon, the logical return is zero for every point inside the polygons. I am certain that's not true. The polygons are made of Lat and lon coordinates and so are the points.I tried using "isinterior" and 'inpolygon" I get the same results. I would like to know what am I missing. I could not anything helpful on the forums to help me get over this hump.
This is the part of the code that checks for that:
for i = 1 : nSheet %nSheet is the number of sheet. Which defines number of polygons
Polyin = polyshape(boundlon{i}(:,1),boundlat{i}(:,1));
for j = 1:size(Pfd_value,1) %Pfd_value is a 356X1 double
%TFin = inpolygon(Pfd_lon(j),Pfd_lat(j),finalplot.Longitude,finalplot.Latitude);
TFin = isinterior(Polyin,Pfd_lon(j),Pfd_lat(j));
if Pfd_value(j) <= Limit_range(i) && TFin == 1 %Limit_range is the constantlimit for each polygon
  1 Comment
wenqing zhong
wenqing zhong on 14 Mar 2022
You should convert latitude and longitude to XY, use polar coordinates to convert to XY coordinate system. And then use 'inpolygon' or isinterior. Because your polyshape Polygon maybe folded or discontinuity due to being in polar regions.

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 22 Feb 2021
I think your inputs to inpolygon need to be the points. Try using the inputs to polyshape. inpolygon can test a vector of query points. Here's a sample (untested).
TFin = inpolygon(Pfd_lon,Pfd_lat,boundlon{i}(:,1),boundlat{i}(:,1));
Cris LaPierre
Cris LaPierre on 14 Mar 2022
this sounds like a workflow for groupsummary. Just a note that I don't think you can use Polyin. You would have to use the separate boundlon{i}(:,1) and boundlat{i}(:,1). Group by lat then by lon and set the 'method' to sum.

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 22 Feb 2021
I would vectorize the calculations. Let's make a simple polyshape:
P = polyshape([0.75 0.3 -0.4 -0.1 -0.6 -0.3 0.5], [0.1 0.6 0.8 -0.25 0.1 -0.8 0.2]);
axis([-1 1 -1 1])
hold on
Now let's generate some sample points.
xy = 2*rand(10, 2)-1; % Random numbers between -1 and +1
Are they inside the polyshape P? Test all the points in one call rather than testing each individual point.
isin = isinterior(P, xy(:, 1), xy(:, 2));
Now plot them.
plot(xy(isin, 1), xy(isin, 2), 'go', 'MarkerSize', 12, 'MarkerFaceColor', 'g')
plot(xy(~isin, 1), xy(~isin, 2), 'rx', 'MarkerSize', 12)
This has the added benefit of only creating three graphics objects in the axes: the polygon created by plotting the polyshape, the line with the red X markers, and the line with the green circle markers. Your code created lots of graphics objects for individual points. You could likely adapt this to use scatter instead of plot.
Mini Me
Mini Me on 14 Mar 2022
@Steven Lord, thanks for your reply. I decided to start a new trend instead with more concrete details. See the link below:

Sign in to comment.


Find more on Elementary Polygons in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!