Here are some examples of how to use pygeoprocessing
for reclassification. Note: tested with pygeoprocessing 2.3.2.
pygeoprocessing
provides the reclassify_raster
function which can handle basic cases. See the docstring for details.
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
)
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
)
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
)