Creating the hillshade

We can run this data through the same hillshade algorithm used in the Creating a shaded relief section, in Chapter 7, Python and Elevation Data:

# Hillshade the elevation image
log.info("Hillshading elevation data")
im = Image.open(elv_img + ".jpg").convert("L")
dem = np.asarray(im)

# Set up structure for a 3x3 window to
# process the slope throughout the grid
window = []
# x, y resolutions
xres = (maxx-minx)/w
yres = (maxy-miny)/h

# Create the windows
for row in range(3):
    for col in range(3):
        window.append(dem[row:(row + dem.shape[0]-2),
                      col:(col + dem.shape[1]-2)])

# Process each cell
x = ((z * window[0] + z * window[3] + z * window[3] + z * window[6]) -
     (z * window[2] + z * window[5] + z * window[5] + z * window[8])) 
     / (8.0 * xres * scale)

y = ((z * window[6] + z * window[7] + z * window[7] + z * window[8]) -
     (z * window[0] + z * window[1] + z * window[1] + z * window[2])) 
     / (8.0 * yres * scale)

# Calculate slope

slope = 90.0 - np.arctan(np.sqrt(x*x + y*y)) * rad2deg

# Calculate aspect
aspect = np.arctan2(x, y)

# Calculate the shaded relief
shaded = np.sin(altitude * deg2rad) * np.sin(slope * deg2rad) 
       + np.cos(altitude * deg2rad) * np.cos(slope * deg2rad) 
       * np.cos((azimuth - 90.0) * deg2rad - aspect)

shaded = shaded * 255
..................Content has been hidden....................

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