From 7d88913bb5a773a2da93098acbf2b696e02af4be Mon Sep 17 00:00:00 2001 From: Ole Nielsen Date: Sat, 1 Sep 2012 16:27:23 +0700 Subject: [PATCH] Replaced occurences of .interpolate() with assign_hazar_levels_to_exposure_data as per issue #101 --- safe/engine/test_engine.py | 87 +++++++++++++++++++++++++---------- safe/storage/interpolation.py | 21 +++++++-- 2 files changed, 79 insertions(+), 29 deletions(-) diff --git a/safe/engine/test_engine.py b/safe/engine/test_engine.py index 9270ddef13..fa83cf88dc 100644 --- a/safe/engine/test_engine.py +++ b/safe/engine/test_engine.py @@ -14,6 +14,7 @@ from safe.storage.vector import Vector from safe.storage.interpolation import interpolate_polygon_raster from safe.storage.interpolation import interpolate_raster_vector_points +from safe.storage.interpolation import assign_hazard_values_to_exposure_data from safe.storage.utilities import DEFAULT_ATTRIBUTE from safe.common.polygon import separate_points_by_polygon @@ -1246,7 +1247,8 @@ def test_interpolation_functions(self): assert numpy.allclose(AA, A), msg # Test interpolation function with default layer_name - I = R.interpolate(V, attribute_name='value') + #I = R.interpolate(V, attribute_name='value') + I = assign_hazard_values_to_exposure_data(R, V, attribute_name='value') assert V.get_name() == I.get_name() Icoordinates = I.get_geometry() @@ -1285,8 +1287,11 @@ def test_interpolation_lembang(self): attributes = exposure_vector.get_data() # Test interpolation function - I = hazard_raster.interpolate(exposure_vector, - attribute_name='MMI') + #I = hazard_raster.interpolate(exposure_vector, + # attribute_name='MMI') + I = assign_hazard_values_to_exposure_data(hazard_raster, + exposure_vector, + attribute_name='MMI') Icoordinates = I.get_geometry() Iattributes = I.get_data() assert numpy.allclose(Icoordinates, coordinates) @@ -1343,7 +1348,7 @@ def test_interpolation_tsunami(self): """ # Name file names for hazard level, exposure and expected fatalities - hazard_filename = ('%s/tsunami_max_inundation_depth_utm56s.tif' + hazard_filename = ('%s/tsunami_max_inundation_depth_4326.tif' % TESTDATA) exposure_filename = ('%s/tsunami_building_exposure.shp' % TESTDATA) @@ -1355,8 +1360,11 @@ def test_interpolation_tsunami(self): coordinates = exposure_vector.get_geometry() # Test interpolation function - I = hazard_raster.interpolate(exposure_vector, - attribute_name='depth') + #I = hazard_raster.interpolate(exposure_vector, + # attribute_name='depth') + I = assign_hazard_values_to_exposure_data(hazard_raster, + exposure_vector, + attribute_name='depth') Icoordinates = I.get_geometry() Iattributes = I.get_data() assert numpy.allclose(Icoordinates, coordinates) @@ -1397,7 +1405,8 @@ def test_interpolation_tsunami_maumere(self): attributes = E.get_data() # Test the interpolation function - I = H.interpolate(E, attribute_name='depth') + #I = H.interpolate(E, attribute_name='depth') + I = assign_hazard_values_to_exposure_data(H, E, attribute_name='depth') Icoordinates = I.get_geometry() Iattributes = I.get_data() assert numpy.allclose(Icoordinates, coordinates) @@ -1524,8 +1533,12 @@ def test_interpolation_from_polygons_one_poly(self): E_attributes = E.get_data() # Test interpolation function - I = H.interpolate(E, layer_name='depth', - attribute_name=None) # Take all attributes across + #I = H.interpolate(E, layer_name='depth', + # attribute_name=None) # Take all attributes across + I = assign_hazard_values_to_exposure_data(H, E, + layer_name='depth', + # Take all attributes across + attribute_name=None) I_attributes = I.get_data() assert I.get_name() == 'depth' @@ -1585,8 +1598,13 @@ def test_interpolation_from_polygons_multiple(self): E_attributes = E.get_data() # Test interpolation function - I = H.interpolate(E, layer_name='depth', - attribute_name=None) # Take all attributes across + #I = H.interpolate(E, layer_name='depth', + # attribute_name=None) # Take all attributes across + I = assign_hazard_values_to_exposure_data(H, E, + layer_name='depth', + # Take all attributes across + attribute_name=None) + I_attributes = I.get_data() N = len(I_attributes) @@ -1709,9 +1727,14 @@ def Xtest_point_interpolation_from_polygons_one_attribute(self): E_attributes = E.get_data() # Test interpolation function - I = H.interpolate(E, layer_name='depth', - # Spelling is as in test data - attribute_name='Catergory') + #I = H.interpolate(E, layer_name='depth', + # # Spelling is as in test data + # attribute_name='Catergory') + I = assign_hazard_values_to_exposure_data(H, E, + layer_name='depth', + # Spelling is as in test data + attribute_name='Catergory') + #I.write_to_file('MM_res.shp') I_attributes = I.get_data() @@ -1767,9 +1790,10 @@ def test_interpolation_from_polygons_error_handling(self): # Check projection mismatch is caught try: - H.interpolate(E) + #H.interpolate(E) + assign_hazard_values_to_exposure_data(H, E) except VerificationError, e: - msg = ('Projection mismatch shoud have been caught: %s' + msg = ('Projection mismatch should have been caught: %s' % str(e)) assert 'Projections' in str(e), msg else: @@ -1913,8 +1937,13 @@ def test_line_interpolation_from_polygons_one_poly(self): E = read_layer(exposure_filename) # Test interpolation function - I = H.interpolate(E, layer_name='depth', - attribute_name=None) # Take all attributes across + #I = H.interpolate(E, layer_name='depth', + # attribute_name=None) # Take all attributes across + I = assign_hazard_values_to_exposure_data(H, E, + layer_name='depth', + # Take all attributes across + attribute_name=None) + I_geometry = I.get_geometry() I_attributes = I.get_data() assert I.get_name() == 'depth' @@ -2025,9 +2054,14 @@ def Xtest_line_interpolation_from_polygons_one_attribute(self): E = read_layer(exposure_filename) # Test interpolation function - I = H.interpolate(E, layer_name='depth', - # Spelling is as in test data - attribute_name='Catergory') + #I = H.interpolate(E, layer_name='depth', + # # Spelling is as in test data + # attribute_name='Catergory') + I = assign_hazard_values_to_exposure_data(H, E, + layer_name='depth', + # Spelling is as in test data + attribute_name='Catergory') + I_geometry = I.get_geometry() I_attributes = I.get_data() @@ -2096,8 +2130,13 @@ def Xtest_line_interpolation_from_polygons(self): E = read_layer(exposure_filename) # Test interpolation function - I = H.interpolate(E, layer_name='depth', - attribute_name=None) # Take all attributes across + #I = H.interpolate(E, layer_name='depth', + # attribute_name=None) # Take all attributes across + I = assign_hazard_values_to_exposure_data(H, E, + layer_name='depth', + # Take all attributes across + attribute_name=None) + I_geometry = I.get_geometry() I_attributes = I.get_data() assert I.get_name() == 'depth' @@ -2534,6 +2573,6 @@ def test_lognormal_cdf(self): assert numpy.allclose(x, r, rtol=1.0e-6, atol=1.0e-6), msg if __name__ == '__main__': - suite = unittest.makeSuite(Test_Engine, 'test') + suite = unittest.makeSuite(Test_Engine, 'test_interpolation_tsunami') runner = unittest.TextTestRunner(verbosity=2) runner.run(suite) diff --git a/safe/storage/interpolation.py b/safe/storage/interpolation.py index 58451a5a46..fb74bdc87a 100644 --- a/safe/storage/interpolation.py +++ b/safe/storage/interpolation.py @@ -81,14 +81,25 @@ def assign_hazard_values_to_exposure_data(hazard, exposure, """ - # FIXME: Push type checking into the individual modules + # FIXME: Push type checking into separate function input_checks + # Also take care of None values there + msg = ('Projections must be the same: I got %s and %s' + % (hazard.projection, exposure.projection)) + verify(hazard.projection == exposure.projection, msg) + + msg = ('Parameter attribute_name must be either a string or None. ' + 'I got %s' % (str(type(exposure)))[1:-1]) + verify(attribute_name is None + or isinstance(attribute_name, basestring), msg) + + msg = ('Parameter layer_name must be either a string or None. ' + 'I got %s' % (str(type(exposure)))[1:-1]) + verify(layer_name is None + or isinstance(layer_name, basestring), msg) + if hazard.is_raster: if exposure.is_vector: # Interpolate this raster layer to geometry of exposure - msg = ('Name must be either a string or None. I got %s' - % (str(type(exposure)))[1:-1]) - verify(attribute_name is None - or isinstance(attribute_name, basestring), msg) return interpolate_raster_vector(hazard, exposure, layer_name=layer_name, attribute_name=attribute_name)