# Check if Lat, Lon coordinate fall inside a polyshape Polygon

30 views (last 30 days)

Show older comments

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 Comment

wenqing zhong
on 14 Mar 2022

### Accepted Answer

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));

##### 7 Comments

Cris LaPierre
on 14 Mar 2022

### More Answers (1)

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]);

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.

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!