Creating a color hillshade

In this example, we'll combine the previous techniques in order to combine our terrain hillshade from Chapter 7, Python and Elevation Data, with the color classification that we used on the LIDAR. For this example, we'll need the ASCII Grid DEMs named dem.asc and relief.asc that we used in the previous chapter. We'll create a colorized DEM and a hillshade and then use PIL in order to blend them together for an enhanced elevation visualization. The code comments will guide you through the example as many of these steps are already familiar to you:

import gdal_array as gd
try:
    import Image
except:
    from PIL import Image

relief = "relief.asc"
dem = "dem.asc"
target = "hillshade.tif"

# Load the relief as the background image
bg = gd.numpy.loadtxt(relief, skiprows=6)

# Load the DEM into a numpy array as the foreground image
fg = gd.numpy.loadtxt(dem, skiprows=6)[:-2, :-2]

# Create a blank 3-band image to colorize the DEM
rgb = gd.numpy.zeros((3, len(fg), len(fg[0])), gd.numpy.uint8)

# Class list with DEM upper elevation range values.
classes = [356, 649, 942, 1235, 1528,
           1821, 2114, 2300, 2700]

# Color look-up table (lut)
# The lut must match the number of classes.
# Specified as R, G, B tuples
lut = [[63, 159, 152], [96, 235, 155], [100, 246, 174],
       [248, 251, 155], [246, 190, 39], [242, 155, 39],
       [165, 84, 26], [236, 119, 83], [203, 203, 203]]

# Starting elevation value of the first class
start = 1

# Process all classes.
for i in range(len(classes)):
    mask = gd.numpy.logical_and(start <= fg,
                                fg <= classes[i])
    for j in range(len(lut[i])):
        rgb[j] = gd.numpy.choose(mask, (rgb[j], lut[i][j]))
    start = classes[i]+1

# Convert the shaded relief to a PIL image
im1 = Image.fromarray(bg).convert('RGB')

# Convert the colorized DEM to a PIL image.
# We must transpose it from the Numpy row, col order
# to the PIL col, row order (width, height).
im2 = Image.fromarray(rgb.transpose(1, 2, 0)).convert('RGB')

# Blend the two images with a 40% alpha
hillshade = Image.blend(im1, im2, .4)

# Save the hillshade
hillshade.save(target)

The following image shows the output that makes a great backdrop for GIS maps:

Creating a color hillshade
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset