📄 m_grid.m
字号:
function m_grid(varargin);% M_GRID make a grid on a map.% M_GRID('parameter','value',...) with any number (or no)% optional parameters is used to draw a lat/long grid for a% previously initialized map projection.%% The optional parameters allow the user% to control the look of the grid. These parameters are listed% by M_GRID('get'), with defualt parameters in M_GRID('set');%% see also M_PROJ% Rich Pawlowicz (rich@ocgy.ubc.ca) 2/Apr/1997%% This software is provided "as is" without warranty of any kind. But% it's mine, so you can't sell it.%% 19/6/97 - set visibility of titles and so forth to 'on' (they% default to 'off' when axes visibility is turned off)% 2/11/97 - for M5.1, the old way of making the patch at the bottom (i.e.% by rearranging the axes children) instead causes matlab to loose% track of titles. Try a different fix.% 11/01/98 - Added way of making longitude lines cut off to prevent crowding near poles (you have% to specify a vector for allowabale latitudes for this to work).% 16/02/98 - Made a little fudge to allow the user to fully specify grid location% without getting the edge points. It doesn't quite work if only *one* edge% point is desired....but I hope it will be OK that way.% 19/02/98 - PC-users complain about layers getting out of order! Their fault for using% such an awful OS...however (with help from Eric Firing) I think I have% a fix.% 7/04/98 - Another fix to grid locations to not automatically add edge points% (as requested by EF)% 7/05/98 - Added 'fancy' outline box.% 14/11/98 - Changed tag names from m_* to m_grid_*.% 11/07/99 - Apparently fontname changing didn't work (thanks to Dave McCollum)% 28/04/04 - Changed m_grid_color code so it works right under unix; old% way retained for windows (ugh).% 16/10/05 - Kirk Ireson discovered that the way to fix those annoying 'cut-throughs'% in fancy_box was to add a 'large' zdata...so I've adapted his fix in% fancybox and fancybox2.% 21/11/06 - added 'backcolor'% 16/4/07 - sorted ticklabels when user-specified (prevents an odd problem near in% azimuthal projections).% Note that much of the work in generating line data % is done by calls to the individual projections - % most of M_GRID is concerned with the mechanics of plotting% These structures are initialized by m_proj()global MAP_PROJECTION MAP_VAR_LIST% Have to have initialized a map firstif isempty(MAP_PROJECTION), disp('No Map Projection initialized - call M_PROJ first!'); return;end;% Otherwise we are drawing a grid!% Set current projection to geographicCurrentmap=m_coord('set');m_coord(MAP_PROJECTION.coordsystem.name);% Default parameters for gridxtick=6;ytick=6;xlabels=NaN;ylabels=NaN;gcolor='k';gbackcolor='w'; %%get(gcf,'color');glinestyle=':';glinewidth=get(gca,'linewidth');gbox='on'; gfontsize=get(gca,'fontsize');gfontname=get(gca,'fontname');gxaxisloc=get(gca,'xaxislocation'); gyaxisloc=get(gca,'yaxislocation');gtickdir=get(gca,'tickdir'); gticklen=get(gca,'ticklen'); gticklen=gticklen(1); gxticklabeldir='middle';gyticklabeldir='end';dpatch=5; % interpolation factor for fancy grids% Parse parameter list for options. I really should do some% error checking here, but...k=1;while k<=length(varargin), switch lower(varargin{k}(1:3)), case 'box', gbox=varargin{k+1}; case 'xti', if length(varargin{k})==5, xtick=sort(varargin{k+1}); % Added 'sort' here for people who put things in else % a random order near poles xlabels=varargin{k+1}; end; case 'yti', if length(varargin{k})==5, ytick=sort(varargin{k+1}); else ylabels=varargin{k+1}; end; case 'xla', gxticklabeldir=varargin{k+1}; case 'yla', gyticklabeldir=varargin{k+1}; case 'col', gcolor=varargin{k+1}; case 'bac', gbackcolor=varargin{k+1}; case 'lin', switch lower(varargin{k}(1:5)), case 'linew', glinewidth=varargin{k+1}; case 'lines', glinestyle=varargin{k+1}; end; case 'fon', switch lower(varargin{k}(1:5)), case 'fonts', gfontsize=varargin{k+1}; case 'fontn', gfontname=varargin{k+1}; end; case 'xax', gxaxisloc=varargin{k+1}; case 'yax', gyaxisloc=varargin{k+1}; case 'tic', switch lower(varargin{k}(1:5)), case 'tickl', gticklen=varargin{k+1}; case 'tickd', gtickdir=varargin{k+1}; end; case {'get','usa'}, disp(' ''box'',( ''on'' | ''fancy'' | ''off'' )'); disp(' ''xtick'',( num | [value1 value2 ...])'); disp(' ''ytick'',( num | [value1 value2 ...])'); disp(' ''xticklabels'',[label1;label2 ...]'); disp(' ''yticklabels'',[label1;label2 ...]'); disp(' ''xlabeldir'', ( ''middle'' | ''end'' )'); disp(' ''ylabeldir'', ( ''end'' | ''middle'' )'); disp(' ''ticklen'',value'); disp(' ''tickdir'',( ''in'' | ''out'' )'); disp(' ''color'',colorspec'); disp(' ''backcolor'',colorspec'); disp(' ''linewidth'', value'); disp(' ''linestyle'', ( linespec | ''none'' )'); disp(' ''fontsize'',value'); disp(' ''fontname'',name'); disp(' ''XaxisLocation'',( ''bottom'' | ''middle'' | ''top'' ) '); disp(' ''YaxisLocation'',( ''left'' | ''middle'' | ''right'' ) '); return; case 'set', disp([' box = ' gbox]); disp([' xtick = ' num2str(xtick)]); disp([' ytick = ' num2str(ytick)]); disp([' ticklen = ' num2str(gticklen)]); disp([' tickdir = ' gtickdir]); disp([' xlabeldir = ' gxticklabeldir]); disp([' ylabeldir = ' gyticklabeldir]); disp([' color = ' gcolor]); disp([' linewidth = ' num2str(glinewidth)]); disp([' linestyle = ' glinestyle]); disp([' fontsize = ' num2str(gfontsize)]); disp([' fontname = ' gfontname]); disp([' XaxisLocation = ' gxaxisloc]); disp([' YaxisLocation = ' gyaxisloc]); return; end; k=k+2;end; if strcmp(gbox,'fancy'), if strcmp(MAP_VAR_LIST.rectbox,'on') | strcmp(MAP_VAR_LIST.rectbox,'circle'), gbox='on'; warning([' No fancy outline with ''rectbox'' set to ''' MAP_VAR_LIST.rectbox '''']); end;end;% Draw the plot box[X,Y]=feval(MAP_PROJECTION.routine,'box');if strcmp(gbox,'on'); line(X(:),Y(:),'linest','-','linewi',glinewidth,'color',gcolor,'tag','m_grid_box','clip','off');end;% Axes background - to defeat the inverthardcopy, I need a non-white border (the edgecolor),% but sneakily I can set it's width to (effectively) 0 so it doesn't actually show!% Now, putting in a white background works under linux (at least) and% NOT under windows...I don't know about macs.%%a=ver('matlab'); % Ver doesn't return stuff under v5!a=version;%%if sscanf(a.Version,'%f') >6.0 & ~ispc,if sscanf(a(1:3),'%f') >6.0 & ~ispc, patch('xdata',X(:),'ydata',Y(:),'zdata',-bitmax*ones(size(X(:))),'facecolor',gbackcolor,... 'edgecolor','k','linest','none','tag','m_grid_color');else% Now, I used to set this at a large (negative) zdata, but this didn't work for PC users,% so now I just draw a patch...but I have decided to go back to the old% way (above) with higher versions. Maybe the PC version works now?% Unfortunately this kludge has some strange side-effects. patch('xdata',X(:),'ydata',Y(:),'facecolor',gbackcolor,... 'edgecolor','k','linest','none','tag','m_grid_color'); % Now I set it at the bottom of the children list so it gets drawn first (i.e. doesn't % cover anything) show=get(0, 'ShowHiddenHandles'); set(0, 'ShowHiddenHandles', 'on'); hh=get(gca,'children'); htags = get(hh,'tag'); k = strmatch('m_grid_color',htags); hht = hh; hh(k) = []; hh = [hh;hht(k)]; set(gca,'children',hh); set(0, 'ShowHiddenHandles', show);end;% X-axis labels and gridif ~isempty(xtick), % Tricky thing - if we are drawing a map with the poles, its nasty when the lines get too close % together. So we can sort of fudge this by altering MAP_VAR_LIST.lats to be slightly smaller, % and then changing it back again later. fudge_north='n';fudge_south='n'; if ~isempty(ytick) & length(ytick)>1, if MAP_VAR_LIST.lats(2)==90, fudge_north='y'; MAP_VAR_LIST.lats(2)=ytick(end); end; if MAP_VAR_LIST.lats(1)==-90, fudge_south='y'; MAP_VAR_LIST.lats(1)=ytick(1); end; end; [X,Y,lg,lgI]=feval(MAP_PROJECTION.routine,'xgrid',xtick,gxaxisloc); [labs,scl]=m_labels('lon',lg,xlabels); % Draw the grid. Every time we draw something, I first reshape the matrices into a long % row so that a) it plots faster, and b) all lines are given the same handle (which cuts % down on the number of children hanging onto the axes). [n,m]=size(X); line(reshape([X;NaN+ones(1,m)],(n+1)*m,1),reshape([Y;NaN+ones(1,m)],(n+1)*m,1),... 'linest',glinestyle,'color',gcolor,'linewidth',0.1,'tag','m_grid_xgrid'); % Get the tick data [ltx,lty,utx,uty]=maketicks(X,Y,gticklen,gtickdir); % Draw ticks if labels are on top or bottom (not if they are in the middle) if strcmp(gxticklabeldir,'middle'), if lgI==size(X,1) & strcmp(gxaxisloc,'top'), % Check to see if the projection supports this option. vert='bottom';horiz='center';drawticks=1; xx=utx(1,:);yy=uty(1,:);rotang=atan2(diff(uty),diff(utx))*180/pi+90; elseif lgI==1 & strcmp(gxaxisloc,'bottom') vert='top';horiz='center';drawticks=1; xx=ltx(1,:);yy=lty(1,:);rotang=atan2(diff(lty),diff(ltx))*180/pi-90; else vert='middle';horiz='center';lgIp1=lgI+1;drawticks=0; xx=X(lgI,:); yy=Y(lgI,:);rotang=atan2(Y(lgIp1,:)-Y(lgI,:),X(lgIp1,:)-X(lgI,:))*180/pi-90; end; else if lgI==size(X,1) & strcmp(gxaxisloc,'top'), % Check to see if the projection supports this option. vert='middle';horiz='left';drawticks=1; xx=utx(1,:);yy=uty(1,:);rotang=atan2(diff(uty),diff(utx))*180/pi+180; elseif lgI==1 & strcmp(gxaxisloc,'bottom') vert='middle';;horiz='right';drawticks=1; xx=ltx(1,:);yy=lty(1,:);rotang=atan2(diff(lty),diff(ltx))*180/pi; else vert='top';;horiz='center';lgIp1=lgI+1;drawticks=0; xx=X(lgI,:); yy=Y(lgI,:);rotang=atan2(Y(lgIp1,:)-Y(lgI,:),X(lgIp1,:)-X(lgI,:))*180/pi; end; end; if strcmp(gbox,'fancy'), if gtickdir(1)=='i', fancybox(lg,MAP_VAR_LIST.longs,'xgrid','bottom',dpatch,gticklen); drawticks=0; else fancybox2(lg,MAP_VAR_LIST.longs,'xgrid','bottom',dpatch,gticklen); end; end; if drawticks, [n,m]=size(ltx); line(reshape([ltx;NaN+ones(1,m)],(n+1)*m,1),reshape([lty;NaN+ones(1,m)],(n+1)*m,1),... 'linest','-','color',gcolor,'linewidth',glinewidth,'tag','m_grid_xticks-lower','clip','off'); line(reshape([utx;NaN+ones(1,m)],(n+1)*m,1),reshape([uty;NaN+ones(1,m)],(n+1)*m,1),... 'linest','-','color',gcolor,'linewidth',glinewidth,'tag','m_grid_xticks-upper','clip','off'); end; % Add the labels! (whew) ik=1:size(X,2); for k=ik, [rotang(k), horizk, vertk] = upright(rotang(k), horiz, vert); text(xx(k),yy(k),labs{k},'horizontal',horizk,'vertical',vertk, ... 'rot',rotang(k),'fontsize',gfontsize*scl(k),'color',gcolor,... 'tag','m_grid_xticklabel','fontname',gfontname); end; if fudge_north=='y', MAP_VAR_LIST.lats(2)=90; end; if fudge_south=='y', MAP_VAR_LIST.lats(1)=-90; end;end;if ~isempty(ytick), % Y-axis labels and grid [X,Y,lt,ltI]=feval(MAP_PROJECTION.routine,'ygrid',ytick,gyaxisloc); [labs,scl]=m_labels('lat',lt,ylabels); % Draw the grid [n,m]=size(X); line(reshape([X;NaN+ones(1,m)],(n+1)*m,1),reshape([Y;NaN+ones(1,m)],(n+1)*m,1),... 'linest',glinestyle,'color',gcolor,'linewidth',0.1,'tag','m_grid_ygrid'); % Get the tick data [ltx,lty,utx,uty]=maketicks(X,Y,gticklen,gtickdir); % Draw ticks if labels are on left or right (not if they are in the middle) if strcmp(gyticklabeldir,'end'),
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -