Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save phargogh/435db78e062bcb53baca5cdd8929e4bd to your computer and use it in GitHub Desktop.
Save phargogh/435db78e062bcb53baca5cdd8929e4bd to your computer and use it in GitHub Desktop.
Pygeoprocessing raster_calculator call to convert from a TauDEM D8 flow direction to pygeoprocessing D8 flow direction.
import numpy
import pygeoprocessing
from osgeo import gdal
def convert_taudem_flow_dir_to_pygeoprocessing(
taudem_flow_dir_raster_path, target_flow_dir_raster_path):
source_nodata = pygeoprocessing.get_raster_info(
taudem_flow_dir_raster_path)['nodata'][0]
dest_nodata = 128 # Matches pygeoprocessing-produced flow dir nodata.
def _convert(taudem_flow_dir_array):
dest_array = numpy.full(taudem_flow_dir_array.shape, dest_nodata,
dtype=numpy.uint8)
valid_pixels = (taudem_flow_dir_array != source_nodata)
dest_array[valid_pixels] = taudem_flow_dir_array[valid_pixels] - 1
return dest_array
pygeoprocessing.raster_calculator(
[(taudem_flow_dir_raster_path, 1)], _convert,
target_flow_dir_raster_path, gdal.GDT_Byte, dest_nodata)
if __name__ == '__main__':
convert_taudem_flow_dir_to_pygeoprocessing(
'my_taudem_d8_flow_dir_raster.tif',
'pygeoprocessing_d8_flow_dir.tif')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment