Skip to content

Instantly share code, notes, and snippets.

@emlys
Last active October 13, 2021 19:38
Show Gist options
  • Save emlys/e77b28b68ed5e8ee9dcc1edb93b9365d to your computer and use it in GitHub Desktop.
Save emlys/e77b28b68ed5e8ee9dcc1edb93b9365d to your computer and use it in GitHub Desktop.
Using pygeoprocessing to reclassify raster values

Using pygeoprocessing to reclassify raster values

Here are some examples of how to use pygeoprocessing for reclassification. Note: tested with pygeoprocessing 2.3.2.

The reclassify_raster function

pygeoprocessing provides the reclassify_raster function which can handle basic cases. See the docstring for details.

Convert one valid value to another valid value

import pygeoprocessing
import numpy

##### EDIT THIS #################################
# replace these with your own paths

input_raster_path = '/path/to/your/input/raster.tif'
output_raster_path = '/path/to/your/output/raster.tif'
#################################################

raster_info = pygeoprocessing.get_raster_info(input_raster_path)
nodata = raster_info['nodata'][0]
unique_values = numpy.unique(pygeoprocessing.raster_to_numpy_array(input_raster_path))
value_map = {value: value for value in unique_values}

##### EDIT THIS #################################
# edit the value_map here to set your own value(s) to reclassify
# in this example, all pixels with value 2 will be reclassified to 4

value_map[2] = 4
#################################################

pygeoprocessing.reclassify_raster(
  (input_raster_path, 1),   # base raster (path, band) tuple
  value_map,
  output_raster_path,       # save the results to your desired output raster path
  raster_info['datatype'],  # give the output raster the same data type as the input raster has
  nodata                    # give the output raster the same nodata value as the input raster has
)

Convert nodata to a valid value

import pygeoprocessing
import numpy

##### EDIT THIS #################################
# replace these with your own paths

input_raster_path = '/path/to/your/input/raster.tif'
output_raster_path = '/path/to/your/output/raster.tif'
#################################################

raster_info = pygeoprocessing.get_raster_info(input_raster_path)
nodata = raster_info['nodata'][0]
unique_values = numpy.unique(pygeoprocessing.raster_to_numpy_array(input_raster_path))
value_map = {value: value for value in unique_values}

##### EDIT THIS #################################
# here we set the value(s) to reclassify
# in this example, all nodata pixels will be reclassified to 0
# replace 0 with the number that you want to reclassify to

value_map[nodata] = 0
#################################################

pygeoprocessing.reclassify_raster(
  (input_raster_path, 1),   # base raster (path, band) tuple
  value_map,
  output_raster_path,       # save the results to your desired output raster path
  raster_info['datatype'],  # give the output raster the same data type as the input raster has
  nodata                    # give the output raster the same nodata value as the input raster has
)

Convert a valid value to nodata

import pygeoprocessing
import numpy

##### EDIT THIS #################################
# replace these with your own paths

input_raster_path = '/path/to/your/input/raster.tif'
output_raster_path = '/path/to/your/output/raster.tif'
#################################################

raster_info = pygeoprocessing.get_raster_info(input_raster_path)
nodata = raster_info['nodata'][0]
unique_values = numpy.unique(pygeoprocessing.raster_to_numpy_array(input_raster_path))
value_map = {value: value for value in unique_values}

##### EDIT THIS #################################
# here we set the value(s) to reclassify
# in this example, all pixels with value 0 will be reclassified to nodata
# replace 0 with the number that you want to reclassify

value_map[0] = nodata
#################################################

pygeoprocessing.reclassify_raster(
  (input_raster_path, 1),   # base raster (path, band) tuple
  value_map,
  output_raster_path,       # save the results to your desired output raster path
  raster_info['datatype'],  # give the output raster the same data type as the input raster has
  nodata                    # give the output raster the same nodata value as the input raster has
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment