How to create a mask on a map in lat-lon coordinates?

12 views (last 30 days)
A LL on 13 Jan 2022
Answered: Binaya on 20 Oct 2023
I want to produce a mask on the region shown in figure (map_mask.fig).
I have a lon x lat grid of size 1440x121.
It represents the northen hemisphere with latitude [60,90]N and longitude [-180,180]W at 1/4 degree resolution.
I tried to use poly2mask with the freehand object I wanted as a mask. I got an empty matrix with only 0s.
%Load var: latitude, longitude, latMat, lonMat
%unsuccessful attempt
roi = drawfreehand;
%Plot domain
figure(1);
hold on;
axesm('MapProjection','eqdazim','MapLatLimit',[60 90]);
axis off;
framem on;
gridm on;
mlabel on;
plabel on;
setm(gca,'MLabelParallel',0);
geoshow('landareas.shp','FaceColor',"none",'LineWidth',1);
axis off;
set(gca,'Fontsize',15);
gcf;
Thank you

Binaya on 20 Oct 2023
Hi
Based on my understanding, you are seeking assistance in generating a mask using the "drawfreehand" function over a "geoshow" plot that spans from latitude 60° to 90° and longitude -180o to 180o.
Upon reviewing the provided code, it appears that the "drawfreehand" function is being called before plotting the map, resulting in an empty mask. To address this issue, please follow the steps outlined below:
1. Remove line [6] from the code, which declares the mask prematurely.
2. Move line [5], which calls the "drawfreehand" function, to the end of the code. Please note that the "drawfreehand" function returns position coordinates in Cartesian format, which need to be converted to latitude-longitude coordinates.
3. Utilize the code provided below for the conversion, as there is no direct function available for converting the output of "drawfreehand"(cartesian output) on a circular latitude-longitude "geoshow" plot to latitude-longitude coordinates.
x = roi.Position(:,1); %Gather cartesian coordinates from drawfreehand
y = roi.Position(:,2);
[theta,r] = cart2pol(x,y) %Convert to polar coordinates
theta = theta *180/pi; %Convert theta from radians to degrees
lat1 = -60*r + 90; %Convert "r" into latitude coordinates
lon1 = theta;
idx1 = abs(lon1)<=90; %Converting theta into longitudinal cooridnates
idx2 = lon1<-90;
idx3 = lon1>90;
lon1(idx1) = lon1(idx1)+90;
lon1(idx2) = lon1(idx2) + 90;
lon1(idx3) = lon1(idx3)-270;
[lat1,lon1]
4. After getting the “drawfreehand” coordinates in lat-lon format from using the above code snippet, you can use the poly2mask function by referring to its documentation.
Please refer to the below Mathworks documentation for more reference:
I hope this helps.
Regards
Binaya

Categories

Find more on Geographic Plots in Help Center and File Exchange

R2021a

Community Treasure Hunt

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

Start Hunting!