'inpolygon' returns zeros logical variable (R2019b, Win10)

2 visualizzazioni (ultimi 30 giorni)
Hi,
I try to make a mask for area of ocean's basin using function inpolygon.
I have a big area with latitude from 66° to 73° (length is 420) and longitude from -5° to 18° (length is 1380):
lat = linspace(66,73,420)'; % latitude
lon = linspace(-5,18,1380)'; % longitude
[LAT,LON] = ndgrid(lat,lon); % 2-D matrix
Next, I use contourf to plot bathymetry ('z' has 420x1380 size) and choose mask border with mouse (polygon vertices):
contourf(lon,lat,z) % bathymetry
[xi,yi] = getpts(gca); % choose mask border
xi = [xi; xi(1)]; yi = [yi; yi(1)];
hold on
plot(xi, yi,'k','LineWidth',2) % polygon (mask)
So i get something like this:
Finally, I use inpolygon to check what points (grid nodes - LAT and LON) belong to this polygon. But there is no 'true' points inside (variable 'check' is zero).
mask = inpolygon (LAT, LON, xi, yi);
check = sum(sum(mask))
Anyone have any ideas how to fix this?
  1 Commento
Elena Novoselova
Elena Novoselova il 2 Lug 2020
I still don't know what problem is but I found another solution without 'inpolygon':
lat = linspace(66,73,420)'; % latitude
lon = linspace(-5,18,1380)'; % longitude
[LAT,LON] = ndgrid(lat,lon); % 2-D matrix
LAT = LAT(:); LON = LON(:); % 2-D to 1-D
contourf(lon,lat,z) % bathymetry ('z' is depth of the ocean)
[xi,yi] = getpts(gca); % choose mask border with mouse
pgon = polyshape(xi,yi); % 2-D polygons
TFin = isinterior(pgon,LON,LAT); % query points inside polyshape
mask = reshape(TFin,[420,1380]); % from 1-D to 2-D
hold on
plot(xi, yi,'k','LineWidth',2) % polygon (line)
plot(LON(mask),LAT(mask),'r+') % points inside polygon
Maybe it isn't the best solution but it's work.

Accedi per commentare.

Risposta accettata

Voss
Voss il 2 Lug 2020
Note that longitude is horizontal and latitude is vertical, so you'd need to swap LAT and LON in your call to inpolygon. (Like you have in your call to isinterior in the solution you found.)
  3 Commenti
Voss
Voss il 2 Lug 2020
You have LAT first in your call to inpolygon:
mask = inpolygon (LAT, LON, xi, yi);
check = sum(sum(mask))

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Elementary Polygons in Help Center e File Exchange

Prodotti


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by