Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UFRM: preserve source AOI fields in output vector #1626

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ Unreleased Changes
(https://github.com/natcap/invest/issues/1598)
* Fixed a bug that was allowing readonly workspace directories on Windows
(https://github.com/natcap/invest/issues/1599)
* Urban Flood Risk
* Fields present on the input AOI vector are now retained in the output.
(https://github.com/natcap/invest/issues/1600)

3.14.2 (2024-05-29)
-------------------
Expand Down
5 changes: 5 additions & 0 deletions src/natcap/invest/urban_flood_risk_mitigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,7 @@ def _write_summary_vector(
"""
source_aoi_vector = gdal.OpenEx(source_aoi_vector_path, gdal.OF_VECTOR)
source_aoi_layer = source_aoi_vector.GetLayer()
source_aoi_field_defns = source_aoi_layer.schema
source_geom_type = source_aoi_layer.GetGeomType()
source_srs_wkt = pygeoprocessing.get_vector_info(
source_aoi_vector_path)['projection_wkt']
Expand Down Expand Up @@ -612,6 +613,10 @@ def _write_summary_vector(
'flood_vol', float(flood_volume_stats[feature_id]['sum']))

target_watershed_layer.CreateFeature(target_feature)

for field_defn in source_aoi_field_defns:
target_watershed_layer.CreateField(field_defn)

target_watershed_layer.SyncToDisk()
target_watershed_layer = None
target_watershed_vector = None
Expand Down
25 changes: 19 additions & 6 deletions tests/test_ufrm.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,24 @@ def test_ufrm_regression(self):
"""UFRM: regression test."""
from natcap.invest import urban_flood_risk_mitigation
args = self._make_args()
input_vector = gdal.OpenEx(args['aoi_watersheds_path'],
gdal.OF_VECTOR)
input_layer = input_vector.GetLayer()
input_fields = [field.GetName() for field in input_layer.schema]

urban_flood_risk_mitigation.execute(args)

result_vector = gdal.OpenEx(os.path.join(
args['workspace_dir'], 'flood_risk_service_Test1.shp'),
gdal.OF_VECTOR)
result_layer = result_vector.GetLayer()

# Check that all four expected fields are there.
# Check that all expected fields are there.
output_fields = ['aff_bld', 'serv_blt', 'rnf_rt_idx',
'rnf_rt_m3', 'flood_vol']
output_fields += input_fields
self.assertEqual(
set(('aff_bld', 'serv_blt', 'rnf_rt_idx', 'rnf_rt_m3',
'flood_vol')),
set(output_fields),
set(field.GetName() for field in result_layer.schema))

result_feature = result_layer.GetFeature(0)
Expand All @@ -94,6 +101,11 @@ def test_ufrm_regression_no_infrastructure(self):
from natcap.invest import urban_flood_risk_mitigation
args = self._make_args()
del args['built_infrastructure_vector_path']
input_vector = gdal.OpenEx(args['aoi_watersheds_path'],
gdal.OF_VECTOR)
input_layer = input_vector.GetLayer()
input_fields = [field.GetName() for field in input_layer.schema]

urban_flood_risk_mitigation.execute(args)

result_raster = gdal.OpenEx(os.path.join(
Expand All @@ -115,9 +127,11 @@ def test_ufrm_regression_no_infrastructure(self):
result_layer = result_vector.GetLayer()
result_feature = result_layer.GetFeature(0)

# Check that only the two expected fields are there.
# Check that only the expected fields are there.
output_fields = ['rnf_rt_idx', 'rnf_rt_m3', 'flood_vol']
output_fields += input_fields
self.assertEqual(
set(('rnf_rt_idx', 'rnf_rt_m3', 'flood_vol')),
set(output_fields),
set(field.GetName() for field in result_layer.schema))

for fieldname, expected_value in (
Expand Down Expand Up @@ -218,7 +232,6 @@ def test_ufrm_explicit_zeros_in_table(self):
except ValueError:
self.fail('unexpected ValueError when testing curve number row with all zeros')


def test_ufrm_string_damage_to_infrastructure(self):
"""UFRM: handle str(int) structure indices.

Expand Down
Loading