Skip to content

Commit

Permalink
Handling numpy values explicitly as nodata values.
Browse files Browse the repository at this point in the history
GDAL only wants python ints/floats, not numpy ints/floats. RE:natcap#396
  • Loading branch information
phargogh committed Jun 18, 2024
1 parent 15eae09 commit b2d52eb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/pygeoprocessing/geoprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4239,6 +4239,12 @@ def numpy_array_to_raster(
"Origin and pixel size must both be defined or both be None")
new_band = new_raster.GetRasterBand(1)
if target_nodata is not None:
if numpy.issubdtype(type(target_nodata), numpy.floating):
target_nodata = float(target_nodata)
elif numpy.issubdtype(type(target_nodata), numpy.integer):
target_nodata = int(target_nodata)
# Explicitly leaving off an else clause in case there's an edge case we
# don't know about. If so, we should wait for GDAL to raise an error.
new_band.SetNoDataValue(target_nodata)
new_band.WriteArray(base_array)
new_band = None
Expand Down
4 changes: 2 additions & 2 deletions tests/test_geoprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,8 @@ def test_reclassify_raster_reclass_max_nodata(self):
test_value = 0.5
pixel_matrix[:] = test_value
nodata = numpy.finfo(numpy.float32).max - 1
pixel_matrix[0,0] = nodata
pixel_matrix[5,7] = nodata
pixel_matrix[0, 0] = nodata
pixel_matrix[5, 7] = nodata
raster_path = os.path.join(self.workspace_dir, 'raster.tif')
target_path = os.path.join(self.workspace_dir, 'target.tif')
_array_to_raster(
Expand Down

0 comments on commit b2d52eb

Please sign in to comment.