Converting DEM rasters into OBJ meshes for use in Blender

This is the technique I use to get Digital Elevation Models (DEMs) into Blender from QGIS. Here's an example using this technique:-

Indian Ocean. Sea was added as an additional, transparent mesh at sea level (elevation 0)


For this, you’ll need

- Blender (I use 2.79)
- QGIS
- Python
- dem2obj (here on Github)
- git

First of all, fork the project in Github. That'll give you a fresh copy to play with (and enhancements/fixes are welcome)

cd /path/to/project
git clone https://www.github.com/your_user_name/dem2obj
pyvenv venv
source ./venv/bin/activate

You should now see the command line prompt change to (venv).

(venv) pip install -r requirements.txt

That might take a while (a few minutes, potentially)

Running dem2obj

When that's done, you should be able to run this. If you're not seeing the (venv) prompt, you'll need to reactivate the virtual environment, as follows:-

cd /path/to/project
source ./venv/bin/activate

You can now run the tool. You need to run AT LEAST --input FILENAME and --output FILENAME.

If your DEM is in degrees (e.g. WGS84) and elevation in meters, pass in the -w (or --wgs84) flags

(venv) python rastertomesh.py --input /path/to/input.tiff --output /tmp/foo.obj

Importing into Blender

Blender supports OBJ file import as a standard, no need for plugins.

In Blender, File > Import > Wavefront (.obj)

You need to make sure your settings look like this. I'm using Y forward, Z up. You can save these settings and recall them later using the [+] button. You'll need to reselect this each time you import a new mesh!

Screen Shot 2017-11-18 at 20.53.54.png

Styling Relief Maps in Blender

There are lots of ways of styling in Blender. I find the traditional way (lighting from the top-left, and viewing straight down with an Orthogonal camera) works really well.

This is the Nodes recipe I use (with Cycles renderer). This applies a relief-map colour ramp to the z-axis to give cartographic-looking results...

an example configuration

Troubleshooting

Out-of-memory (Blender crashes). I find a 4Gb machine can handle 1000x1000 fine; 8Gb RAM is good for 2000x2000. If your raster is too large Blender will crash during loading. You can use QGIS or gdalwarp to resize your raster to something your machine can cope with.

If the tool runs, and Blender imports it but you can't see anything, it may be that the scale means the mesh is too large (clipping means that very large models can disappear). You can pass this in using the --scale VALUE option. The default is 1.0, which means 1 unit of size in the DEM equals 1 Blender unit. You can scale it up/down in Blender, or run it again with this option.

If the model imports but is rotated on its side, check you imported using the suggested settings. Remember, y forward, z up.

Modelling the 2015 York Floods

Modelling the extent of the floods in York caused by Storm Eva over Christmas 2015. This shows the approximate extent of the water, having risen by 4.5 meters.

visualising extent of Christmas 2015 flooding in York

The view is from the south, looking North (that's York Minster in the top left). From looking at photos in the news coverage, it looks fairly accurate, at least from the photos I've been able to locate...

The elevation model was based on the 1m resolution Open LiDAR data for England (released under the OGL), cell SE65.

I used SAGA GIS to model the rise in water levels, and a custom Python GDAL script to generate a mesh in Wavefront OBJ format for import into Blender.

Caveat

I'm not sure exactly how up-to-date this elevation data is... higher resolution tiles have dates (2008), but in this case I couldn't see how current the 1m data is. It's possible that some flood defences might be missing.

An alternative to Perspective in Geovisualisation

One of the problems with perspective is foreshortening - the inability to compare heights at different distances.

This reminds me of one of my favourite Father Ted sketches...

"ok one last time.." (showing Father Dougal some toy cows)
"these are small, but the ones out there are far away."
"small..."
"far away..."

This is why using 3d pie charts are a bad idea - foreshortening distorts the angles, which are difficult enough to compare to begin with.

There are 3 types of 3d.

Perspective : given two objects of the same height, the further away one looks smaller.

Reverse Perspective: given two objects of the same height, the one closer to you looks smaller. This looks really confusing and surreal.

Orthographic: if two objects are the same height, they appear to have the same height irrespective of distance. This happens because the light travels in parallel lines, rather than radiating out from the observer point - you're seeing the view from an infinite distance away.

The advantage of the Orthographic Camera

Blender is capable of rendering using an orthographic camera, and these allow us to compare the heights of mountains. We don't need to concern ourself with how far away something is; this preserving of height at any distance means we can easily compare heights of two separate maps.

This profile comparison compares the heights of the Ben Nevis ridge and the terrain around Edinburgh. The high point in Edinburgh is Arthurs' Seat - to its left is Castle Rock and Corstorphine Hill. Behind it in White is the Ben Nevis ridge, looking from the NW.

 

The image below shows how Blender has been set up (the view is from above). The triangle on the left shows the camera's field of view. The green mesh was taken from around Edinburgh (North to the top, so North is left on the image above). The taller, white mesh is the Ben Nevis ridge.

view from above. Camera type is orthographic. Ben Nevis map was rotated so that its most recognisable aspect was used.

This property is not limited to side-on views, as shown here. It also works when looking at an angle. So if you want to do a 3d rendering of a surface, orthographic view gives equal optical weight across the whole mesh.

Challenge accepted

I got an interesting challenge from Donald Noble via Twitter...

What would Ben Nevis look like if you were standing in Musselburgh, looking towards Arthurs Seat, but using normal perspective?

To answer this question, I had move both meshes so that the camera was positioned in Musselburgh (for the Lothian mesh) and Caol (for the Ben Nevis mesh). The meshes were rotated so that the camera pointed roughly in the direction of the highest point.

I used perspective view, with a FOV of 50mm. I had to make the Ben Nevis mesh slightly transparent for this to work... and here's the result...

by pinning a 50mm camera to Musselburgh / Caol, and using a normal perspective camera. This is what Ben Nevis would look like if it plonked itself on Edinburgh's doorstep, and you were in Musselburgh.

Ben Nevis is about 5 times as high as Arthurs Seat; in perspective, only about twice as high. But interesting to compare!

 

 

Credits

Used OS Open Terrain data, crown copyright and database right 2015.