-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathm_scale.m
96 lines (75 loc) · 2.56 KB
/
m_scale.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
function scale_factor=m_scale(scale_factor),
% M_SCALE Draws the map at a specified scale
% After M_GRID has been called, the map is sized to fit within
% the plot region of the current axes. If you want to force the
% map to appear at a specific scale (e.g. 1:250000), call
%
% M_SCALE(scale_factor)
%
% where the map scale is 1:scale_factor. The map will be drawn
% with its origin at bottom left within the figure limits (which
% can be set with calls to ORIENT or by setting the 'paperposition'
% property of the figure).
%
% M_SCALE('auto') returns to autoscaling.
%
% SC=M_SCALE returns the current scale (this is useful if you want to
% know roughly wht scale you currently have)
%
% see also M_PROJ, M_GRID.
% Rich Pawlowicz (rich@ocgy.ubc.ca) 13/Nov/1998
% This software is provided "as is" without warranty of any kind. But
% it's mine, so you can't sell it.
% 1/Feb/99 - added scale calculations for return value.
% 6/Nov/00 - eliminate returned stuff if ';' neglected (thx to D Byrne)
% 4/DEc/11 - isstr to ischar
%
% Need earth radius, in centimeters.
erad=637813700; %cm (from WGS-84)
if nargin==0,
au=get(gca,'units');
set(gca,'units','centimeter');
a_pos=get(gca,'position');
set(gca,'units',au);
map_x=diff(get(gca,'xlim'));
map_y=diff(get(gca,'ylim'));
if map_x>10, % we are probably using meters (i.e. UTM coords)
scale_factor=max(map_x/(a_pos(3)/100),map_y/(a_pos(4)/100));
else
scale_factor=max(map_x*erad/(a_pos(3)),map_y*erad/(a_pos(4)));
end;
else
if ischar(scale_factor),
gca_pos=get(gca,'userdata');
if ~isempty(gca_pos),
set(gca,'position',gca_pos);
end;
else
fu=get(gcf,'units');
set(gcf,'units','centimeter');
f_pos=get(gcf,'position');
set(gcf,'units',fu);
map_x=diff(get(gca,'xlim'));
map_y=diff(get(gca,'ylim'));
if map_x>10, % we are probably using meters (i.e. UTM coords)
map_x=map_x/scale_factor*100;
map_y=map_y/scale_factor*100;
else
map_x=map_x*erad/scale_factor;
map_y=map_y*erad/scale_factor;
end;
if map_x> f_pos(3) | map_y>f_pos(4),
disp('Warning - map larger than current window at this scale');
elseif map_x< f_pos(3)/2 & map_y<f_pos(4)/2,
disp('Warning - map much smaller than current window at this scale');
end;
if isempty(get(gca,'userdata')),
set(gca,'userdata',get(gca,'position'));
end;
gca_u=get(gca,'unit');
set(gca,'unit','centi','position',[1 1 map_x map_y],'unit',gca_u)
end;
end;
if nargout==0,
clear scale_factor
end;