I have 96 points (longitude and latitude); each point is a center of 0.5 x 0.5 pixel (box), on the other hand, I have a polygon, I want to select each pixel that placed in or on my polygon.
I can do it easily like this scrip below but the problem of this code is it just considers a point (center of the pixel) not an entire pixel to index.
polygon1_x = polygon1_x.'; % x of polygons
polygon1_y = polygon1_y.'; % y of polygons
lat = Points.lat; % x of points = lat
lon = Points.lon; % y of points = lon
[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
clffigure(1)plot(lon, lat, '.') % Plot All Points
hold onplot(polygon1_y, polygon1_x, '.') % Plot Polygon
plot(loncoord, latcoord, 'gp') OUTPUT = idx; % the output is idx
So I would be grateful if anyone can told me how I can index the row number of points from Point.mat which placed in/on my shapefile if the points are center of 0.5 x0.5 pixels. In this way, I can effectively select pixels that are in or on my polygon. I need the output like idx.
Thank you so much.
Best Answer