Applying a mask creates an odd artifact in the resulting map

32 visualizaciones (últimos 30 días)
Emily T. Griffiths
Emily T. Griffiths el 24 de Oct. de 2025 a las 13:46
Comentada: Mathieu NOE hace alrededor de 4 horas
I am trying to limit a map to the data included within Denmark's EEZ. For example:
The EEZ shape file was downloaded from here: https://www.marineregions.org/gazetteer.php?p=details&id=5674
I want to include everything that is within the black lines. To do this, I am reading in the original mapping data, which is stored in a nc file, with 4D data (lat, lon, frequency, percentile). I extract it as so:
sample23 = 'path to nc file'
energy23 = ncread(sample23, '/AcousticData/energy');
lat = ncread(sample23, '/AcousticData/lat');
lon = ncread(sample23, '/AcousticData/lon');
prtc = ncread(sample23, '/AcousticData/percentile');
frq = ncread(sample23, '/AcousticData/frequency');
[num_lat, num_lon, num_percentile, num_freq, ~] = size(energy23);
% Create mask
eez=shaperead('./ShapeFiles/eez/eez.shp'); % From link above
[LonGrid, LatGrid] = meshgrid(lon, lat); % Note: lat is Y, lon is X
Y=eez.Y(~isnan(eez.Y))';
X=eez.X(~isnan(eez.X))';
inPoly = inpolygon(LonGrid, LatGrid,X,Y);
%apply EEZ mask
energy23sub=reshape(energy23, [],num_percentile*num_freq);
energy23sub(~inPoly, :)=nan;
energy23sub=reshape(energy23sub, size(energy23));
This has worked on other areas. However, when I apply this mask to these data, this is the resulting map that I get:
Where part of the data has been displaced/flipped to outside of the EEZ polygon. Can anyone help explain this? Or have any ideas on how to fix it?
I am sorry that I can not supply the data within the nc file.
  3 comentarios
Emily T. Griffiths
Emily T. Griffiths hace alrededor de 3 horas
Hey!
So a colleague better versed in Matlab than me fixed this using the Mapping Toolbox. There were overlapping lats/longs in the EEZ file, which the mapping tool box can understand and handle, but the inpolygon function can not. I don't know exactly what he did, apologies, but it helps to use the right tool for the job!

Iniciar sesión para comentar.

Respuestas (1)

Tridib
Tridib hace alrededor de 5 horas
In MATLAB, the way you set up your grid for images or matrices can be different from how geographic coordinates work. For example, "meshgrid(lon, lat)" treats latitude as the Y-axis (rows) and longitude as the X-axis (columns). But if the data or the plotting function uses a different order, the mask might not line up correctly.
You also need to make sure the coordinates in your shapefile (like the EEZ boundary) match the order and projection of your data grid. If either the order or the projection is different, the mask can be shifted or misplaced. For example, NetCDF files sometimes store latitude from north to south or south to north, and if the lat vector is flipped compared to the data grid, the mask will be applied in the wrong place. After making the mask (inPoly), check that its shape matches the first two dimensions of the data (energy23). If it does not, try transposing or permuting it.
Hope this helps!
  1 comentario
Emily T. Griffiths
Emily T. Griffiths hace alrededor de 3 horas
This makes sense! I think you are correct in your assumption that the mask did not line up correctly. As stated above, colleague was able to fix this issue using the mapping toolbox, rather than the functions I was using for the job!

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by