File Exchange

image thumbnail

DEM: Shaded relief image plot (digital elevation model)

version 2.6.0.0 (13 KB) by François Beauducel
Pretty shaded relief map as flat image.

19 Downloads

Updated 20 May 2019

View License

This function plots regular grids of elevation X,Y,Z in a more efficient manner than SURFL Matlab's function, because it recomputes lighting and displays result as shaded color flat RGB image. It uses also median-style filter to deal with min/max values of elevations and gradient, and proposes two specific colormaps "landcolor" and "seacolor".
Color mapping and lighting parameters can be changed from default values. In addition, several options are available: 'cartesian' to add decimal axis, 'latlon' to add geographical axis (GMT-like), 'legend' for an automatic scaling legend, 'lake' for automatic flat area color-filling and 'interp' to fill the novalue gaps...
This may be useful to produce high-quality and moderate-size Postscript image adapted for publication. Also, the output image is not dependant on current figure colormap, thus additional plots with indexed colors can be added (contour, pcolor, ...).
Figure examples:
Moon North Pole using the bone colormap and high contrast lighting (DEM source: raster LRO/LOLA LTVT)
Indonesia archipelago using default colormaps, 'latlon' axis basemap and legend scales (DEM source: raster NOAA/NGDC ETOPO1)

Soufrière of Guadeloupe volcano lava dome: 1-m resolution with NaN values (DEM source: OVSG-IPGP/SCIAC)

See "doc dem" for syntax, examples and help. See also the READHGT function from same author.

Cite As

