Skip to content

Commit

Permalink
Merge pull request #422 from phargogh/bugfix/421-helpful-error-in-war…
Browse files Browse the repository at this point in the history
…p-raster-for-nonstring-rasters

Adding type checking for base, target path inputs to `warp_raster`
  • Loading branch information
claire-simpson authored Jan 8, 2025
2 parents a9fe8df + fa012a5 commit 4814279
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
3 changes: 3 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ Release History
https://github.com/natcap/pygeoprocessing/issues/415
* Added validation to ``reclassify_raster`` to raise a ``TypeError`` with a
descriptive message if ``value_map`` contains non-numeric keys.
* If either ``base_raster_path`` or ``target_raster_path`` are not strings, a
``ValueError`` is now raised with a more helpful error message.
https://github.com/natcap/pygeoprocessing/issues/421

2.4.6 (2024-10-15)
------------------
Expand Down
8 changes: 7 additions & 1 deletion src/pygeoprocessing/geoprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2507,8 +2507,14 @@ def warp_raster(
if ``mask_options`` is not None but the
``mask_vector_path`` is undefined or doesn't point to a valid
file.
ValueError
if either ``base_raster_path`` or ``target_raster_path`` are
not strings.
"""
for path_key in ['base_raster_path', 'target_raster_path']:
if not isinstance(locals()[path_key], str):
raise ValueError('%s must be a string', path_key)

_assert_is_valid_pixel_size(target_pixel_size)

base_raster_info = get_raster_info(base_raster_path)
Expand Down
26 changes: 25 additions & 1 deletion tests/test_geoprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def test_reclassify_raster_nonnumeric_key(self):
target_path = os.path.join(self.workspace_dir, 'target.tif')
_array_to_raster(
pixel_matrix, target_nodata, raster_path)

value_map = {1: 2, None: 3, "s": 4, numpy.nan: 5, numpy.float32(99): 6}
with self.assertRaises(TypeError) as e:
pygeoprocessing.reclassify_raster(
Expand Down Expand Up @@ -1754,6 +1754,30 @@ def test_warp_raster(self):
pygeoprocessing.raster_to_numpy_array(
target_raster_path)).all())

def test_warp_raster_invalid_paths(self):
"""PGP.geoprocessing: error on invalid raster paths."""
pixel_a_matrix = numpy.ones((5, 5), numpy.int16)
target_nodata = -1
base_a_path = os.path.join(self.workspace_dir, 'base_a.tif')
_array_to_raster(
pixel_a_matrix, target_nodata, base_a_path)

target_raster_path = os.path.join(self.workspace_dir, 'target_a.tif')
base_a_raster_info = pygeoprocessing.get_raster_info(base_a_path)

for invalid_source_path in [(base_a_path,), (base_a_path, 1)]:
with self.assertRaises(ValueError):
pygeoprocessing.warp_raster(
invalid_source_path, base_a_raster_info['pixel_size'],
target_raster_path, 'near', n_threads=1)

for invalid_target_path in [(target_raster_path,),
(target_raster_path, 1)]:
with self.assertRaises(ValueError):
pygeoprocessing.warp_raster(
base_a_path, base_a_raster_info['pixel_size'],
invalid_target_path, 'near', n_threads=1)

def test_warp_raster_overview_level(self):
"""PGP.geoprocessing: warp raster overview test."""
# This array is big enough that build_overviews will render several
Expand Down

0 comments on commit 4814279

Please sign in to comment.