Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
plugin_list = get_plugins(plugin_name)
assert len(plugin_list) == 1
assert plugin_list[0].keys()[0] == plugin_name
IF = plugin_list[0][plugin_name]
impact_vector = calculate_impact(layers=[H, E],
impact_fcn=IF)
impact_filename = impact_vector.get_filename()
# Read input data
hazard_raster = read_layer(haz_filename)
A = hazard_raster.get_data()
mmi_min, mmi_max = hazard_raster.get_extrema()
exposure_vector = read_layer(exp_filename)
coordinates = exposure_vector.get_geometry()
attributes = exposure_vector.get_data()
# Extract calculated result
icoordinates = impact_vector.get_geometry()
iattributes = impact_vector.get_data()
# First check that interpolated MMI was done as expected
fid = open('%s/test_buildings_percentage_loss_and_mmi.txt'
% TESTDATA)
reference_points = []
MMI = []
DAM = []
for line in fid.readlines()[1:]:
fields = line.strip().split(',')
def test_earthquake_fatality_estimation_allen(self):
"""Fatalities from ground shaking can be computed correctly 1
using aligned rasters
"""
# Name file names for hazard level, exposure and expected fatalities
hazard_filename = '%s/Earthquake_Ground_Shaking_clip.tif' % TESTDATA
exposure_filename = '%s/Population_2010_clip.tif' % TESTDATA
# Calculate impact using API
H = read_layer(hazard_filename)
E = read_layer(exposure_filename)
plugin_name = 'Earthquake Fatality Function'
plugin_list = get_plugins(plugin_name)
assert len(plugin_list) == 1
assert plugin_list[0].keys()[0] == plugin_name
IF = plugin_list[0][plugin_name]
# Call calculation engine
impact_layer = calculate_impact(layers=[H, E],
impact_fcn=IF)
impact_filename = impact_layer.get_filename()
# Do calculation manually and check result
hazard_raster = read_layer(hazard_filename)
"""
# This test merely exercises the use case as there is
# no reference data. It does check the sanity of values as
# far as possible.
hazard_filename = ('%s/tsunami_max_inundation_depth_4326.tif'
% TESTDATA)
exposure_filename = ('%s/tsunami_building_exposure.shp' % TESTDATA)
exposure_with_depth_filename = ('%s/tsunami_building_exposure'
'.shp' % TESTDATA)
reference_impact_filename = ('%s/tsunami_building_assessment'
'.shp' % TESTDATA)
# Calculate impact using API
H = read_layer(hazard_filename)
E = read_layer(exposure_filename)
plugin_name = 'Tsunami Building Loss Function'
plugin_list = get_plugins(plugin_name)
assert len(plugin_list) == 1
assert plugin_list[0].keys()[0] == plugin_name
IF = plugin_list[0][plugin_name]
impact_vector = calculate_impact(layers=[H, E],
impact_fcn=IF)
impact_filename = impact_vector.get_filename()
# Read calculated result
impact_vector = read_layer(impact_filename) # Read to have truncation
icoordinates = impact_vector.get_geometry()
iattributes = impact_vector.get_data()
N = len(icoordinates)
def test_earthquake_impact_on_women_example(self):
"""Earthquake impact on women example works
"""
# This only tests that the function runs and has the right
# strings in the output. No test of quantitative numbers
# (because we can't).
# Name file names for hazard level, exposure and expected fatalities
hazard_filename = '%s/Earthquake_Ground_Shaking_clip.tif' % TESTDATA
exposure_filename = '%s/Population_2010_clip.tif' % TESTDATA
# Calculate impact using API
H = read_layer(hazard_filename)
E = read_layer(exposure_filename)
plugin_name = 'Earthquake Women Impact Function'
plugin_list = get_plugins(plugin_name)
assert len(plugin_list) == 1
assert plugin_list[0].keys()[0] == plugin_name
IF = plugin_list[0][plugin_name]
# Call calculation engine
impact_layer = calculate_impact(layers=[H, E],
impact_fcn=IF)
impact_filename = impact_layer.get_filename()
I = read_layer(impact_filename) # Can read result
print impact_layer.get_impact_summary()
E = read_layer(exposure_filename)
plugin_name = 'Empirical Fatality Function'
plugin_list = get_plugins(plugin_name)
assert len(plugin_list) == 1
assert plugin_list[0].keys()[0] == plugin_name
IF = plugin_list[0][plugin_name]
# Call calculation engine
impact_layer = calculate_impact(layers=[H, E],
impact_fcn=IF)
impact_filename = impact_layer.get_filename()
# Do calculation manually and check result
hazard_raster = read_layer(hazard_filename)
H = hazard_raster.get_data(nan=0)
exposure_raster = read_layer(exposure_filename)
E = exposure_raster.get_data(nan=0)
# Verify correctness of result
C = impact_layer.get_data(nan=0)
# Calculate impact manually
# FIXME (Ole): Jono will do this
return
# Compare shape and extrema
msg = ('Shape of calculated raster differs from reference raster: '
'C=%s, F=%s' % (C.shape, F.shape))
assert numpy.allclose(C.shape, F.shape, rtol=1e-12, atol=1e-12), msg
# Name file names for hazard level, exposure and expected fatalities
hazard_filename = ('%s/maumere_aos_depth_20m_land_wgs84.asc'
% HAZDATA)
exposure_filename = ('%s/maumere_pop_prj.shp' % TESTDATA)
# Read input data
H = read_layer(hazard_filename)
A = H.get_data()
depth_min, depth_max = H.get_extrema()
# Compare extrema to values read off QGIS for this layer
assert numpy.allclose([depth_min, depth_max], [0.0, 16.68],
rtol=1.0e-6, atol=1.0e-10)
E = read_layer(exposure_filename)
coordinates = E.get_geometry()
attributes = E.get_data()
# Test the interpolation function
I = H.interpolate(E, attribute_name='depth')
Icoordinates = I.get_geometry()
Iattributes = I.get_data()
assert numpy.allclose(Icoordinates, coordinates)
N = len(Icoordinates)
assert N == 891
# Verify interpolated values with test result
for i in range(N):
interpolated_depth = Iattributes[i]['depth']
raster_filename)
# Write test interpolation point to a vector file
coordinates = []
for xi in longitudes:
for eta in latitudes:
coordinates.append((xi, eta))
vector_filename = unique_filename(suffix='.shp')
write_vector_data(data=None,
projection=projection,
geometry=coordinates,
filename=vector_filename)
# Read both datasets back in
R = read_layer(raster_filename)
V = read_layer(vector_filename)
# Then test that axes and data returned by R are correct
x, y = R.get_geometry()
msg = 'X axes was %s, should have been %s' % (longitudes, x)
assert numpy.allclose(longitudes, x), msg
msg = 'Y axes was %s, should have been %s' % (latitudes, y)
assert numpy.allclose(latitudes, y), msg
AA = R.get_data()
msg = 'Raster data was %s, should have been %s' % (AA, A)
assert numpy.allclose(AA, A), msg
# Test interpolation function
I = R.interpolate(V, attribute_name='value')
Icoordinates = I.get_geometry()
Iattributes = I.get_data()
def test_interpolation_tsunami(self):
"""Interpolation using tsunami data set works
This is test for issue #19 about interpolation overshoot
"""
# Name file names for hazard level, exposure and expected fatalities
hazard_filename = ('%s/tsunami_max_inundation_depth_utm56s.tif'
% TESTDATA)
exposure_filename = ('%s/tsunami_building_exposure.shp' % TESTDATA)
# Read input data
hazard_raster = read_layer(hazard_filename)
A = hazard_raster.get_data()
depth_min, depth_max = hazard_raster.get_extrema()
exposure_vector = read_layer(exposure_filename)
coordinates = exposure_vector.get_geometry()
attributes = exposure_vector.get_data()
# Test interpolation function
I = hazard_raster.interpolate(exposure_vector,
attribute_name='depth')
Icoordinates = I.get_geometry()
Iattributes = I.get_data()
assert numpy.allclose(Icoordinates, coordinates)
# Verify interpolated values with test result
for i in range(len(Icoordinates)):
def test_interpolation_from_polygons_one_poly(self):
"""Point interpolation using one polygon from Maumere works
This is a test for interpolation (issue #48)
"""
# Name file names for hazard level and exposure
hazard_filename = ('%s/tsunami_polygon_WGS84.shp' % TESTDATA)
exposure_filename = ('%s/building_Maumere.shp' % TESTDATA)
# Read input data
H = read_layer(hazard_filename)
H_attributes = H.get_data()
H_geometry = H.get_geometry()
# Cut down to make test quick
# Polygon #799 is the one used in separate test
H = Vector(data=H_attributes[799:800],
geometry=H_geometry[799:800],
projection=H.get_projection())
#H.write_to_file('MM_799.shp') # E.g. to view with QGis
E = read_layer(exposure_filename)
E_geometry = E.get_geometry()
E_attributes = E.get_data()
# Test interpolation function
I = H.interpolate(E, name='depth',
plugin_name = 'Earthquake Guidelines Function'
plugin_list = get_plugins(plugin_name)
assert len(plugin_list) == 1
assert plugin_list[0].keys()[0] == plugin_name
IF = plugin_list[0][plugin_name]
impact_vector = calculate_impact(layers=[H, E],
impact_fcn=IF)
impact_filename = impact_vector.get_filename()
# Read input data
hazard_raster = read_layer(hazard_filename)
A = hazard_raster.get_data()
mmi_min, mmi_max = hazard_raster.get_extrema()
exposure_vector = read_layer(exposure_filename)
coordinates = exposure_vector.get_geometry()
attributes = exposure_vector.get_data()
# Extract calculated result
icoordinates = impact_vector.get_geometry()
iattributes = impact_vector.get_data()
# Verify interpolated MMI with test result
for i in range(len(iattributes)):
calculated_mmi = iattributes[i]['MMI']
if numpy.isnan(calculated_mmi):
continue
# Check that interpolated points are within range
msg = ('Interpolated mmi %f from file %s was outside '