| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Mapping Toolbox |
| Contents | Index |
| Learn more about Mapping Toolbox |
| On this page… |
|---|
Lighting a Terrain Map Constructed from a DTED File |
The lightm function creates light objects in the current map. To modify the positions and colors of lights created on world maps or large regions you can use the interactive lightmui GUI. For finer control over light position (for example, in small areas lit by several lights), you have to specify light positions using projected coordinates. This is because lights are children of axes and share their coordinate space. See Lighting a Global Terrain Map with lightm and lightmui for an example of using lightmui.
In this exercise, you manually specify the position of a single light in the northwest corner of a DTED DEM for Cape Cod.
To illustrate lighting terrain maps, begin by following the exercise in Mapping a Single DTED File with the DTED Function, or execute the steps below:
latlim = [ 41.20 41.95];
lonlim = [-70.95 -70.10];
cd dted\w071 %Note: Your absolute path may vary
samplefactor = 1;
[capeterrain, caperef] = dted('n41.dt0', samplefactor,...
latlim, lonlim);
capeterrain(capeterrain == 0) = -1;
capecoast = shaperead('usastatehi',...
'UseGeoCoords', true,...
'BoundingBox', [lonlim' latlim']);
Construct a map of the region within the specified latitude and longitude limits:
figure ax = usamap(latlim,lonlim); geoshow(ax, capecoast, 'FaceColor', 'none'); geoshow(ax, capeterrain, caperef, 'DisplayType', 'mesh'); demcmap(capeterrain)
The map looks like this.

Set the vertical exaggeration. Use daspectm to specify that elevations are in meters and should be multiplied by 20:
daspectm('m',20)
Make sure that the line data is visible. To ensure that it is not obscured by terrain, use zdatam to set it to the highest elevation of the cape1 terrain data:
zdatam('allline',max(capeterrain(:)))
Specify a location for a light source with lightm:
h = lightm(42,-71);
If you omit arguments, a GUI for setting positional properties for the new light opens.
To see the properties of light objects, inspect the handle returned by lightm:
get(h) Position = [-0.00616097 0.796039 1] Color = [1 1 1] Style = infinite BeingDeleted = off ButtonDownFcn = Children = [] Clipping = on CreateFcn = DeleteFcn = BusyAction = queue HandleVisibility = on HitTest = on Interruptible = on Parent = [138.001] Selected = off SelectionHighlight = on Tag = Type = light UIContextMenu = [] UserData = [ (1 by 1) struct array] Visible = on
Had you used the MATLAB light function in place of lightm, you would have needed to specify the position in Cartesian 3-space.
The lighting computations caused the map to become quite dark with specular highlights. Now restore its luminance by specifying three surface reflectivity properties in the range of 0 to 1:
ambient = 0.7; diffuse = 1; specular = 0.6; material([ambient diffuse specular])
The surface looks blotchy because there is no interpolation of the lighting component (flat facets are being modeled). Correct this by specifying Phong shading:
lighting phong
The map now looks like this.

If you want to compare the lit map with the unlit version, you can toggle the lighting off:
lighting none
For additional information, see the reference pages for daspectm, lightm, light, lighting, and material.
In this example, you create a global topographic map and add a local light at a distance of 250 km above New York City, (40.75 °N, 73.9 °W). You then change the material and lighting properties, add a second light source, and activate the lightmui tool to change light position, altitude, and colors.
The lightmui display plots lights as circular markers whose facecolor indicates the light color. To change the position of a light, click and drag the circular marker. Alternatively, right-clicking the circular marker summons a dialog box for changing the position or color of the light object. Clicking the color bar in that dialog box invokes the uisetcolor dialog box that can be used to specify or pick a color for the light.
Load the topo DTM files, and set up an orthographic projection:
load topo
axesm ('mapprojection','ortho', 'origin',[10 -20 0])Plot the topography and assign a topographic colormap:
meshm(topo,topolegend); demcmap(topo)
Set up a yellow light source over New York City:
hl = lightm(40.75,-73.9, 500/almanac('earth','radius'),...
'color','yellow', 'style', 'local');The first two arguments to lightm are the latitude and longitude of the light source. The third argument is its altitude, in units of Earth radii (in this case they are in kilometers, the default units of almanac).
The surface is quite dark, so give it more reflectivity by specifying
material([0.7270 1.5353 1.9860 4.0000 0.9925]); lighting phong; hidem(gca)
The lighted orthographic map looks like this.

