wolfhece.rem.RasterViz

Module Contents

wolfhece.rem.RasterViz.start[source]
wolfhece.rem.RasterViz.usage = Multiline-String[source]
Show Value
"""
Python script for OpenTopography Raster Derivative/Visualization products.
This script can be called from Python using its class/methods or as a CLI utility.

CLI Usage:

"python RasterViz.py viz_type [-z (default=1)] [-alt (default=45)] [-azim (default=315)] [-multidirectional]
    [-cmap (default=terrain)] [-out_ext tif | img (default=tif)] [-make_png] [-make_kmz] [-docker] [-shell]
    /path/to/dem"

Options:

    viz_type: string corresponding to raster product to be produced, one of the following strings:
        ["hillshade", "slope", "aspect", "roughness", "color-relief", "hillshade-color"]

    -z: only if using "hillshade" or "hillshade-color" viz_type,
        factor to scale/exaggerate vertical topographic differences. Default 1 (no rescale).

    -alt: only if using "hillshade" or "hillshade-color" viz_type,
          altitude of light source in degrees [0-90]. Default 45.

    -azim: only if using "hillshade" or "hillshade-color" viz_type,
           azimuth of light source in degrees [0-360]. Default 315.

    -multidirectional: only if using "hillshade" or "hillshade-color" viz_type.
                       Makes multidirectional hillshade, overriding alt and azim args.

    -cmap: only if using "color-relief" viz_type, name of a matplotlib or seaborn colormap. Default "terrain".
           (see https://matplotlib.org/stable/gallery/color/colormap_reference.html)

    -out_ext: the extension/file format to use for geodata outputs (tif or img). Default "tif".

    -make_png: output a png version (EPSG:3857) of the viz_type in addition to the viz raster in source projection.

    -make_kmz: output a kmz version of the viz_type in addition to the viz raster in source projection.

    -docker: run GDAL commands from within the osgeo/gdal docker container. This makes it run slightly slower but will
             be needed to run all features if gdal install is on a version <2.2. If using docker, input path must be
             within the working directory path or a subdirectory.

    -shell: call GDAL functions (gdaldem, gdal_translate, gdalwarp) as shell commands instead of using Python bindings.
            This may be faster than using pure Python but requires additional environment configuration.

    /path/to/dem.tif: path to input DEM, currently assumed to be in GeoTIFF format.

Notes: Output file naming convention uses the DEM basename as a prefix, then viz_type and file extension.
       E.g. output.tin.tif --> output_hillshade.tif, output_hillshade.png, output_hillshade.kmz.
       Outputs are saved to the working directory.

Dependencies:
    - Python >=3.6
    - GDAL >=2.2 (or run with lower version in environment using -docker flag)
"""
wolfhece.rem.RasterViz.print_usage()[source]
class wolfhece.rem.RasterViz.RasterViz(dem, out_dir='./', out_ext='.tif', make_png=False, make_kmz=False, docker_run=False, shell=False, cache_dir='./.cache', *args, **kwargs)[source]

Bases: object

Handler to produce DEM derivatives/visualizations.

Parameters:
  • dem (str) – path to input DEM, either in GeoTIFF (.tif), ASCII (.asc), or IMG (.img) format.

  • out_dir (str) – output file directory. Defaults to current working directory.

  • out_ext (str, '.tif' or '.img') – extension for output georaster files.

  • make_png (bool) – output a png image of visualizations (EPSG:3857) in addition to a raster in source projection.

  • make_kmz (bool) – output a kmz file (e.g. Google Earth) of visualizations in addition to a raster in source projection.

  • docker_run (bool) – only if shell=True as well, calls gdal utilities from shell with docker container. Must have the osgeo/gdal docker container configured locally to use this option.

  • shell (bool) – call gdal utilities from a shell instead of using the Python bindings. May run faster for large files but can be more difficult to configure GDAL environment outside conda.

  • cache_dir (str) – cache directory

property dem[source]
property out_dir[source]
property cache_dir[source]
static valid_input(dem)[source]
_asc_to_tif(asc)[source]

Convert ascii grid to geotiff

_check_dem_nodata()[source]

Check that input DEM has a NoData value set. If not, set to zero.

_png_kmz_checker()[source]

Used as a wrapper for making viz products, making png and kmz after tif if selected

make_hillshade(z=1, alt=45, azim=315, multidirectional=False, *args, **kwargs)[source]

Make hillshade raster from the input DEM.

Parameters:
  • z (float >1) – z factor for exaggerating vertical scale differences (default 1).

  • alt (float [0-90]) – altitude of light source in degrees (default 45).

  • azim (float [0-360]) – azimuth for light source in degrees (default 315).

  • multidirectional (bool) – makes multidirectional hillshade if True, overriding alt and azim.

Returns:

path to output hillshade raster.

Return type:

str

make_slope(*args, **kwargs)[source]

Make slope map from DEM, with slope at each pixel in degrees [0-90].

Returns:

path to output slope raster.

Return type:

str

make_aspect(*args, **kwargs)[source]

Make aspect map from DEM, with aspect at each pixel in degrees [0-360].

Returns:

path to output aspect raster.

Return type:

str

make_roughness(*args, **kwargs)[source]

Make roughness map from DEM.

Returns:

path to output roughness raster.

Return type:

str

make_color_relief(cmap='terrain', log_scale=False, *args, **kwargs)[source]

Make color relief map from DEM (3 band RGB raster).

Parameters:
  • cmap (str) – matplotlib or seaborn named colormap to use for making color relief map. (see https://matplotlib.org/stable/gallery/color/colormap_reference.html)

  • log_scale (bool) – bool, makes the colormap on a log scale from zero, so terrain closer to 0 elevation has greater color variation. Intended to be used for REMs or coastal datasets.

Returns:

path to output color-relief raster.

Return type:

str

make_hillshade_color(blend_percent=60, *args, **kwargs)[source]

Make a pretty composite hillshade/color-relief image.

Parameters:

blend_percent (float [0-100]) – Percent weight of hillshdae in blend, color-relief takes opposite weight.

This method also accepts all arguments of make_hillshade and make_color_relief if the respective rasters have not yet been created.

Returns:

path to output hillshade-color raster.

Return type:

str

blend_images(blend_percent=60)[source]

Blend hillshade and color-relief rasters by linearly interpolating RGB values

Parameters:

blend_percent – Percent weight of hillshdae in blend, color-relief takes opposite weight [0-100]. Default 60.

get_cmap_txt(cmap='terrain', log_scale=False)[source]

Make a matplotlib named colormap into a gdaldem colormap text file for color-relief mapping. Format is “elevation R G B” where RGB are in [0-255] range.

Parameters:
  • cmap – colormap to use for making color relief map.

  • log_scale – bool, logarithmically scale colormap, showing greatest color variation at zero elevation. Intended to be used for REMs or coastal datasets.

Returns:

.txt file containing colormap mapped to DEM

get_projection()[source]

Get EPSG code for DEM raster projection.

get_elev_range()[source]

Get range (min, max) of DEM elevation values.

tile_and_compress(in_path, out_path)[source]

Used to turn intermediate raster viz products into final outputs.

raster_to_png(ras_path)[source]

Convert raster to .png file. Coerce to EPSG:3857 consistent with existing OT service.

raster_to_kmz(ras_path)[source]

Convert .tif raster to .kmz file

static get_scaling(ras_name)[source]

Get scaling string for gdal_translate call when converting to Byte array for png/kmz outputs

_clean_up()[source]

Delete all intermediate files. Called by _png_kmz_checker decorator at end of function calls.

wolfhece.rem.RasterViz.argv[source]