# Wolfram Language™

Live Version Coming Soon Check out other examples

## Make a 3D Image from an Elevation Map

From an array of elevation values, create a 3D image of terrain.

### code

ElevationMapToImage3D[emap_] := Block[{}, data = ImageData[emap]; data3D = Map[# Table[UnitStep[50 # - k], {k, 50}] &, data, {-1}]; transposedData = Reverse[Transpose[data3D, {2, 3, 1}], {1}]; Image3D[transposedData, ColorFunction -> "AlpineColorsOpacity"] ]

### how it works

Heres a simple 2D elevation map:

You can see what that data looks like as a contour map using ListContourPlot:

ListContourPlot[ImageData[ImageReflect[emap]], Contours -> 50]

Heres the same data in three dimensions:

ListPlot3D[ImageData[ImageReflect[emap, Top -> Bottom]], MeshFunctions -> {#3 &}, Mesh -> 50]

To turn the elevation map data into a 3D image, extract the 2D data array from the map image:

data = ImageData[emap];

Fill the column of voxels above each pixel with the pixel value to a height proportional to the value:

data3D = Map[# Table[UnitStep[50 # - k], {k, 50}] &, data, {-1}];

Rearrange the 3D data so that its x, y, and z axes correspond to the original elevation map:

transposedData = Reverse[Transpose[data3D, {2, 3, 1}], {1}];

Render the 3D image using geographic colors:

Image3D[transposedData, ColorFunction -> "AlpineColorsOpacity"]

Package those steps as a function that can be easily applied to other elevation maps:

ElevationMapToImage3D[emap_] := Block[{}, data = ImageData[emap]; data3D = Map[# Table[UnitStep[50 # - k], {k, 50}] &, data, {-1}]; transposedData = Reverse[Transpose[data3D, {2, 3, 1}], {1}]; Image3D[transposedData, ColorFunction -> "AlpineColorsOpacity"] ]

Test the function on elevation data from Denver, Colorado: