Create a box around points and use in inpolygon

9 views (last 30 days)
Dear all,
I used this code below in order to select the points that are placed in/on the polygon which is correct and gave me the right answer.
% lat and lon are the coordinates of my 96 points that I want to select between them
% and polygon1_x and polygon1_y are coordinates of the shape file
[in,on] = inpolygon(lat,lon,polygon1_x,polygon1_y); % Logical Matrix
inon = in | on; % Combine ‘in’ And ‘on’
idx = find(inon(:)); % Linear Indices Of ‘inon’ Points
latcoord = lat(idx); % X-Coordinates Of ‘inon’ Points
loncoord = lon(idx); % Y-Coordinates Of ‘inon’ Points
clf
figure(1)
plot(lon, lat, '.') % Plot All Points
hold on
plot(polygon1_y, polygon1_x, '.') % Plot Polygon
plot(loncoord, latcoord, 'gp') % Overplot ‘inon’ Points
hold off
idx = idx.';
But I want to edit this code in order to consider each point as the center of 0.5 x 0.5 grid-box and then start checking. So if any part of this box placed in/on polygon I want to select (index) the former point (center).
I attached my data. I tried to edit this by myself but unfortunately it not accomplished well. So any suggestion is highly appreciated.
Thank you all.

Accepted Answer

KSSV
KSSV on 4 Aug 2020
Edited: KSSV on 4 Aug 2020
load("polygon1_x.mat") ;
load("polygon1_y.mat") ;
load("lon.mat") ;
load("lat.mat") ;
% remove Nan's from the data
xv = polygon1_y ; yv = polygon1_x ;
xv(isnan(xv)) = [] ;
yv(isnan(yv)) = [] ;
%
loncoord = zeros([],1) ;
latcoord = zeros([],1) ;
count = 0 ;
for i = 1:length(lon)
i
% make grid around (lon,lat)
x = lon(i)-0.5:0.5:lon(i)+0.5 ;
y = lat(i)-0.5:0.5:lat(i)+0.5 ;
[X,Y] = meshgrid(x,y) ;
[in,on] = inpolygon(X(:),Y(:),xv,yv); % Logical Matrix
inon = in | on; % Combine ‘in’ And ‘on’
idx = find(inon(:)); % Linear Indices Of ‘inon’ Points
if any(idx)
count = count+1 ;
loncoord(count) = lon(i); % Y-Coordinates Of ‘inon’ Point
latcoord(count) = lat(i) ;
end
end
plot(polygon1_y,polygon1_x,'b')
hold on
plot(loncoord,latcoord,'*r')
  5 Comments
KSSV
KSSV on 4 Aug 2020
Edited the code....check the answer...

Sign in to comment.

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!