Clear Filters
Clear Filters

Plotting a grid of points with inpolygon check

4 views (last 30 days)
I have the latitude and longitude coordinates for the points of a polygon (is_15_ok_ks.txt).
I want to create a grid with ranges x = -99.5 : 0.1 : -95.6, y = 33.6 : 0.1 : 37.6
I then need to plot a series of nodes/points. Each node is at the centre of each 0.1x0.1 cell (such that the nodes are the ranges x = -99.45 : 0.1 : -95.65, y = 33.65 : 0.1 : 37.55)
I then will plot the polygon.
Then using inpolygon, I want to be able to determine which of those nodes are inside the polygon (and potentially retrieve each of their coordinates).
Any help on how I would do this?
Thanks Gareth
  1 Comment
Gareth Maver
Gareth Maver on 14 Mar 2016
I have got this so far. But 0s and 1s created by inpolygon are not correct. Any thoughts?
filename='is_15_ok_ks.txt' ;
data = load(filename) ;
lon = data(:,1) ;
lat = data(:,2) ;
%plot polygon
plot(lon,lat,'k') ;
hold on
%plot grid lines
g_y=[33.6:0.1:37.6];
g_x=[-99.5:0.1:-95.6];
for i=1:length(g_x)
plot([g_x(i) g_x(i)],[g_y(1) g_y(end)],'r:') %y grid lines
hold on
end
for i=1:length(g_y)
plot([g_x(1) g_x(end)],[g_y(i) g_y(i)],'r:') %x grid lines
hold on
end
%plot nodes
[X,Y] = meshgrid(-99.45:0.1:-95.65,33.65:0.1:37.55);
scatter(X(:), Y(:), 1);
%find nodes that lie within polygon
IN = inpolygon(X,Y,lon,lat) ;

Sign in to comment.

Accepted Answer

Adam
Adam on 14 Mar 2016
Replacing
[X,Y] = meshgrid(-99.45:0.1:-95.65,33.65:0.1:37.55);
with
[X,Y] = meshgrid(-99.45:0.1:-95.65,37.55:-0.1:33.65);
looks to give the correct answer as the y coordinates were just flipped. I don't have time to figure out exactly why they are, but you may spot why yourself now.
  4 Comments
Gareth Maver
Gareth Maver on 14 Mar 2016
Edited: Gareth Maver on 14 Mar 2016
Thanks again. One last question. And how do I save those pairs?
I have this - but its not keeping the pairs correct.
fid = fopen('polygon_nodes.txt','wt');
fprintf(fid,'%f%f\n',[X(IN);Y(IN)]);
fclose(fid);
Adam
Adam on 15 Mar 2016
fprintf(fid,'%f %f\n',X(IN), Y(IN));
should do the job. You need a space between the numbers and you have to pass the X and Y arguments in separately to fill the two %f place-holders.

Sign in to comment.

More Answers (0)

Categories

Find more on Geographic Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!