Creating mask from coastline
6 次查看(过去 30 天)
显示 更早的评论
I'm trying to create a mask from coastline data, which is a N x 2 array of the points, however I'm having trouble actually creating the mask. I've tried using the inpolygon function as an alternative, but it's coming up empty. Here is the part of the code:
C=load('costatotal.dat'); % loading the coastal data
C=C(~isnan(C)); % taking out the NaNs as they don't work in deg2utm
C=reshape(C,numel(C)/2,2); % reshapes
[x_c,y_c]=deg2utm(C(:,2),C(:,1)); % C(:,2)=latitude , C(:,1)=longitude
[X,Y]=meshgrid(linspace(min(xo),max(xo),N),linspace(min(yo),max(yo),N)); % creates a mesh grid that contains the lats and longs of the query points
[in]=inpolygon(X,Y,x_c,y_c); % inpolygon is testing to see if the query points are within the coastline
4 个评论
Sara
2014-8-7
- Are xo,yo the coordinates or a rectangular area in lat lon coordinates or UTM?
- What do you mean with 40 by 40? cells, km, degrees?
- Is what you want to do extracting the points that lie within an area defined with xo,yo from the coordinates in C? In this case you can do:
k = find(x_c >= min(xo) & x_c <= max(xo) & y_c >= min(yo) & y_c <= max(yo));
x_c = x_c(k);
y_c = y_c(k);
Note, this line in your code may be in the wrong if the lat and the lon are not NaN's at the same time:
C=C(~isnan(C)); % taking out the NaNs as they don't work in deg2utm
采纳的回答
Iain
2014-8-7
Ok... the method you need to follow is:
1. Take the location you want to find is inside your polygon, and note either the lat.
2. Take the list of points, and establish if the lat falls within each pair of points.
a. If no pair of points encompasses that lat, then the location is outside of the coastline - do the next point.
3. For each pair of points that encompass the right lat, establish if the point is to the left or right of the line that links the points. Count the total number of lines to the left & to the right. If there is an ODD number to the left & to the right, then you are INSIDE the boundary.
3 个评论
更多回答(1 个)
Image Analyst
2014-8-20
Wow - so complicated. Too bad you don't have the Image Processing Toolbox where you could simply use poly2mask(). Very simple - one line of code.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!