Creating mask from coastline

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 comentarios

Sara
Sara el 6 de Ag. de 2014
maybe attach the inputs...
Roseanne
Roseanne el 7 de Ag. de 2014
My inputs are too long to put on here.. coastline (or C in my code) is 447015x2 after the ~isnan function, and the query points are 246x1 for xo and yo, which are used to create a meshgrid of the limits to a 40x40. All I want to achieve is to create a mask if these 40x40 points occur within the lats and longs stated in the 447015x2, any ideas?
Sara
Sara el 7 de Ag. de 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 el 8 de Ag. de 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.

Iniciar sesión para comentar.

 Respuesta aceptada

Iain
Iain el 7 de Ag. de 2014

0 votos

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 comentarios

Roseanne
Roseanne el 8 de Ag. de 2014
Thank you for the suggestion, I will give it a try!
Iain
Iain el 8 de Ag. de 2014
Test it out on something simple, like a u shape or a square.
Roseanne
Roseanne el 20 de Ag. de 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!

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 20 de Ag. de 2014

0 votos

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.

Preguntada:

el 6 de Ag. de 2014

Respondida:

el 20 de Ag. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by