Creating mask from coastline

25 views (last 30 days)
Roseanne
Roseanne on 6 Aug 2014
Answered: Image Analyst on 20 Aug 2014
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 Comments
Sara
Sara on 7 Aug 2014
  • 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
Roseanne
Roseanne on 8 Aug 2014
  • My co-ordinates are the lat lon co-ordinates, I change them to UTM as a function I use after this stage requires them to be in this format.
  • When I said my meshgrid was 40x40, I mean 40 cells by 40 cells.
Thank you for the suggestion, but will this not only find when it is the exact value? All my points occur in the sea, and I want to be able to mask out the land for the purposes of an extrapolation function I use further down as otherwise it includes this land space in the function, which is wrong, as it only applies to the sea.

Sign in to comment.

Accepted Answer

Iain
Iain on 7 Aug 2014
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 Comments
Iain
Iain on 8 Aug 2014
Test it out on something simple, like a u shape or a square.
Roseanne
Roseanne on 20 Aug 2014
So what I ended up doing was using the convhull function, which allowed me to create a polygon from my coastal data, and then used inpolygon with this shape. Thanks for all the help!

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 20 Aug 2014
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.

Community Treasure Hunt

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

Start Hunting!