Skip to content

Commit

Permalink
Replaced occurences of .interpolate() with assign_hazar_levels_to_exp…
Browse files Browse the repository at this point in the history
…osure_data as per issue #101
  • Loading branch information
uniomni committed Sep 1, 2012
1 parent 1dbf6ec commit 7d88913
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 29 deletions.
87 changes: 63 additions & 24 deletions safe/engine/test_engine.py
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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)
21 changes: 16 additions & 5 deletions safe/storage/interpolation.py
Expand Up @@ -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)
Expand Down

0 comments on commit 7d88913

Please sign in to comment.