MATLAB Answers

Shift Map in App Designer

10 views (last 30 days)
meghannmarie on 24 Jul 2020
Edited: Adam Danz on 28 Jul 2020
In app designer I am trying to shift my world map to be centered over the pacific. The whole point of the map is to use the function drawrectangle so the user can select coordinates with a bounding box. I want to be able to convert map to be centered over the pacific in case the user wants to specify a box crossing the dateline. It seems like app designer limited what I can do with figures. I tried to make a map axis so I could just reproject it, but I cannot figure out how to get the map axis in appdesigner nor do I know if it would work with drawrectangle. Here is what i presently have to draw the map centered over the atlantic (-180 to 180). Any ideas how to change this to center the map and shapefile over the pacific (0 to 360)?
The commented out lines is what i tried to get the map axis into the UIAxis, it didnt work...
% Code that executes after component creation
function startupFcn(app)
d = uiprogressdlg(app.UIFigure,'Title','Setting up parallel pool.',...
blue = [180/255 198/255 231/255];
green = [83/255 129/255 53/255];
% figure(app.UIFigure)
% h = axesm('mercator', 'maplonlimit', [0 360]);
% hCopy = copyobj(h.Children, app.UIAxes); % Copy all of the axis' children to your app axis
% delete(h.Parent)
geoshow(app.UIAxes,'landareas.shp', 'FaceColor', green);
app.UIAxes.Color = blue;
xlim(app.UIAxes,[-180 180])
ylim(app.UIAxes,[-90 90])
app.UIAxes.XGrid = true;
app.UIAxes.YGrid = true;
app.pool = gcp;
app.ParallelPoolSizeEditField.Value = app.pool.NumWorkers;


Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 28 Jul 2020
Edited: Adam Danz on 28 Jul 2020
Since the worldmap function does not currently support setting parent axes, you'll have to create the plot in an external figure, make all needed changes, and then copy it to your app's axes. Here are instructions.
To center the plot on the pacific ocean, set the lat and lon limits using worldmap(latlim,lonlim).
map = shaperead('landareas.shp', 'UseGeoCoords',true);
worldmap([-45 80],[0 360]) % approximately centered over the pacific
Then copy to your app axes. See this comment below for details.
Then draw rectangle.
h = drawrectangle(app.UIAxes); % supply your axis handle


Show 1 older comment
Adam Danz
Adam Danz on 28 Jul 2020
First, to make the UIAxes appear like the original axes,
app.UIFigure.HandleVisibility = 'on';
app.UIFigure.HandleVisibility = 'on';
Secondly, map axes aren't actual latitudinal and longitudinal coordinates. Those values are just written on the axes. For example, after plotting the map, run xlim() and ylim(); you'll see that the outputs are not lat,lon coordinates. Use minvtran & mfwdtran to convert between map and [lat,lon] coordinates.
The demo is done in a regular figure but you can apply it to the app designer axes.
% Create map
map = shaperead('landareas.shp', 'UseGeoCoords',true);
ax = worldmap([-45 80],[0 360]); % approximately centered over the pacific
% Draw rectangle over the pacific
h = drawrectangle(ax);
% Compute the 4 corners of the rectangle.
cornerCoordinates = h.Position(1:2) + ...
[0 0; 0 h.Position(4); h.Position(3:4); h.Position(3), 0];
% plot the 4 corners
hold(ax, 'on')
plot(cornerCoordinates(:,1), cornerCoordinates(:,2), ...
'rx', 'MarkerSize', 14, 'LineWidth', 4)
% Convert the map coordinate to lat,lon
[lat,lon] = minvtran(cornerCoordinates(:,1), cornerCoordinates(:,2));
% Plot a blue x at lat=25 lon=180; but first convert back to map coordinates
[x,y] = mfwdtran(25,180);
plot(x,y,'bx','MarkerSize', 14, 'LineWidth', 4)
meghannmarie on 28 Jul 2020
Thanks, that is exactly what I was looking for!! I am pretty sure this will work for me.
Adam Danz
Adam Danz on 28 Jul 2020
It was fun to figure it out along with ya.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!