Skip to content

Instantly share code, notes, and snippets.

@cquest
Last active July 5, 2022 06:30
Show Gist options
  • Save cquest/8179870 to your computer and use it in GitHub Desktop.
Save cquest/8179870 to your computer and use it in GitHub Desktop.
transparent hillshade cooking guide using gdal

Ingredients

  • a decent DEM
  • gdal
  • some computer

compute grayscale hillshade using gdaldem

gdaldem hillshade -compute_edges your_dem_file hillshade.tif

color-ramp to compute transparency (alpha channel)

In another bowl, prepare the following shade.ramp file:

  • first value is the hillshade grey level, 220 is the value for flat areas
  • 2nd, 3rd, 4th value are the output RGB components (0 0 0 = black)
  • last value is the alpha channel: 255 = opaque, 0 = transparent
0 0 0 0 255
32 0 0 0 240
64 0 0 0 180
96 0 0 0 120
220 0 0 0 0
221 255 255 255 0
255 255 255 255 192

other option (smoother shades)

0 0 0 0 255
220 0 0 0 0
221 255 255 255 0
255 255 255 255 192

and third one if you want only shades

0 0 0 0 255
220 0 0 0 0
255 0 0 0 0

stir together using gdaldem color-relief

gdaldem color-relief hillshade.tif -alpha shade.ramp hillshade-overlay.tif

DEM smooth/blur using .vrt

Here is a .vrt example applying a gaussian blur on the source (srtm.vrt) The resulting .vrt can be use to compute the hillshade instead of the original DEM file. It is also possible to smooth/blur the hillshade output instead of the DEM.

<VRTDataset rasterXSize="10801" rasterYSize="10801">
  <SRS>GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433],AUTHORITY["EPSG","4326"]]</SRS>
  <GeoTransform>  9.9986111111111109e-01,  2.7777777777777778e-04,  0.0000000000000000e+00,  5.0000138888888891e+01,  0.0000000000000000e+00, -2.7777777777777778e-04</GeoTransform>
  <VRTRasterBand dataType="Int16" band="1">
    <NoDataValue>-32768</NoDataValue>
    <ColorInterp>Gray</ColorInterp>
<KernelFilteredSource>
  <SourceFilename relativeToVRT="1">srtm.vrt</SourceFilename>
  <SourceBand>1</SourceBand>
      <SourceProperties RasterXSize="10801" RasterYSize="10801" DataType="Int16" BlockXSize="128" BlockYSize="128" />
      <SrcRect xOff="0" yOff="0" xSize="10801" ySize="10801" />
      <DstRect xOff="0" yOff="0" xSize="10801" ySize="10801" />
      <NODATA>-32768</NODATA>
  <Kernel normalized="1">
    <Size>9</Size>
    <Coefs>0 0.000001 0.000014 0.000055 0.000088 0.000055 0.000014 0.000001 0 0.000001 0.000036 0.000362 0.001445 0.002289 0.001445 0.000362 0.000036 0.000001 0.000014 0.000362 0.003672 0.014648 0.023205 0.014648 0.003672 0.000362 0.000014 0.000055 0.001445 0.014648 0.058434 0.092566 0.058434 0.014648 0.001445 0.000055 0.000088 0.002289 0.023205 0.092566 0.146634 0.092566 0.023205 0.002289 0.000088 0.000055 0.001445 0.014648 0.058434 0.092566 0.058434 0.014648 0.001445 0.000055 0.000014 0.000362 0.003672 0.014648 0.023205 0.014648 0.003672 0.000362 0.000014 0.000001 0.000036 0.000362 0.001445 0.002289 0.001445 0.000362 0.000036 0.000001 0 0.000001 0.000014 0.000055 0.000088 0.000055 0.000014 0.000001 0</Coefs>
  </Kernel>
</KernelFilteredSource>
  </VRTRasterBand>
</VRTDataset>
@StyXman
Copy link

StyXman commented Aug 19, 2019

It would be awesome if you could include some output examples. Thanks for the info!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment