필터 지우기
필터 지우기

Plotting a grid of points with inpolygon check

조회 수: 4 (최근 30일)
Gareth Maver
Gareth Maver 2016년 3월 14일
댓글: Adam 2016년 3월 15일
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
Gareth Maver
Gareth Maver 2016년 3월 14일
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) ;

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

채택된 답변

Adam
Adam 2016년 3월 14일
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
Gareth Maver
Gareth Maver 2016년 3월 14일
편집: Gareth Maver 2016년 3월 14일
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 2016년 3월 15일
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.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Geographic Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by