Create a box around points and use in inpolygon

조회 수: 10 (최근 30일)
BN
BN 2020년 8월 4일
댓글: BN 2020년 8월 4일
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.

채택된 답변

KSSV
KSSV 2020년 8월 4일
편집: KSSV 2020년 8월 4일
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
KSSV
KSSV 2020년 8월 4일
Edited the code....check the answer...
BN
BN 2020년 8월 4일
Thank you so much

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Map Display에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by