Skip to content

Commit

Permalink
Merge branch 'main' into bugfix/1635-pollination-inf-values-in-outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
phargogh authored Oct 8, 2024
2 parents 97465db + 3a4a4c7 commit 908b34c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
4 changes: 4 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ Unreleased Changes
updated the stated dtype of most pollination model outputs to be float32
instead of the float64 dtype that was being assumed previously. This
will result in smaller output filesizes with minimal loss of precision.
* Urban Nature Access
* The model now works as expected when the user provides an LULC raster
that does not have a nodata value defined.
https://github.com/natcap/invest/issues/1293
* Workbench
* Several small updates to the model input form UI to improve usability
and visual consistency (https://github.com/natcap/invest/issues/912)
Expand Down
21 changes: 10 additions & 11 deletions src/natcap/invest/urban_nature_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -2542,23 +2542,22 @@ def _warp_lulc(source_lulc_path, target_lulc_path, target_pixel_size,
"""
source_raster_info = pygeoprocessing.get_raster_info(source_lulc_path)
target_nodata = source_raster_info['nodata'][0]
if target_nodata is None:
# Guarantee that our nodata cannot be represented by the datatype -
# select a nodata value that's out of range.
target_nodata = pygeoprocessing.choose_nodata(
source_raster_info['numpy_type']) + 1

pygeoprocessing.warp_raster(
source_lulc_path, target_pixel_size, target_lulc_path,
'near', target_bb=target_bounding_box,
target_projection_wkt=source_raster_info['projection_wkt'])

# if there is no defined nodata, set a default value
raster = gdal.OpenEx(target_lulc_path, gdal.GA_Update)
band = raster.GetRasterBand(1)
band.SetNoDataValue(target_nodata)
band = None
raster = None
if target_nodata is None:
# Guarantee that our nodata cannot be represented by the datatype -
# select a nodata value that's out of range.
target_nodata = pygeoprocessing.choose_nodata(
source_raster_info['numpy_type']) + 1
raster = gdal.OpenEx(target_lulc_path, gdal.GA_Update)
band = raster.GetRasterBand(1)
band.SetNoDataValue(target_nodata)
band = None
raster = None


def _mask_raster(source_raster_path, mask_raster_path, target_raster_path):
Expand Down
15 changes: 15 additions & 0 deletions tests/test_urban_nature_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,21 @@ def test_core_model(self):
self.assertAlmostEqual(numpy.min(valid_pixels), 1171.7352294921875)
self.assertAlmostEqual(numpy.max(valid_pixels), 11898.0712890625)

def test_no_lulc_nodata(self):
"""UNA: verify behavior when the LULC has no nodata value."""
from natcap.invest import urban_nature_access

args = _build_model_args(self.workspace_dir)
args['search_radius_mode'] = urban_nature_access.RADIUS_OPT_UNIFORM
args['search_radius'] = 100

raster = gdal.OpenEx(args['lulc_raster_path'], gdal.OF_RASTER)
band = raster.GetRasterBand(1)
band.DeleteNoDataValue()
band = None
raster = None
urban_nature_access.execute(args)

def test_split_urban_nature(self):
from natcap.invest import urban_nature_access

Expand Down

0 comments on commit 908b34c

Please sign in to comment.