If you want, add more lights, as follows:
h2 = lightm(20,40, .1,'color','magenta', 'style', 'local')
The second light is magenta, and positioned over the Gulf of Arabia.
To modify the lights, use the lightmui GUI, which lets you drag lights across a world map and specify their color and altitudes:
lightmui(gca)
The lights are shown as appropriately colored circles, which you can drag to new positions. You can also Ctrl+click a circle to bring up a dialog box for directly specifying that light's position, altitude, and color. The GUI and the map look like this at this point.

In the lightmui window, drag the yellow light to the eastern tip of Brazil, and drag the magenta light to the Straits of Gibraltar.

Ctrl+click or Shift+click the magenta circle in the lightmui window. A second UI, for setting light position and color, opens. Set the altitude to 0.04 (Earth radii). Set the light color components to 1.0 (red), 0.75 (green), and 1.0 (blue). Press Return after each action. The colorbar on the UI changes to indicate the color you set. If you prefer to pick a color, click on the colorbar to bring up a color-choosing UI. The map now looks like this.

For additional information, see the reference pages for lightm and lightmui.
You can make dimensional monochrome shaded-relief maps with the function surflm, which is analogous to the MATLAB surfl function. The effect of surflm is similar to using lights, but the function models illumination itself (with one "light source" that you specify when you invoke it, but cannot reposition) by weighting surface normals rather than using light objects.
Shaded relief maps of this type are usually portrayed two-dimensionally rather than as perspective displays. The surflm function works with any projection except globe.
The surflm function accepts geolocated data grids only. Recall, however, that regular data grids are a subset of geolocated data grids, to which they can be converted using meshgrat (see Fitting Gridded Data to the Graticule). The following example illustrates this procedure.
As stated above, surflm simulates a single light source instead of inserting light objects in a figure. Conduct the following exercise with the korea data set to see how surflm behaves. It uses worldmap to set up an appropriate map axes and reference outlines.
Set up a projection and display a vector map of the Korean peninsula with worldmap:
figure;
ax = worldmap('korea');
latlim = getm(ax,'MapLatLimit');
lonlim = getm(ax,'MapLonLimit');
coastline = shaperead('landareas',...
'UseGeoCoords', true,...
'BoundingBox', [lonlim' latlim']);
geoshow(ax, coastline, 'FaceColor', 'none');worldmap chooses a projection and map bounds to make this map.

load korea
Generate the grid of latitudes and longitudes to transform the regular data grid to a geolocated one:
[klat,klon] = meshgrat(map,refvec);
Use surflm to generate a default shaded relief map, and change the colormap to a monochromatic scale, such as gray, bone, or copper.
ht = surflm(klat,klon,map);
colormap('copper')In this default case, the lighting direction is set at 45º counterclockwise from the viewing direction; thus the "sun" is in the southeast. This map is shown below.

To make the light come from some other direction, specify the light source's azimuth and elevation as the fourth argument to surflm. Clear the terrain map and redraw it, specifying an azimuth of 135º (northeast) and an elevation of 60º above the horizon:
clmo(ht); ht=surflm(klat,klon,map,[135,60]);
The surface lightens and has a new character because it is lit closer to overhead and from a different direction.

Now shift the light to the northwest (-135º azimuth), and lower it to 40º above the horizon. Because a lower "sun" decreases the overall reflectance when viewed from straight above, also specify a more reflective surface as a fifth argument to surflm. This is a 1-by-4 vector describing relative contributions of ambient light, diffuse reflection, specular reflection, and a specular shine coefficient. It defaults to [.55 .6 .4 10].
clmo(ht); ht=surflm(klat,klon,map,[-135, 30],[.65 .4 .3 10]);
This is a good choice for lighting this terrain, because of the predominance of mountain ridges that run from northeast to southwest, more or less perpendicular to the direction of illumination. Here is the final map.

