Check if Lat, Lon coordinate fall inside a polyshape Polygon

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
colormap(hsv(size(Pfd_value,1)));
geoscatter(Pfd_lat(j),Pfd_lon(j),[],Pfd_value(j),'o','filled');
end
end
end

1 Kommentar

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.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Cris LaPierre
Cris LaPierre am 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));

7 Kommentare

Thanks Cris, I get the same results. It returns 0 for all points
Can you show us a small sample of data for Pfd_lon, Pfd_lat, boundlon, and boundlat where you expect some of the points to be inside the polygonal region but they are not detected as being in that region?
When you use Pfd_lon and Pfd_lat to create a polyshape object, do you receive any warning messages (in orange) when MATLAB creates the polyshape?
Mini Me
Mini Me am 22 Feb. 2021
Bearbeitet: Mini Me am 22 Feb. 2021
Steve, I use the boundary lat and boundary lon to create the polyshape. I do receive warning using Polyshape. Here it is:
Warning: Polyshape has duplicate vertices, intersections, or other inconsistencies that may produce inaccurate or unexpected results. Input data has been modified to
create a well-defined polyshape.
In polyshape/checkAndSimplify (line 480)
In polyshape (line 175)
Cris LaPierre
Cris LaPierre am 22 Feb. 2021
Bearbeitet: Cris LaPierre am 22 Feb. 2021
Save your variables boundlat, boundlon, Pfd_lat and Pfd_lon to a mat file and attach it to your post using the paperclip icon.
Here are the *.mat files
It looks like they didn't get attached.
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

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]);
plot(P)
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.

3 Kommentare

@cris LaPierre and @Steven Lord, on the same topic, what if I wanted to do a union between the coordinates of Polyin (or bound_lat and bound_lon) and Pfd_lat and Pfd_lon with each coordinate in a matrix. And if the lat and lon are the same fuse them but add the Pfd_values together.
Can you give a (small) concrete example to help us understand the problem? It's not 100% clear to me what you want the union of a polyshape and a collection of points to be: a larger polyshape that encompasses both all the points in the original polyshape plus the other scattered points? In that case I would assume you'd want the shape to be kind of "smooth" rather than just having spikes poking out to encompass those scattered points? Or did you have something else in mind?
@Steven Lord, thanks for your reply. I decided to start a new trend instead with more concrete details. See the link below: https://www.mathworks.com/matlabcentral/answers/1671324-do-a-union-of-polygons-and-add-the-associated-values-for-overlapping-coordinates

Melden Sie sich an, um zu kommentieren.

Kategorien

Produkte

Version

R2019b

Gefragt:

am 22 Feb. 2021

Kommentiert:

am 14 Mär. 2022

Community Treasure Hunt

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

Start Hunting!

Translated by