Tutorials/3.0/gazebo dem


 * 1) Tutorial: Using Digital Elevation Models#

A Digital Elevation Model (DEM) is a 3D representation of a terrain's surface that does not include any objects like buildings or vegetation. DEMs are frequently created by using a combination of sensors, such as LIDAR, radar, or cameras. The terrain elevations for ground positions are sampled at regularly-spaced horizontal intervals. Wikipedia is a good resource for getting more details about DEMs.

The term DEM is just a generic denomination, not a specific format. In fact, the DEMs can be represented as a grid of elevations (raster) or as a vector-based triangular irregular network (TIN). Currently, Gazebo only supports raster data in the supported formats available in GDAL.

The main motivation to support DEMs in Gazebo is to be able to simulate a realistic terrain. Rescue or agriculture applications might be interested in testing their robot behaviors using a simulated terrain that matches the real world.


 * 1) Using DEMs in Gazebo ###


 * 1) Bring DEM support to Gazebo ####

In order to work with DEM files you should install GDAL libraries.

sudo apt-get install gdal-bin libgdal-dev libgdal1 python-gdal
 * On Ubuntu Saucy, Raring or Quantal:**

sudo apt-get install gdal-bin libgdal1-1.7.0 libgdal1-dev python-gdal
 * On Ubuntu Precise:**

Note: gazebo packages are not compiled with support for DEM. You will need to compile gazebo from source using the default branch after have installed the gdal packages (remember to run cmake after the packages installation) to get a version of gazebo with DEM support.


 * 1) DEM file and the definition into SDF format

There are several organizations that provide elevation data. As an example, let's download a DEM file of Mount St. Helens before or after its eruption back in the '80s. Unzip the file and rename it mtsthelens.dem.

unzip ~/Downloads/mtsthelens_after.zip -d /tmp mv /tmp/10.2.1.1043901.dem /tmp/mtsthelens.dem

Usually, DEM files have big resolutions and Gazebo cannot handle it, so it's a good idea to adjust the resolution of your DEM. The next command will scale the terrain to 129x129 and will copy into the Gazebo media/dem/ directory.

mkdir -p /tmp/media/dem/ gdalwarp -ts 129 129 /tmp/mtsthelens.dem /tmp/media/dem/mtsthelens_129.dem

A DEM file in Gazebo is loaded in the same way that you load a heightmap image. Gazebo automatically detects if the file is a plain image or a DEM file. Create the file volcano.world and copy the next content. Save the file anywhere you want, for example, in /tmp.

  model://sun

true file://media/dem/mtsthelens_129.dem 150 150 50              0 0 0

 file://media/materials/textures/dirt_diffusespecular.png file://media/materials/textures/flat_normal.png 1                file://media/materials/textures/grass_diffusespecular.png file://media/materials/textures/flat_normal.png 1                file://media/materials/textures/fungus_diffusespecular.png file://media/materials/textures/flat_normal.png 1                2 5 4 5 file://media/dem/mtsthelens_129.dem 150 150 50              0 0 0

The  element in the code above, tells Gazebo whether to load the DEM with the original dimensions or to scale it. In case you prefer to scale the DEM, the   element tells Gazebo the size in meters that the terrain will have in the simulation. If you want to maintain the correct aspect ratio, be sure to properly calculate the width, height and elevation (which is the third number in  ).   will be ignored if you set  to true. In our example, the DEM will be scaled to a square of 150 x 150 meters and a max elevation of 50 meters.

Launch Gazebo with the world containing your DEM file and you should see the volcano. In our case, the file is in the /tmp directory.

. /usr/share/gazebo/setup.sh GAZEBO_RESOURCE_PATH="$GAZEBO_RESOURCE_PATH:/tmp" gazebo /tmp/volcano.world
 * 1) Be sure of sourcing gazebo setup.sh in your own installation path




 * 1) How do I get a DEM file of my region of interest? ###

Next, we are going to describe one method for obtaining a DEM file of a specific region of interest.

Global Land Cover Facility maintains a high-resolution digital topographic database of Earth. Go to its Search and Preview tool and you will see something similar to the image below. Every terrain patch has a unique path and row that you should know before using the tool. We'll use QGIS to discover the path/row of our region of interest.



QGIS is a cross-platform open source geographic information system program that provides data viewing, editing, and analysis capabilities. Download QGIS following the instructions detailed on the QGIS website.

Open up QGIS, click on the left column icon labeled WMS/WMTS layer, click on Add default servers, select Lizardtech server, and then, press the connect button. Select the MODIS value and press Add. Close the pop-up window. The next step is to add another layer with all the different patches available. Download this shapefile and decompress it in any folder. Go back to QGIS and press Add Vector Layer (left column icon). Press Browse, and select your previously uncompressed wrs2_descending.shp file. Press OK in the window that opens. Now, you'll see both layers on the main window. Let's change the transparency of the wrs2_descending layer to be able to see both layers at the same time. Double click on wrs2_descending layer, and then, modify its transparency value to something around 85%.



Use the scroll and left button to navigate to your region of interest. Then click on the icon labeled Identify Features on the top bar. Click on your region of interest and all the terrain patches around the area will be highlighted. A new pop up window will show the path/row values for each highlighted patch. In the image below you can see the path and row of the DEM patch containing Las Palmas, one of the heavenly places of the Canary Islands, Spain.



Go back to your browser with the GLCF search tool and write the path/row values in the columns labeled Start Path and Start Row. Then click in Submit Query; press Preview and Download to see your results. Choose your terrain file and press Download. Finally, select your file with extension .gz, and decompress it in your favorite folder. Global Land Cover Facility files are in GeoTiff format, one of the most common format of DEM files available.


 * 1) Preparing DEM data for use in Gazebo ###

DEM data is usually created at very high resolution. Use *gdalwarp* to reduce the resolution of the terrain to a more manageable size before using it in Gazebo.

gdalwarp -ts  

DEM data often contain "holes" or "void" areas. These sections correspond to areas where data could not be collected while the DEM was created. In the case of a data "hole", the hole will be assigned the minimum or maximum value of the data type that is used in that DEM.

Always try to download "finished" versions of DEM data sets, where the holes have been filled. If your DEM terrain contains holes (also known as NODATA values), try to manually repair it using gdal tools, such as *gdal_fillnodata.py*.


 * 1) Working with multiple DEMs in Gazebo ###

Although Gazebo does not directly support multiple DEMs, GDAL has a set of utilities for merging a set of DEMs into a single one. The first step is to download the set of DEMs that you want to merge. Note that the patches can even overlap with one another; GDAL will merge them seamlessly. Assuming that your current directory contains a set of Geotiff files ready to be merged, run the next command.

gdal_merge.py *.tif -o dem_merged.tif

Now, you can just use dem_merged.tif in your world file and Gazebo will load the terrain with all the patches merged. In the next screenshot you can see the result of merging four terrain patches surrounding the Canary Islands.