For further information, see the reference pages for surflm and surfl.
Shaded relief representations can highlight the fine structure of the land and sea floor, but because of the monochromatic coloration, it is difficult to distinguish land from sea. The next section describes how to color such maps to set off land from water.
The functions meshlsrm and surflsrm display maps as shaded relief with surface coloring as well as light source shading. You can think of them as extensions to surflm that combine surface coloring and surface light shading. Use meshlsrm to display regular data grids and surflsrm to render geolocated data grids.
These two functions construct a new colormap and associated CData matrix that uses grayscales to lighten or darken a matrix component based on its calculated surface normal to a light source. While there are no analogous MATLAB display functions that work like this, you can obtain similar results using MATLAB light objects, as Relief Mapping with Light Objects explains.
In this exercise, you use surflsrm in a way similar to how you used surflm in the preceding exercise, Creating Monochrome Shaded Relief Maps Using surflm. In addition, you set a vertical scale and view the map from various perspectives.
Start with a new map axes and the korea data, and then georeference the regular data grid:
load korea [klat,klon] = meshgrat(map,refvec); axesm miller
Create a colormap for DEM data; it is transformed by surflsm to shade relief according to how you specify the sun's altitude and azimuth:
[cmap,clim] = demcmap(map);
Plot the colored shaded relief map, specifying an azimuth of -135º and an altitude of 50º for the light source:
surflsrm(klat,klon,map,[-130 50],cmap,clim)
You can also achieve the same effect with meshlsrm, which operates on regular data grids (it first calls meshgrat, just as you just did), e.g., meshlsrm(map,maplegend).
The surface has more contrast than if it were not shaded, and it might help to lighten it uniformly by 25% or so:
brighten(.25)
The map, which has an overhead view, looks like this.

Plot an oblique view of the surface by hiding its bounding box, exaggerating terrain relief by a factor of 50, and setting the view azimuth to -30º (south-southwest) and view altitude to 30º above the horizon:
set(gca,'Box','off')
daspectm('meters',50)
view(-30,30)The map now looks like this.

You can continue to rotate the perspective with the view function (or interactively with the Rotate 3D tool in the figure window), and to change the vertical exaggeration with the daspectm function. You cannot change the built-in lighting direction without generating a new view using surflsrm.
For further information, see the reference pages for surflsrm, meshlsrm, daspectm, and view.
In the exercise Lighting a Global Terrain Map with lightm and lightmui, you created light objects to illuminate a Globe display. In the following one, you create a light object to mimic the map produced in the previous exercise (Coloring Shaded Relief Maps and Viewing Them in 3-D), which uses shaded relief computations rather than light objects.
The meshlsrm and surflsrm functions simulate lighting by modifying the colormap with bands of light and dark. The map matrix is then converted to indices for the new "shaded" colormap based on calculated surface normals. Using light objects allows for a wide range of lighting effects. The toolbox manages light objects with the lightm function, which depends upon the MATLAB light function. Lights are separate MATLAB graphic objects, each with its own object handle.
As a comparison to the lighted shaded relief example shown earlier, add a light source to the surface colored data grid of the Korean peninsula region:
If you need to, load the korea DEM, and create a map axes using the Miller projection:
load korea
figure; axesm('MapProjection','miller',...
'MapLatLimit',[30 45],'MapLonLimit',[115 135])Display the DEM with meshm, and color it with terrain hues:
meshm(map,refvec,size(map),map); demcmap(map)
The map, without lighting effects, looks like this.

Create a light object with lightm (similar to the MATLAB light function, but specifies position with latitude and longitude rather than x, y, z). The light is placed at the northwest corner of the grid, one degree high:
h=lightm(45,115,1)
The figure becomes darker.
To see any relief in perspective, it is necessary to exaggerate the vertical dimension. Use a factor of 50 for this:
daspectm('meters',50)The figure becomes darker still, with highlights at peaks.
Set the ambient (direct), diffuse (skylight), and specular (highlight) surface reflectivity characteristics, respectively:
material ([.7, .9, .8])
By default, the lighting is flat (plane facets). Change this to Phong shading (interpolated normal vectors at facet corners):
lighting phong
The map now looks like this.

Finally, remove the edges of the bounding box and set a viewpoint of -30º azimuth, 30º altitude:
set(gca,'Box','off') view(-30,30)
The view from (-30,30) with one light at (45,115,1) and Phong shading is shown below. Compare it to the final map in the previous exercise, Coloring Shaded Relief Maps and Viewing Them in 3-D.

To remove a light (when there is only one) from the current figure, type
clmo(handlem('light'))For more information, consult the reference pages for lightm, daspectm, material, lighting, and view, along with the section on Lighting as a Visualization Tool in the 3–D Visualization documentation.
![]() | Determining and Visualizing Visibility Across Terrain | Draping Data on Elevation Maps | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |