-
Notifications
You must be signed in to change notification settings - Fork 9
/
mapzoom.m
398 lines (333 loc) · 11.2 KB
/
mapzoom.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
function [] = mapzoom(varargin)
%MAPZOOM zooms a map to a specified location and scale.
%
% This function is part of Chad Greene's Antarctic Mapping Tools and
% requires Matlab's Mapping Toolbox. The Antarctic Mapping Tools package
% can be found at http://www.mathworks.com/matlabcentral/fileexchange/47638.
%
%% Citing Antarctic Mapping Tools
% This function was developed for Antarctic Mapping Tools for Matlab (AMT). If AMT is useful for you,
% please cite our paper:
%
% Greene, C. A., Gwyther, D. E., & Blankenship, D. D. Antarctic Mapping Tools for Matlab.
% Computers & Geosciences. 104 (2017) pp.151-157.
% http://dx.doi.org/10.1016/j.cageo.2016.08.003
%
% @article{amt,
% title={{Antarctic Mapping Tools for \textsc{Matlab}}},
% author={Greene, Chad A and Gwyther, David E and Blankenship, Donald D},
% journal={Computers \& Geosciences},
% year={2017},
% volume={104},
% pages={151--157},
% publisher={Elsevier},
% doi={10.1016/j.cageo.2016.08.003},
% url={http://www.sciencedirect.com/science/article/pii/S0098300416302163}
% }
%
%% Syntax
%
% mapzoom(lat,lon)
% mapzoom(x,y)
% mapzoom('SCAR location')
% mapzoom(...,'mapwidth',widthkm)
% mapzoom(...,widthkm)
% mapzoom(...,'inset')
% mapzoom(...,'inset','location')
% mapzoom(...,'insetsize',sizefraction)
% mapzoom(...,'frame','off')
% mapzoom('default')
%
%% Description
%
% mapzoom(lat,lon) centers a 500 km by 500 km map about the georeferenced
% location given by lat, lon.
%
% mapzoom(x,y) centers a 500 km by 500 km map about the polar stereographic
% eastings and northings x and y. Polar stereographic coordinates are
% automatically determined by the islatlon function.
%
% mapzoom('SCAR location') uses scarloc to find the lat,lon corresponding
% to the string 'SCAR location'.
%
% mapzoom(...,'mapwidth',widthkm) specifies width of the map in kilometers.
% Default map width is 500 km.
%
% mapzoom(...,widthkm) is shorthand for 'mapwidth',widthkm.
%
% mapzoom(...,'inset') places an inset map in the lower right hand corner
% of the map to show geographic context.
%
% mapzoom(...,'inset','location') specifies location of the inset. Location
% can be
% 'southeast' or 'se' lower right corner (default)
% 'northwest' or 'nw' upper left corner
% 'northeast' or 'ne' upper right corner
% 'southwest' or 'sw' lower left corner
%
% mapzoom(...,'insetsize',sizefraction) specifies size of the inset as a
% fraction of the width of the current map. Default sizefraction is 0.25.
%
% mapzoom(...,'frame','off') removes frame from the inset.
%
% mapzoom('default') returns a map to default extents.
%
%% EXAMPLES:
%
% antmap
% load coast
% patchm(lat,long,[.588 .976 .482])
% mapzoom('antarctic peninsula')
%
% figure
% bedmap2('patchshelves')
% bedmap2('patchgl')
% mapzoom('Wilkins Ice shelf')
% scarlabel('Wilkins Ice Shelf','fontangle','italic')
%
% figure
% bedmap2('velocity','caxis',[0 1500])
% mapzoom('recovery glacier','mapwidth',1000,...
% 'inset','southeast','insetsize',.4,...
% 'frame','off')
%
% figure
% bedmap2('patchshelves')
% bedmap2('patchgl')
% mapzoom(-80,-120,2000)
%
% figure
% bedmap2 bed
% mapzoom('thwaites glacier','inset','northwest',...
% 'mapwidth',2000)
% scalebar('length',500,'color','white')
%
%
%% Author info and function history
%
% September 2013: This function was originally created as bedmap2_zoom.
%
% August 2014: The function was renamed mapzoom and made more efficient
% and robust. A major improvement to the inset feature has been made possible
% by Kelly Kearney's plotboxpos function (http://www.mathworks.com/matlabcentral/fileexchange/9615)
% which is included as a subfunction in mapzoom.
%
% January 2015: A minor change: Inset map axes are now given a tag 'insetmap'
% so if mapzoom is called multiple times, previous insets are found and
% deleted before placing a new inset map.
%
% Created by Chad A. Greene
% Institute for Geophysics
% The University of Texas at Austin
%
% See also scarloc and mapzoomps.
%% Check state, initialize a map if needed
assert(license('test','map_toolbox')==1,'Cannot find an active license for the Mapping Toolbox. Matlab''s Mapping Toolbox is required for use of the mapzoom function. But you can use mapzoomps instead!')
try % Is a map already initialized?
mapinitialized = getm(gca,'MapProjection');
catch
antmap;
end
% Get viewing angle of map:
[az,el] = view;
%% Parse inputs:
inset=false;
centeredmethod=false; % refers to whether the map is centered on a location
% Check for inset declaration:
tmp = strcmpi(varargin,'inset');
if any(tmp)
inset = true;
insetsize = 0.25;
frameon = true;
try
tmpi = find(tmp)+1;
if strcmpi(varargin{tmpi},'southwest')||strcmpi(varargin{tmpi},'northwest')||...
strcmpi(varargin{tmpi},'southeast')||strcmpi(varargin{tmpi},'northeast')||...
strcmpi(varargin{tmpi},'sw')||strcmpi(varargin{tmpi},'nw')||...
strcmpi(varargin{tmpi},'se')||strcmpi(varargin{tmpi},'ne')
location = varargin{tmpi};
tmp(tmpi)=1;
end
catch
location = 'southeast';
end
varargin = varargin(~tmp);
end
% check for frame declaration:
tmp = strcmpi(varargin,'box')|strcmpi(varargin,'frame');
if any(tmp)
if strcmpi(varargin{find(tmp)+1},'off')||strcmpi(varargin{find(tmp)+1},'none');
frameon = false;
end
tmp(find(tmp)+1) = 1;
varargin = varargin(~tmp);
end
nk = 1:length(varargin);
% check for inset size declaration:
for k = 1:length(varargin)
if strcmpi(varargin{k},'size')||strcmpi(varargin{k},'insetsize')
insetsize = varargin{k+1};
nk(k:k+1)=[];
end
end
varargin = varargin(nk);
mapwidthkm = 500; % sets default map width to 500 km
for k = 1:length(varargin)
if strcmpi(varargin{k},'mapwidth')||strcmpi(varargin{k},'mapwidthkm')||...
strcmpi(varargin{k},'width')
mapwidthkm = varargin{k+1};
end
end
if length(varargin)>2
mapwidthkm = varargin{end};
elseif ischar(varargin{1}) && ~ischar(varargin{end})
mapwidthkm = varargin{end};
end
% Find and delete old inset if one exists:
try
hinsetmap = findobj(gcf,'tag','insetmap');
delete(hinsetmap);
end
if ischar(varargin{1})
switch lower(varargin{1})
case {'unzoom','default','continent','antarctica'}
set(gca,'xlim',[-0.4802 0.4802],'ylim',[-0.4802 0.4802]);
otherwise
[centerlat,centerlon] = scarloc(varargin{1});
centeredmethod=true;
end
else
if islatlon(varargin{1},varargin{2})
centerlat = varargin{1};
centerlon = varargin{2};
else
[centerlat,centerlon] = ps2ll(varargin{1},varargin{2});
end
centeredmethod=true;
end
if centeredmethod
[lat1,lon1]=minvtran(0,0);
[lat2,lon2]=minvtran(.1,0);
kmpermapunit = 10*distance(lat1,lon1,lat2,lon2,6378.137);
[centerx,centery] = mfwdtran(centerlat,centerlon);
unitstoedge = .5*mapwidthkm/kmpermapunit;
set(gca,'xlim',[centerx-unitstoedge centerx+unitstoedge],...
'ylim',[centery-unitstoedge centery+unitstoedge])
end
if inset
load AMTdata.mat % contains crude grounding line and ice shelf outlines, approximated and downsampled significantly from Bedmap2
gcah = gca;
gp = plotboxpos(gca);
insetwidth = insetsize*gp(3);
insetheight = insetsize*gp(4);
switch lower(location)
case {'southwest','sw'}
insetx = gp(1);
insety = gp(2);
case {'northeast','ne'}
insetx = gp(1) + gp(3) - insetwidth;
insety = gp(2) + gp(4) - insetheight;
case {'northwest','nw'}
insetx = gp(1);
insety = gp(2) + gp(4) - insetheight;
case {'southeast','se'}
insetx = gp(1) + gp(3) - insetwidth;
insety = gp(2);
otherwise
error('Unrecognized inset location.')
end
axes('position',[insetx insety insetwidth insetheight],'tag','insetmap');
antmap('oceancolor','white')
for k = 1:30
patchm(slat{k},slon{k},[.8 .8 .8])
end
for k = 1:30
patchm(glat{k},glon{k},[.5 .5 .5])
end
if ~frameon
antmap('frame','off')
end
plot([centerx-unitstoedge centerx-unitstoedge centerx+unitstoedge...
centerx+unitstoedge centerx-unitstoedge],...
[centery-unitstoedge centery+unitstoedge centery+unitstoedge...
centery-unitstoedge centery-unitstoedge],'r-','linewidth',1);
view([az,el]); % Sets inset rotation to match main map rotation
axes(gcah); % returns to original axes
uistack(gcah,'down');
end
%% Kelly Kearney's plotboxpos function:
function pos = plotboxpos(h)
%PLOTBOXPOS Returns the position of the plotted axis region
%
% pos = plotboxpos(h)
%
% This function returns the position of the plotted region of an axis,
% which may differ from the actual axis position, depending on the axis
% limits, data aspect ratio, and plot box aspect ratio. The position is
% returned in the same units as the those used to define the axis itself.
% This function can only be used for a 2D plot.
%
% Input variables:
%
% h: axis handle of a 2D axis (if ommitted, current axis is used).
%
% Output variables:
%
% pos: four-element position vector, in same units as h
% Copyright 2010 Kelly Kearney
% Check input
if nargin < 1
h = gca;
end
if ~ishandle(h) || ~strcmp(get(h,'type'), 'axes')
error('Input must be an axis handle');
end
% Get position of axis in pixels
currunit = get(h, 'units');
set(h, 'units', 'pixels');
axisPos = get(h, 'Position');
set(h, 'Units', currunit);
% Calculate box position based axis limits and aspect ratios
darismanual = strcmpi(get(h, 'DataAspectRatioMode'), 'manual');
pbarismanual = strcmpi(get(h, 'PlotBoxAspectRatioMode'), 'manual');
if ~darismanual && ~pbarismanual
pos = axisPos;
else
dx = diff(get(h, 'XLim'));
dy = diff(get(h, 'YLim'));
dar = get(h, 'DataAspectRatio');
pbar = get(h, 'PlotBoxAspectRatio');
limDarRatio = (dx/dar(1))/(dy/dar(2));
pbarRatio = pbar(1)/pbar(2);
axisRatio = axisPos(3)/axisPos(4);
if darismanual
if limDarRatio > axisRatio
pos(1) = axisPos(1);
pos(3) = axisPos(3);
pos(4) = axisPos(3)/limDarRatio;
pos(2) = (axisPos(4) - pos(4))/2 + axisPos(2);
else
pos(2) = axisPos(2);
pos(4) = axisPos(4);
pos(3) = axisPos(4) * limDarRatio;
pos(1) = (axisPos(3) - pos(3))/2 + axisPos(1);
end
elseif pbarismanual
if pbarRatio > axisRatio
pos(1) = axisPos(1);
pos(3) = axisPos(3);
pos(4) = axisPos(3)/pbarRatio;
pos(2) = (axisPos(4) - pos(4))/2 + axisPos(2);
else
pos(2) = axisPos(2);
pos(4) = axisPos(4);
pos(3) = axisPos(4) * pbarRatio;
pos(1) = (axisPos(3) - pos(3))/2 + axisPos(1);
end
end
end
% Convert plot box position to the units used by the axis
temp = axes('Units', 'Pixels', 'Position', pos, 'Visible', 'off', 'parent', get(h, 'parent'));
set(temp, 'Units', currunit);
pos = get(temp, 'position');
delete(temp);