François Beauducel (2019). DEM: Shaded relief image plot (digital elevation model) (https://www.mathworks.com/matlabcentral/fileexchange/36380-dem-shaded-relief-image-plot-digital-elevation-model), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (36)

in addition, if you want to control everything on the colormap, set ZCUT to zero (...,'Zcut',0) to avoid the automatic median filter effect, and eventually use [ZMIN,ZMAX] option to set your own elevation limits.

Hi Calvin,
ZMAX and ZMIN defines the max and min values of elevations for colormap instead of default which are the real min/max values of your DEM. If ZLIM is specified as input argument, it will use these values to fix the colormap scale. With default colormap ZMIN will be associated to dark blue and ZMAX to brown.
ZCUT is a median filter that uses all elevation data and "cut" a % of their lowest and highest values to adjust the colormap scale. By default, ZCUT is set to 0.5%, e.g., with a 500x500 DEM grid, the 1250 lowest elevation data and 1250 highest elevation data will be "overscaled" in the colormap, i.e., associated to min and max colors. This is useful to avoid unwanted effects when there is spikes values in the DEM. If the DEM is smooth, the small adjustment will not be detectable.

Calvin Hill

Hi François,

Thanks for the answer. One more question: I am trying to find the relationship between Zcut and Zlim (more specifically, could one calculate ZCUT knowing ZMAX and ZMIN?). Thanks again

Dear Calvin,
Yes it is normal, you must use:
axis xy
after imshow() or imagesc() functions because they suppose an inverted Y-axis as for a photo. You can also use:
imagesc(x,y,I)
to get an image not vertically flipped.

Calvin Hill

Is there a reason why when trying to view the illuminated image I created by [H,I] = dem(x,y,z) using imshow(I), the image is vertically flipped (as opposed to the one see when just running dem(x,y,z)? Thanks in advance for the help! Really appreciate the great work

PeymanS

Thanks, François for your awesome code but why I get the following error for readhgt(26:38,44:63):

Error using interp1>reshapeAndSortXandV (line
424)
X and V must be of the same length.

Error in interp1 (line 93)
[X,V,orig_size_v] =
reshapeAndSortXandV(varargin{1},varargin{2});

Error in dem (line 590)
cmap =
cat(1,interp1(1:lcs,csea,linspace(1,lcl,abs(zmin)),'*linear'),
...

Error in readhgt>fplot (line 531)
dem(x,y,z,demoptions{:},'fontsize',40)

Error in readhgt (line 486)
fplot(Y.lon,Y.lat,Y.z,decim,url,novalue)

Gia Albano

I just had to change the fontsizes of the labels but overall very useful. François Beauducel also replies promptly

Hi,
Is there a simple way to use your code to calculate the extent of shadows given a DEM and sun-earth geometry? I am trying to mask shadows from an image, so I'd like to produce a map of 0s and 1s, 0 if there is a shadow cast on the pixel location and 1 if there is not.

C G

Shuo Yang

Is there a way to use input X,Y that are matrices of same size of Z?

I cannot seem to override the colormap options and define my own, I am sorry to keep asking for help. I have been trying to do this for last 2 days.

My apologies for posting inappropriately to the readhgt function.

Is there a parameter and value I can add to my dem() to output it as a gray colormap instead of the RGB relief map?

Hi,
From X,Y,Z values you have to interpolate them into regular grid before using dem.m
For example use the function griddata from Matlab.
Cheers,

hi sir could u pay small attention on my query
i have x,,y,z values but how to use this code to generate DEM from those xyz values pleas give me a example to use your code

Hi François
Thank you for the code. Where i find the code of the pictures example's?

Dear Tomke,
Thanks for the rating and the tip. But freezecolors uses CData to make RGB colors. So you won't be able to print vectorial PostScript from your figure:
>> print -depsc -painters freeze.eps
Warning: RGB color data not yet supported in Painter's mode
> In /Applications/MATLAB_R2014a.app/toolbox/matlab/graphics/hardcopy.p>hardcopy at 36
In graphics/private/render at 109
In print>LocalPrint at 276
In print at 233
And you get a raster image... Same problem with all previous releases of Matlab. I have already written a similar solution in dem.m, but I am working on a new one compatible with the painters mode.

Hi, thanks for your quick reply. When you install the oackage "freezecolors" (http://www.mathworks.com/matlabcentral/fileexchange/7943-freezecolors---unfreezecolors) than you can plot contourplots over the elevation map.

My code:
dem (lon,lat,topo,...'legend');
freezeColors;
[c,h]=contour(xkoord,ykoord,I,100); %Konturplot
colormap hot;
hc=colorbar('location','Eastoutside','Fontname','FixedWidth'); %Legende
pos=get(hc,'pos')
hc=colorbar('location','Eastoutside','Fontname','FixedWidth',...
'position', [pos(1)*1.1 pos(2)*0.9 pos(3)*0.5 pos(4)],'fontsize',8);
title(hc,'...');
Cheers! Tomke

Dear Tomke,
This possible in principle: the image produced by dem.m is not indexed but RGB, so independant from colormap function. But I forgot to apply this transformation to the colorbar used by 'legend' option. I'll fix this in the next release. By now you can use a different colormap for contour, with colorbar, but without the elevation legend/colorbar.

Hello,
how can I overlay the DEM-plot with a contourplot that has another colorbar/legend?

Super beautiful, thanks a lot for sharing this!

amrit

Fred Massin

This is great to be able (finally!) to export vector shaded image with matlab, thank you!

Mathieu

Exactly what I was looking for. Great Job!

Julia

François,

Looking forward to using this code. For those of us who mask our DEMs with NaNs, it would be great to have an option to set NaN to white instead of black.

Thanks!

Hi Carlo,
To add the colorbar you need to use the dem(...,'scale') option. Type doc dem to get all the available options.
François.

Simon Lind

thank you for the code
please, how can I add a colorbar?

Zhishan

This is a fantastic code - I love this as I do not have to use the surfl command and other mapping toolbox commands to generate my DEM and drape with other images. Thanks a ton !!
Guys - if you are dealing with 10m DEM with huge matrices, forget about mapping toolbox functions to do your draping - use this function - generate the DEM (use the matrices generated e.g. I in this case) and then use the geotiffwrite command to create beautiful tif images and do the rest in photoshop.

Dear Cristopher,
You must download the file, unzip into your Matlab directory, then type "doc dem" at Matlab prompt.
Or, you can clic on dem.m (on the right of the function window under "Highlights from..."): this will display the function's comments including the doc.
François

cristopher

Sorry, François:
I can't find the "doc dem" for sintax, examples and help.
Can you help me

Updates

2.6.0.0

- improves the colormap splitting between land and sea
- new option 'lakezmin' to limit lake detection

2.2.0.0

- new option 'position' for tick labels
- major update thanks to mas Wiwit

2.1.0.0

minor fix

2.0.0.0

legend scale is now liberated from the figure colormap (thanks to Tomke Froechtenich's comment). No use of freezecolors needed to add other colormap plots.

1.10.0.0

improve backward compatibility (strjoijn)

1.9.0.0

- new options: 'km', 'watermark', 'fontsize', 'bordersize'
- improve legend colorbar
- all options now passed as param/value

1.8.0.0

- 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

1.7.0.0

- adds 'interp' option (fill the gaps)
- adds 'seacolor' colormap for negative elevations (bathymetry)

1.6.0.0

- adds a 'lake' option
- minor bug correction

1.5.0.0

now accepts row/column vectors for X and/or Y.

1.4.0.0

- adds basemap-style axis in decimal or lat/lon modes
- adds elevation and distance scales

1.3.0.0

- new landcolor.m colormap function
- new arguments to control colormap scaling
- median-style filters for light and colormap

1.2.0.0

ZIP now contains landcolor.dat companion file.

1.1.0.0

Optimizations: adds a decimation for large DEM grids.

MATLAB Release Compatibility
Created with R2011b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Acknowledgements

Inspired: shadem