Exemplo n.º 1
0
    def test_ufrm_regression(self):
        """UFRM: regression test."""
        from natcap.invest import urban_flood_risk_mitigation
        args = self._make_args()
        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.
        self.assertEqual(
            set(('aff.bld', 'serv.blt', 'rnf_rt_idx', 'rnf_rt_m3',
                 'flood_vol')),
            set(field.GetName() for field in result_layer.schema))

        result_feature = next(result_layer)
        for fieldname, expected_value in (('aff.bld', 187010830.32202843),
                                          ('serv.blt', 13253546667257.65),
                                          ('rnf_rt_idx', 0.70387527942),
                                          ('rnf_rt_m3', 70870.4765625),
                                          ('flood_vol', 29815.640625)):
            result_val = result_feature.GetField(fieldname)
            places_to_round = (
                int(round(numpy.log(expected_value) / numpy.log(10))) - 6)
            self.assertAlmostEqual(result_val,
                                   expected_value,
                                   places=-places_to_round)

        result_feature = None
        result_layer = None
        result_vector = None
Exemplo n.º 2
0
    def test_ufrm_value_error_on_bad_soil(self):
        """UFRM: assert exception on bad soil raster values."""
        from natcap.invest import urban_flood_risk_mitigation
        args = self._make_args()

        bad_soil_raster = os.path.join(self.workspace_dir,
                                       'bad_soilgroups.tif')
        value_map = {
            1: 1,
            2: 2,
            3: 9,  # only 1, 2, 3, 4 are valid values for this raster.
            4: 4
        }
        pygeoprocessing.reclassify_raster(
            (args['soils_hydrological_group_raster_path'], 1), value_map,
            bad_soil_raster, gdal.GDT_Int16, -9)
        args['soils_hydrological_group_raster_path'] = bad_soil_raster

        with self.assertRaises(ValueError) as cm:
            urban_flood_risk_mitigation.execute(args)

        actual_message = str(cm.exception)
        expected_message = (
            'Check that the Soil Group raster does not contain')
        self.assertTrue(expected_message in actual_message)
Exemplo n.º 3
0
    def test_ufrm_regression_no_infrastructure(self):
        """UFRM: regression for no infrastructure."""
        from natcap.invest import urban_flood_risk_mitigation
        args = self._make_args()
        del args['built_infrastructure_vector_path']
        urban_flood_risk_mitigation.execute(args)

        result_raster = gdal.OpenEx(
            os.path.join(args['workspace_dir'],
                         'Runoff_retention_m3_Test1.tif'), gdal.OF_RASTER)
        band = result_raster.GetRasterBand(1)
        array = band.ReadAsArray()
        nodata = band.GetNoDataValue()
        band = None
        result_raster = None
        result_sum = numpy.sum(array[~numpy.isclose(array, nodata)])
        # expected result observed from regression run.
        expected_result = 156070.36
        self.assertAlmostEqual(result_sum, expected_result, places=0)
Exemplo n.º 4
0
    def test_ufrm_value_error_on_bad_lucode(self):
        """UFRM: assert exception on missing lucodes."""
        import pandas
        from natcap.invest import urban_flood_risk_mitigation
        args = self._make_args()

        bad_cn_table_path = os.path.join(self.workspace_dir,
                                         'bad_cn_table.csv')
        cn_table = pandas.read_csv(args['curve_number_table_path'])

        # drop a row with an lucode known to exist in lulc raster
        # This is a code that will successfully index into the
        # CN table sparse matrix, but will not return valid data.
        bad_cn_table = cn_table[cn_table['lucode'] != 0]
        bad_cn_table.to_csv(bad_cn_table_path, index=False)
        args['curve_number_table_path'] = bad_cn_table_path

        with self.assertRaises(ValueError) as cm:
            urban_flood_risk_mitigation.execute(args)

        actual_message = str(cm.exception)
        expected_message = (
            f'The biophysical table is missing a row for lucode(s) {[0]}')
        self.assertEqual(expected_message, actual_message)

        # drop rows with lucodes known to exist in lulc raster
        # These are codes that will raise an IndexError on
        # indexing into the CN table sparse matrix. The test
        # LULC raster has values from 0 to 21.
        bad_cn_table = cn_table[cn_table['lucode'] < 15]
        bad_cn_table.to_csv(bad_cn_table_path, index=False)
        args['curve_number_table_path'] = bad_cn_table_path

        with self.assertRaises(ValueError) as cm:
            urban_flood_risk_mitigation.execute(args)

        actual_message = str(cm.exception)
        expected_message = (
            f'The biophysical table is missing a row for lucode(s) '
            f'{[16, 17, 18, 21]}')
        self.assertEqual(expected_message, actual_message)
Exemplo n.º 5
0
    def test_ufrm_regression(self):
        """UFRM: regression test."""
        from natcap.invest import urban_flood_risk_mitigation
        args = self._make_args()
        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()
        result_feature = next(result_layer)
        result_val = result_feature.GetField('serv_bld')
        result_feature = None
        result_layer = None
        result_vector = None
        # expected result observed from regression run.
        expected_result = 13253546667257.65
        places_to_round = (
            int(round(numpy.log(expected_result) / numpy.log(10))) - 6)
        self.assertAlmostEqual(result_val,
                               expected_result,
                               places=-places_to_round)
Exemplo n.º 6
0
    def test_ufrm_regression_no_infrastructure(self):
        """UFRM: regression for no infrastructure."""
        from natcap.invest import urban_flood_risk_mitigation
        args = self._make_args()
        del args['built_infrastructure_vector_path']
        urban_flood_risk_mitigation.execute(args)

        result_raster = gdal.OpenEx(
            os.path.join(args['workspace_dir'],
                         'Runoff_retention_m3_Test1.tif'), gdal.OF_RASTER)
        band = result_raster.GetRasterBand(1)
        array = band.ReadAsArray()
        nodata = band.GetNoDataValue()
        band = None
        result_raster = None
        result_sum = numpy.sum(array[~numpy.isclose(array, nodata)])
        # expected result observed from regression run.
        expected_result = 156070.36
        self.assertAlmostEqual(result_sum, expected_result, places=0)

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

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

        for fieldname, expected_value in (('rnf_rt_idx', 0.70387527942),
                                          ('rnf_rt_m3', 70870.4765625),
                                          ('flood_vol', 29815.640625)):
            result_val = result_feature.GetField(fieldname)
            places_to_round = (
                int(round(numpy.log(expected_value) / numpy.log(10))) - 6)
            self.assertAlmostEqual(result_val,
                                   expected_value,
                                   places=-places_to_round)
Exemplo n.º 7
0
    def test_ufrm_invalid_validation(self):
        """UFRM: assert validation error on bad args."""
        from natcap.invest import urban_flood_risk_mitigation

        with self.assertRaises(ValueError):
            urban_flood_risk_mitigation.execute({})