-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdem.m
1426 lines (1258 loc) · 38.6 KB
/
dem.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function varargout=dem(x,y,z,varargin)
%DEM Shaded relief image plot
%
% DEM(X,Y,Z) plots the Digital Elevation Model defined by X and Y
% coordinate vectors and elevation matrix Z, as a lighted image using
% specific "landcolor" and "seacolor" colormaps. DEM uses IMAGESC
% function which is much faster than SURFL when dealing with large
% high-resolution DEM. It produces also high-quality and moderate-size
% Postscript image adapted for publication.
%
% DEM(X,Y,Z,'Param1',Value1,'Param2',Value2,...) specifies options or
% parameter/value couple (case insensitive):
%
% [H,I] = DEM(...); returns graphic handle H and optional illuminated
% image as I, a MxNx3 matrix (if Z is MxN and DECIM is 1).
%
% I = DEM(...,'noplot') returns a structure I containing fields x, y, z,
% and illuminated image .rgb without producing a graph on current figure.
%
%
% --- Lighting options ---
%
% 'Shading', 'light' (default) | 'stack'
% Shading method to combine the relief image and shadow mask:
% 'light' is the default and initial method of dem.m (changing the
% lightness intensity),
% 'stack' is the common transparency method (stacking using an
% opacity value).
%
% 'Azimuth',A
% Light azimuth in degrees clockwise relative to North. Accepts
% a vector for multiple lights. Default is A = [-45,45] for a
% combined north-west and north-east illumination.
%
% 'Elevation',E
% Light elevation (or altitude) (in degrees from the horizon level).
% Default value is E = 45.
%
% 'Opacity',O
% Opacity value (between 0 and 1) of the relief image when using the
% 'stack' shading method. Default value is O = 0.5.
%
% 'Contrast',C
% Light contrast, as the exponent of the gradient value:
% C = 1 for linear contrast (default),
% C = 0 to remove lighting,
% C = 0.5 for moderate lighting,
% C = 2 or more for stronger contrast.
%
% 'LCut',LC
% Lighting scale saturation cut with a median-style filter in % of
% elements, such as LC% of maximum gradient values are ignored (0 is
% default for full scale gradient).
%
% 'km'
% Stands that X and Y coordinates are in km instead of m (default).
% This allows correct lighting. Ignored if LATLON option is used.
%
%
% --- Elevation colorscale options ---
%
% 'ZLim',[ZMIN,ZMAX]
% Fixes min and max elevation values for colormap. Use NaN to keep
% real min and/or max data values.
%
% 'ZCut',ZC
% Median-style filter to cut extremes values of Z (in % of elements),
% such that ZC% of most min/max elevation values are ignored in the
% colormap application:
% ZC = 0.5 is default,
% ZC = 0 for full scale.
%
%
% --- "No Value" elevation options ---
%
% 'NoValue',NOVALUE
% Defines the values that will be replaced by NaN. Note that values
% equal to minimum of Z class are automatically detected as NaN
% (e.g., -32768 for int16 class).
%
% 'NaNColor',[R,G,B]
% Sets the RGB color for NaN/NoValue pixels (default is a dark gray).
% Note that your must specify a valid 3-scalar vector (between 0 and
% 1); color characters like 'w' or 'k' are not allowed, use [1,1,1]
% or [0,0,0] instead.
%
% 'Interp'
% Interpolates linearly all NaN values (fills the gaps using linear
% triangulation), using an optimized algorithm.
%
%
% --- Colormap options ---
%
% 'LandColor',LMAP
% Uses LMAP colormap instead of default (landcolor, if exists or
% jet) for Z > 0 elevations.
%
% 'SeaColor',SMAP
% Sets the colormap used for Z <= 0 elevations. Default is seacolor
% (if exists) or single color [0.7,0.9,1] (a light cyan) to simulate
% sea color.
%
% 'ColorMap',CMAP
% Uses CMAP colormap for full range of elevations, instead of default
% land/sea. This option overwrites LANDCOLOR/SEACOLOR options.
%
% 'Lake'
% Detects automatically flat areas different from sea level (non-zero
% elevations) and colors them as lake surfaces.
%
% 'LakeZmin',ZMIN
% Activates the 'lake' option only above ZMIN elevations. For
% example, use 'lakezmin',0 to limit lake detection on land.
%
% 'Saturation',N
% Changes the whole image color saturation by a factor of N.
%
% 'GrayScale'
% Converts the used colormap(s) to grayscale (colour-blind).
%
% 'Watermark',N
% Makes the whole image lighter by a factor of N.
%
%
% --- Basemap and scale options ---
%
% 'Legend'
% Adds legends to the right of graph: elevation scale (colorbar)
% and a distance scale (in km).
%
% 'Cartesian'
% Plots classic basemap-style axis, considering coordinates X and Y
% as cartesian in meters. Use parameter "km' for X/Y in km.
%
% 'LatLon'
% Plots geographic basemap-style axis in deg/min/sec, considering
% coordinates X as longitude and Y as latitude. Axis aspect ratio
% will be adjusted to approximatively preserve distances (this is
% not a real projection!). This overwrites ZRatio option.
%
% 'AxisEqual', 'auto' (default) | 'manual' | 'off'
% When 'Cartesian' or 'LatLon' option is used, automatic axes scaling
% is applied to respect data aspect ratio. Default mode is 'auto' and
% uses AXIS EQUAL and DASPECT functions. The 'manual' mode modifies
% axes width or height with respect to the paper size in order to
% produce correct data scaling at print (but not necessarily at
% screen). The 'off' mode disables any scaling.
%
% Additionnal options for basemap CARTESIAN, LATLON, and LEGEND:
%
% 'BorderWidth',BW
% Border width of the basemap axis, in % of axis height. Default is
% BW = 1%.
%
% 'XTick',DX
% 'YTick',DY
% X and Y Tick length (same unit as X and Y). Default is automatic.
% Tick labels are every 2 ticks.
%
% 'FontSize',FS
% Font size for X and Y tick labels. Default is FS = 10.
%
% 'FontBold'
% Font weight bold for tick labels.
%
% 'Position',P
% Position of the tick labels: 'southwest' (default), 'southeast',
% 'northwest','northeast'
%
% 'ZUnit',ZU
% Sets the elevation unit as string ZU, used when min/max values are
% indicated on colorbar legend (default is 'm').
%
% --- Decimation options ---
%
% For optimization purpose, DEM will automatically decimate data to limit
% to a total of 1500x1500 pixels images. To avoid this, use following
% options, but be aware that large grids may require huge computer
% ressources or induce disk swap or memory errors.
%
% 'Decim',N
% Decimates matrix Z at 1/N times of the original sampling.
% If N < 0, oversamples at -N rate.
%
% 'NoDecim'
% Forces full resolution of Z, no decimation (N =1).
%
%
%
% --- Informations ---
%
% Colormaps are Mx3 RGB matrix so it is easy to modify contrast
% (CMAP.^N), set darker (CMAP/N), lighter (1 - 1/N + CMAP/N), inverse
% it (flipud(CMAP)), etc...
%
% To get free worldwide topographic data (SRTM), see READHGT function.
%
% For backward compatibility, the former syntax is still accepted:
% DEM(X,Y,Z,OPT,CMAP,NOVALUE,SEACOLOR) where OPT = [A,C,LC,ZMIN,ZMAX,ZC],
% also option aliases DEC, DMS and SCALE, but there is no argument
% checking. Please prefer the param/value syntax.
%
% Author: François Beauducel <[email protected]>
%
% Acknowledgments: Éric Gayer
%
% Created: 2007-05-17 in Guadeloupe, French West Indies
% Updated: 2022-11-26
% History:
% [2022-11-26] v3.2
% - fix an issue with 'grayscale' option
% - allows duplicate arguments (takes the last one)
% [2022-07-26] v3.1
% - minor fix for Octave compatibility
% [2022-07-21] v3.0
% - new option 'shading' (stack method with opacity factor)
% - new option 'elevation' (light elevation for stack shading)
% - lighting with multiple source azimuth
% - graphic improvement of basemap axes corners
% [2021-01-08] v2.11
% - minor optimization for 'interp' option
% [2020-11-29] v2.10
% - new option 'grayscale'
% [2020-11-25] v2.9
% - fix a possible issue with saturation under Matlab < 2014
% [2020-06-01] v2.8
% - new option 'saturation' to change colormaps
% - tick label decimals with smaller font size
% [2019-06-17] v2.7
% - fix an issue for single RGB color in sea/land color option
% [2017-03-29] v2.6
% - fix in 'lakezmin' option (thanks to Mustafa �omo?lu)
% [2017-01-09] v2.5
% - new option 'lakezmin' to limit lake detection
% [2016-12-21] v2.4
% - improves the colormap splitting between land and sea
% [2016-04-19] v2.3
% - major update (thanks to mas Wiwit)
% [2016-01-31] v2.2
% - adds option 'Position' for tick labels
% [2015-08-22] v2.1
% - minor fix (former versions of Matlab compatibility)
% [2015-08-19] v2.0
% - image is now 100% true color (including the legend colorbar),
% thus completely independent from the figure colormap
% [2014-10-14]
% - 'decim' option allows oversampling (negative value)
% [2014-06-06]
% - improves backward compatibility (adds strjoin subfunction)
% [2014-03-18]
% - adds new axisequal option
% [2013-03-11]
% - new options: 'km', 'watermark', 'fontsize', 'bordersize'
% - improve legend colorbar
% - all options now passed as param/value
% [2013-01-14]
% - improved light rendering (using surface normals instead of gradient)
% - improved 'lake' detection algorithm
% - new 'nancolor' option to set NaN color
% - adds a length scale with 'dec' option
% - minor code improvements
% [2013-01-07]
% - adds 'interp' option (fill the gaps)
% - adds 'seacolor' colormap for negative elevations (bathymetry)
% [2013-01-02]
% - adds a 'lake' option
% - minor bug correction
% [2012-09-26]
% - now accepts row/column vectors for X and/or Y.
% [2012-05-29]
% - adds basemap-style axis in decimal or lat/lon modes
% - adds elevation and distance scales
% [2012-05-18]
% - new landcolor.m colormap function
% - new arguments to control colormap scaling
% - median-style filters for light and colormap
% [2012-04-26]
% - Optimizations: adds a decimation for large DEM grids.
%
% Copyright (c) 2007-2022, Fran�ois Beauducel, covered by BSD License.
% All rights reserved.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are
% met:
%
% * Redistributions of source code must retain the above copyright
% notice, this list of conditions and the following disclaimer.
% * Redistributions in binary form must reproduce the above copyright
% notice, this list of conditions and the following disclaimer in
% the documentation and/or other materials provided with the distribution
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
% POSSIBILITY OF SUCH DAMAGE.
if nargin < 3
error('Not enough input arguments.');
end
holdon = ishold;
degkm = 6378*pi/180; % one latitude degree in km
sea_color = [.7,.9,1]; % default sea color (light cyan)
grey = 0.2*[1,1,1]; % a dark gray
% -------------------------------------------------------------------------
% --- Manage input arguments
% number of arguments param/value
nargs = 0;
if ~isnumeric(x) || ~isnumeric(y) || ~isnumeric(z)
error('X,Y and Z must be numeric.')
end
if all(size(x) ~= 1) || all(size(y) ~= 1)
error('X and Y must be vectors, not matrix.')
end
if length(x) ~= size(z,2) || length(y) ~= size(z,1)
error('If Z has a size of [M,N], X must have a length of N, and Y a length of M.')
end
if size(z,3) == 3
rgb = true;
else
rgb = false;
end
% OPTIONS and PARAM/VALUE arguments
% SHADING param/value
[s,shading] = checkparam(varargin,'shading',@ischar,{'light','stack'});
nargs = nargs + 2;
if s==0
shading = 'light'; % default
end
% OPACITY param/value
[s,opacity] = checkparam(varargin,'opacity',@isscalar);
nargs = nargs + 2;
if s==0
opacity = .5; % default
end
% AZIMUTH param/value
[s,az] = checkparam(varargin,'azimuth',@isvec);
nargs = nargs + 2;
if s==0
az = [-45,45]; % default
end
% ZENITH param/value
[s,el] = checkparam(varargin,'elevation',@isscalar);
nargs = nargs + 2;
if s==0
el = 45; % default
end
% CONTRAST param/value
[s,ct] = checkparam(varargin,'contrast',@isscalar);
nargs = nargs + 2;
if s
ct = abs(ct);
else
ct = 1; % default
end
% LCUT param/value
[s,lcut] = checkparam(varargin,'lcut',@isperc);
nargs = nargs + 2;
if s==0
lcut = 0; % default
end
% NOVALUE param/value
[s,novalue] = checkparam(varargin,'novalue',@isscalar);
nargs = nargs + 2;
if s==0
% default: min value for integer class / NaN for float
S = whos('z');
if strfind(S.class,'int')
novalue = intmin(S.class);
else
novalue = NaN;
end
end
% NANCOLOR param/value
[s,novalue_color] = checkparam(varargin,'nancolor',@isrgb);
nargs = nargs + 2;
if s==0
novalue_color = grey; % default
end
% LANDCOLOR param/value
[s,cland] = checkparam(varargin,'landcolor',@isrgb);
nargs = nargs + 2;
if s==0
% default: landcolor or jet
if exist('landcolor','file')
cland = landcolor.^1.3;
else
cland = jet(256);
end
end
% SEACOLOR param/value
[s,csea] = checkparam(varargin,'seacolor',@isrgb);
nargs = nargs + 2;
if s==0
% default: seacolor or single color
if exist('seacolor','file')
csea = seacolor;
else
csea = sea_color;
end
end
% COLORMAP param/value
[s,cmap] = checkparam(varargin,'colormap',@isrgb);
nargs = nargs + 2;
if s
cland = [];
csea = [];
else
% default
cmap = cland;
end
% ZLIM param/value
[s,zmm] = checkparam(varargin,'zlim',@isvec);
nargs = nargs + 2;
if s
zmin = min(zmm);
zmax = max(zmm);
else
zmin = NaN; % default
zmax = NaN; % default
end
% ZCUT param/value
[s,zcut] = checkparam(varargin,'zcut',@isperc);
nargs = nargs + 2;
if s==0
zcut = .5; % default
end
% ZRATIO param/value
[s,zratio] = checkparam(varargin,'zratio',@isscalar);
nargs = nargs + 2;
if s==0
zratio = 1; % default
end
% SATURATION param/value
[s,csat] = checkparam(varargin,'saturation',@isscalar);
nargs = nargs + 2;
if s
csat = abs(csat);
else
csat = 1; % default
end
% WATERMARK param/value
[s,wmark] = checkparam(varargin,'watermark',@isscalar);
nargs = nargs + 2;
if s
wmark = abs(wmark);
else
wmark = 0; % default
end
% DECIM param/value and NODECIM option
[s,decim] = checkparam(varargin,'decim',@isscalar);
if s
decim = round(decim);
nargs = nargs + 2;
else
decim = any(strcmpi(varargin,'nodecim')); % default
nargs = nargs + 1;
end
% LAKEZMIN param/value option
[s,lakezmin] = checkparam(varargin,'lakezmin',@isscalar);
if s
lake = 1;
nargs = nargs + 2;
else
lake = 0;
lakezmin = NaN;
end
% FONTSIZE param/value
[s,fs] = checkparam(varargin,'fontsize',@isscalar);
nargs = nargs + 2;
if s==0
fs = 10; % default
end
% BORDERWIDTH param/value
[s,bw] = checkparam(varargin,'borderwidth',@isperc);
nargs = nargs + 2;
if s==0
bw = 1; % default
end
% XTICK param/value
[s,ddx] = checkparam(varargin,'xtick',@isscalar);
nargs = nargs + 2;
if s==0
ddx = 0; % default (automatic)
end
% YTICK param/value
[s,ddy] = checkparam(varargin,'ytick',@isscalar);
nargs = nargs + 2;
if s==0
ddy = 0; % default (automatic)
end
% POSITION param/value
[s,tpos] = checkparam(varargin,'position',@ischar,{'southwest','southeast','northwest','northeast'});
nargs = nargs + 2;
if s==0
tpos = 'southwest'; % default
end
% ZUNIT param/value
[s,zunit] = checkparam(varargin,'zunit',@ischar);
nargs = nargs + 2;
if s==0
zunit = 'm'; % default
end
% AXISEQUAL param/value
[s,axeq] = checkparam(varargin,'axisequal',@ischar,{'auto','manual','off'});
nargs = nargs + 2;
if s==0 || ~any(strcmpi(axeq,{'manual','off'}))
axeq = 'auto'; % default (automatic)
end
% CROP param/value
[s,crop] = checkparam(varargin,'crop',@isvec,4);
nargs = nargs + 2;
% CLRGB param/value
[s,clrgb] = checkparam(varargin,'CLRGB',@isrgb);
nargs = nargs + 2;
if s==0
clrgb = .5*ones(1,3); % default (mid-grey)
end
% CLLEVEL param/value
[s,cllevel] = checkparam(varargin,'CLLevel',@isvec,1:2);
nargs = nargs + 2;
if s==0
cllevel = [0 0]; % default (auto)
end
% options without argument value
km = any(strcmpi(varargin,'km'));
dec = any(strcmpi(varargin,'cartesian') | strcmpi(varargin,'dec'));
dms = any(strcmpi(varargin,'latlon') | strcmpi(varargin,'dms'));
kmscale = any(strcmpi(varargin,'kmscale'));
scale = any(strcmpi(varargin,'legend') | strcmpi(varargin,'scale'));
inter = any(strcmpi(varargin,'interp'));
lake = any(strcmpi(varargin,'lake')) || lake;
fbold = any(strcmpi(varargin,'fontbold'));
noplot = any(strcmpi(varargin,'noplot'));
clines = any(strcmpi(varargin,'contourlines'));
gscale = any(strcmpi(varargin,'grayscale'));
% for backward compatibility (former syntax)...
nargs = nargs + dec + dms + scale + kmscale + inter + lake + km + fbold + noplot + clines + gscale;
if (nargin - nargs) > 3 && ~isempty(varargin{1})
opt = varargin{1};
if ~isnumeric(opt)
error('OPT = [A,C,S,ZMIN,ZMAX,ZCUT] argument must be numeric.');
end
if ~isempty(opt)
az = opt(1);
end
if length(opt) > 1
ct = opt(2);
end
if length(opt) > 2
lcut = opt(3);
end
if length(opt) > 4
zmin = opt(4);
zmax = opt(5);
end
if length(opt) > 5
zcut = opt(6);
end
end
if (nargin - nargs) > 4 && ~isempty(varargin{2})
cmap = varargin{2};
csea = [];
end
if (nargin - nargs) > 5 && ~isempty(varargin{3})
novalue = varargin{3};
end
if (nargin - nargs) > 6 && ~isempty(varargin{4})
csea = varargin{4};
end
% further test of input arguments
if dms && any(abs(y) > 91)
error('With LATLON option Y must be in valid latitudes interval (decimal degrees).')
end
if km
zratio = 1000;
end
% -------------------------------------------------------------------------
% --- Pre-process DEM data
% crops data if needed
if numel(crop)==4
fprintf('DEM: crops original data from [%g,%g,%g,%g] to [%g,%g,%g,%g]...\n', ...
min(x(:)),max(x(:)),min(y(:)),max(y(:)),crop);
kx = find(x >= crop(1) & x <= crop(2));
ky = find(y >= crop(3) & y <= crop(4));
x = x(kx);
y = y(ky);
z = z(ky,kx,:);
end
% decimates data to avoid disk swap/out of memory...
nmax = 1500;
if decim
n = decim;
else
n = ceil(sqrt(numel(z))/nmax);
end
if n > 1
x = x(1:n:end);
y = y(1:n:end);
z = z(1:n:end,1:n:end,:);
fprintf('DEM: data has been decimated by a factor of %d...\n',n);
end
z = double(z); % necessary for most of the following calculations...
z(z==novalue) = NaN;
if isempty(csea)
k = (z~=0 & ~isnan(z));
else
k = ~isnan(z);
end
if isnan(zmin)
zmin = nmedian(z(k),zcut/100);
end
if isnan(zmax)
zmax = nmedian(z(k),1 - zcut/100);
end
dz = zmax - zmin;
if decim && n < 0
xi = linspace(x(1),x(end),-n*length(x));
yi = linspace(y(1),y(end),-n*length(y))';
[xx,yy] = meshgrid(xi,yi);
z = interp2(x,y,z,xx,yy,'*cubic');
x = xi;
y = yi;
fprintf('DEM: data has been oversampled by a factor of %d...\n',-n);
end
if ~rgb && inter
z = fillgap(x,y,z);
end
% -------------------------------------------------------------------------
% --- Process lighting
if ~rgb && dz > 0
% first check if colormaps have the minimum required size
if size(csea,1) < 2
csea = repmat(csea,256,1);
end
if size(cland,1) < 2
cland = repmat(cland,256,1);
end
% builds the colormap: concatenates seacolor and landcolor around 0
% after interpolation to have exactly one color level per meter.
if ~isempty(csea)
% l = size(csea,1);
% if zmin < 0 && zmax > 0
% r = size(cland,1)*abs(zmin)/zmax/l;
% cmap = cat(1,interp1(1:l,csea,linspace(1,l,ceil(l*r)),'*linear'),cland);
if zmin < 0 && zmax > 0
lcs = size(csea,1);
lcl = size(cland,1);
cmap = cat(1,interp1(1:lcs,csea,linspace(1,lcl,abs(zmin)+1),'*linear'), ...
interp1(1:lcl,cland,linspace(1,lcl,abs(zmax)),'*linear'));
elseif zmax <=0
cmap = csea;
end
end
% normalisation of Z using CMAP and convertion to RGB
I = ind2rgb(uint16(min(max((z - zmin)/dz,0),1)*(size(cmap,1)-1)),cmap);
if ct > 0
dx = diff(x(1:2));
dy = diff(y(1:2));
if dms
dx = dx*degkm*1000;
dy = dy*degkm*1000*cosd(mean(y));
else
dx = zratio*dx;
dy = zratio*dy;
end
% computes lighting from hillshade ArcGIS algotithm
shadow = hillshade(z,az,el,dx,dy,shading);
% computes maximum absolute gradient (median-style), normalizes,
% saturates and duplicates in 3-D matrix
r = repmat(max(min(shadow/nmedian(abs(shadow),1 - lcut/100),1),-1),[1,1,3]);
%r = repmat(max(min(fxy/diff(nmedian(fxy,[lcut/100,1 - lcut/100])),1),-1),[1,1,3]);
%r = repmat(fxy,[1,1,3]);
switch shading
case 'light'
% applies contrast using exponent
rp = (1 - abs(r)).^ct;
% darker for negative hillshade
%I(r<0) = I(r<0).*(1 - abs(r(r<0)));
I = I.*rp;
% lighter for positive gradient
I(r>0) = I(r>0) + (1 - rp(r>0));
otherwise
% applies contrast using exponent
rp = ((r - min(r(:)))/diff(minmax(r))).^(ct*2);
% applies transparency
I = opacity*I + (1-opacity)*rp;
end
end
% set novalues / NaN to nancolor
[i,j] = find(isnan(z));
if ~isempty(i)
I(sub2ind(size(I),repmat(i,1,3),repmat(j,1,3),repmat(1:3,size(i,1),1))) = repmat(novalue_color,size(i,1),1);
end
% lake option
if lake
klake = islake(z);
if ~isnan(lakezmin)
klake(z < lakezmin) = false; % removes indexes below ZMIN
end
else
klake = 0;
end
% set the seacolor (upper color) for 0 values
if ~isempty(csea)
[i,j] = find(z==0 | klake);
if ~isempty(i)
I(sub2ind(size(I),repmat(i,1,3),repmat(j,1,3),repmat(1:3,size(i,1),1))) = repmat(csea(end,:),size(i,1),1);
end
end
txt = '';
elseif rgb
I = z;
txt = '';
else
I = repmat(shiftdim(sea_color,-1),size(z));
cmap = repmat(sea_color,[256,1]);
txt = 'Mak Byur!'; % Splash !
end
% -------------------------------------------------------------------------
% --- applies saturation, grayscale and watermark to image and cmap (for legend)
if gscale
I = rgb2gray(I);
cmap = rgb2gray(cmap);
end
if csat~=1
I = saturation(I,csat);
cmap = saturation(cmap,csat);
end
if wmark
I = watermark(I,wmark);
cmap = watermark(cmap,wmark);
end
if strcmpi(shading,'stack')
cmap = cmap*opacity + sind(el)*(1 - opacity);
end
% -------------------------------------------------------------------------
% --- ends the function when 'noplot' option is on
if noplot
varargout{1} = struct('x',x,'y',y,'z',z,'rgb',I,'cmap',cmap);
return
end
% -------------------------------------------------------------------------
% --- plots the RGB image
hh = imagesc(x,y,I);
if ~isempty(txt)
text(mean(x),mean(y),txt,'Color',sea_color/4, ...
'FontWeight','bold','HorizontalAlignment','center')
end
orient tall; axis xy
if strcmpi(axeq,'auto')
axis equal
end
axis tight
xlim = [min(x),max(x)];
ylim = [min(y),max(y)];
zlim = [min([z(z(:) ~= novalue);zmin]),max([z(z(:) ~= novalue);zmax])];
if dms
% approximates X-Y aspect ratio for this latitude (< 20-m precision for 1x1� grid)
xyr = cos(mean(y)*pi/180);
else
xyr = 1;
end
bw0 = max(diff(xlim)*xyr,diff(ylim))/100;
bwy = bw*bw0; % Y border width = 1%
bwx = bwy/xyr; % border width (in degree of longitude)
% -------------------------------------------------------------------------
% --- Axis basemap style
if dec || dms
axis off
if strcmpi(axeq,'manual')
ppos = get(gcf,'PaperPosition');
apos = get(gca,'Position');
xyf = (xyr*diff(xlim)/apos(3)/ppos(3))/(diff(ylim)/apos(4)/ppos(4));
if xyf >= 1
set(gca,'Position',[apos(1),apos(2),apos(3),apos(4)/xyf]);
else
set(gca,'Position',[apos(1),apos(2),apos(3)*xyf,apos(4)]);
end
end
if strcmpi(axeq,'auto')
if diff(xlim)*xyr <= diff(ylim)
set(gca,'DataAspectRatio',[1,xyr,1])
else
set(gca,'DataAspectRatio',[1/xyr,1,1])
end
end
if bw > 0
% transparent borders with diagonal lines
hold on
patch(xlim([1,1,2,2]),ylim([1,2,2,1]),'k','FaceColor','none','clipping','off')
patch(xlim([1,1,2,2]) + bwx*[-1,-1,1,1],ylim([1,2,2,1]) + bwy*[-1,1,1,-1],'k','FaceColor','none','clipping','off')
plot(repmat(xlim(1) + bwx*[-1;0],1,2),repmat(ylim,2,1) + bwy*[-1,1;0,0],'k-','clipping','off')
plot(repmat(xlim(2) + bwx*[1;0],1,2),repmat(ylim,2,1) + bwy*[-1,1;0,0],'k-','clipping','off')
hold off
end
dlon = {'E','W'};
dlat = {'N','S'};
if fbold
fw = 'bold';
else
fw = 'normal';
end
if ddx == 0
ddx = dtick(diff(xlim),dms);
ddxn = 0;
else
ddxn = double(ddx<0);
ddx = abs(ddx);
end
if ddy == 0
ddy = dtick(diff(ylim),dms);
ddyn = 0;
else
ddyn = double(ddy<0);
ddy = abs(ddy);
end
xtick = (ddx*ceil(xlim(1)/ddx)):ddx:xlim(2);
for xt = xtick(1:2:end)
dt = ddx - max(0,xt + ddx - xlim(2));
dt2 = ddx - max(0,xt + ddx - bwx - xlim(2));
patch(repmat(xt + [0,dt,dt2,0]',[1,2]),[ylim(1) - bwy*[0,0,1,1];ylim(2) + bwy*[0,0,1,1]]','k','clipping','off')
if fs > 0
if ~isempty(regexp(tpos,'north','once'))
text(xt + dt*ddxn,ylim(2) + 1.2*bwy,deg2dms(xt + dt*ddxn,dlon,dec,fs),'FontSize',fs,'FontWeight',fw, ...
'HorizontalAlignment','center','VerticalAlignment','bottom');
else
text(xt + dt*ddxn,ylim(1) - 1.2*bwy,deg2dms(xt + dt*ddxn,dlon,dec,fs),'FontSize',fs,'FontWeight',fw, ...
'HorizontalAlignment','center','VerticalAlignment','top');
end
end
end
ytick = (ddy*ceil(ylim(1)/ddy)):ddy:ylim(2);
for yt = ytick(1:2:end)
dt = ddy - max(0,yt + ddy - ylim(2));
dt2 = ddy - max(0,yt + ddy - bwy - ylim(2));
patch([xlim(1) - bwx*[0,0,1,1];xlim(2) + bwx*[0,0,1,1]]',repmat(yt + [0,dt,dt2,0]',[1,2]),'k','clipping','off')
if fs > 0
if ~isempty(regexp(tpos,'east','once'))
text(xlim(2) + 1.2*bwx,yt + dt*ddyn,deg2dms(yt + dt*ddyn,dlat,dec,fs),'FontSize',fs,'FontWeight',fw, ...
'HorizontalAlignment','center','VerticalAlignment','top','rotation',90);
else
text(xlim(1) - 1.2*bwx,yt + dt*ddyn,deg2dms(yt + dt*ddyn,dlat,dec,fs),'FontSize',fs,'FontWeight',fw, ...
'HorizontalAlignment','center','VerticalAlignment','bottom','rotation',90);
end
end
end
end
% -------------------------------------------------------------------------
% --- Contour lines
% contour lines
if clines
dz = diff(zlim);
% empirical ratio between horizontal extent and elevation interval (dz)
%rzh = dz/min(diff(x([1,end]))*cosd(mean(dlat)),diff(y([1,end])))/degkm/4e2;
% major level lines
if cllevel(1)==0
dd = dtick(dz);
else
dd = abs(cllevel(1));
end
dz0 = ceil(zlim(1)/dd)*dd:dd:floor(zlim(2)/dd)*dd;
dz0(ismember(0,dz0)) = []; % eliminates 0 value
% minor level lines
if numel(cllevel)<2 || cllevel(2)==0
dd = dtick(dz/5);
else
dd = abs(cllevel(2));
end
dz1 = ceil(zlim(1)/dd)*dd:dd:floor(zlim(2)/dd)*dd;
dz1(ismember(dz1,dz0)) = []; % eliminates minor ticks in major ticks
hold on
[~,h] = contour(x,y,z,[0,0,dz1],'-','Color',clrgb);
set(h,'LineWidth',0.1);
[cs,h] = contour(x,y,z,[0,0,dz0],'-','Color',clrgb);
set(h,'LineWidth',1);
if ~isempty(dz0)% && clineslabel
clabel(cs,h,dz0,'Color',clrgb,'FontSize',fs/2,'FontWeight','bold', ...
'LabelSpacing',288,'Margin',fs)
end
hold off
end
% -------------------------------------------------------------------------
% --- Scales legend
%wsc = diff(xlim)*0.01;
wsc = bw0;
xsc = xlim(2) + wsc*2 + bwx;
if scale
% -- elevation scale (colorbar)
zscale = linspace(zmin,zmax,length(cmap))';
yscale = linspace(0,diff(ylim)/2,length(cmap));
ddz = dtick(dz*max(0.5*xyr*diff(xlim)/yscale(end),1));
ztick = (ddz*ceil(zscale(1)/ddz)):ddz:zscale(end);
rgbscale = ind2rgb(uint16(min(max((zscale - zmin)/dz,0),1)*(size(cmap,1)-1)),cmap);
ysc = ylim(1);