def test_flood_population_evacuation_polygon(self): """Flood population evacuation (flood is polygon) """ population = 'pop_clip_flood_test.tif' flood_data = 'flood_poly_clip_flood_test.shp' plugin_name = 'FloodEvacuationFunctionVectorHazard' hazard_filename = os.path.join(TESTDATA, flood_data) exposure_filename = os.path.join(TESTDATA, population) # Calculate impact using API hazard_layer = read_layer(hazard_filename) exposure_layer = read_layer(exposure_filename) impact_function = get_plugin(plugin_name) # Call calculation engine impact_layer = calculate_impact(layers=[hazard_layer, exposure_layer], impact_fcn=impact_function) impact_filename = impact_layer.get_filename() impact_layer = read_layer(impact_filename) keywords = impact_layer.get_keywords() # print "keywords", keywords affected_population = float(keywords['affected_population']) total_population = keywords['total_population'] self.assertEqual(affected_population, 134000) self.assertEqual(total_population, 163000)
def test_volcano_population_evacuation_impact(self): """Population impact from volcanic hazard is computed correctly.""" # Name file names for hazard level, exposure and expected fatalities hazard_filename = '%s/donut.shp' % TESTDATA exposure_filename = ('%s/pop_merapi_clip.tif' % TESTDATA) # Calculate impact using API hazard_layer = read_layer(hazard_filename) exposure_layer = read_layer(exposure_filename) plugin_name = 'Volcano Polygon Hazard Population' impact_function = get_plugin(plugin_name) impact_layer = calculate_impact(layers=[hazard_layer, exposure_layer], impact_fcn=impact_function) impact_filename = impact_layer.get_filename() impact_layer = read_layer(impact_filename) keywords = impact_layer.get_keywords() # Check for expected results: for value in ['Merapi', 192055, 56514, 68568, 66971]: if isinstance(value, int): x = format_int(population_rounding(value)) else: x = value summary = keywords['impact_summary'] msg = ('Did not find expected value %s in summary %s' % (x, summary)) assert x in summary, msg
def test_volcano_circle_population_impact(self): """Volcano function runs circular evacuation zone.""" # Name file names for hazard level, exposure and expected fatalities hazard_filename = '%s/Merapi_alert.shp' % TESTDATA exposure_filename = ('%s/glp10ag.asc' % EXPDATA) # Calculate impact using API hazard_layer = read_layer(hazard_filename) exposure_layer = read_layer(exposure_filename) plugin_name = 'Volcano Polygon Hazard Population' impact_function = get_plugin(plugin_name) impact_layer = calculate_impact(layers=[hazard_layer, exposure_layer], impact_fcn=impact_function) impact_filename = impact_layer.get_filename() impact_layer = read_layer(impact_filename) keywords = impact_layer.get_keywords() print keywords # This is the expected number of people affected # Distance [km] Total Cumulative # 3 15.800 15.800 # 5 17.300 33.100 # 10 125.000 158.000 message = 'Result not as expected' impact_summary = keywords['impact_summary'] self.assertTrue(format_int(15800) in impact_summary, message) self.assertTrue(format_int(17300) in impact_summary, message) self.assertTrue(format_int(125000) in impact_summary, message)
def test_pager_earthquake_fatality_estimation(self): """Fatalities from ground shaking can be computed correctly using the Pager fatality model.""" # Name file names for hazard level, exposure and expected fatalities hazard_filename = '%s/itb_test_mmi.asc' % TESTDATA exposure_filename = '%s/itb_test_pop.asc' % TESTDATA # Calculate impact using API hazard_layer = read_layer(hazard_filename) exposure_layer = read_layer(exposure_filename) plugin_name = 'PAG Fatality Function' impact_function = get_plugin(plugin_name) # Call calculation engine impact_layer = calculate_impact(layers=[hazard_layer, exposure_layer], impact_fcn=impact_function) impact_filename = impact_layer.get_filename() impact_layer = read_layer(impact_filename) keywords = impact_layer.get_keywords() population = keywords['total_population'] fatalities = keywords['total_fatalities'] # Check aggregated values expected_population = 85425000.0 msg = ('Expected population was %f, I got %f' % (expected_population, population)) self.assertEqual(population, expected_population, msg) expected_fatalities = 410000.0 msg = ('Expected fatalities was %f, I got %f' % (expected_fatalities, fatalities)) assert numpy.allclose(fatalities, expected_fatalities, rtol=1.0e-5), msg
def test_flood_vector_building_impact_function(self): """Test flood building impact function works (flood is polygon).""" building = 'test_flood_building_impact_exposure.shp' flood_data = 'test_flood_building_impact_hazard.shp' plugin_name = 'FloodVectorBuildingImpactFunction' hazard_filename = os.path.join(TESTDATA, flood_data) exposure_filename = os.path.join(TESTDATA, building) # Calculate impact using API hazard_layer = read_layer(hazard_filename) exposure_layer = read_layer(exposure_filename) impact_function = get_plugin(plugin_name) # Call calculation engine impact_layer = calculate_impact(layers=[hazard_layer, exposure_layer], impact_fcn=impact_function) impact_filename = impact_layer.get_filename() impact_layer = read_layer(impact_filename) keywords = impact_layer.get_keywords() buildings_total = keywords['buildings_total'] buildings_affected = keywords['buildings_affected'] self.assertEqual(buildings_total, 67) self.assertEqual(buildings_affected, 41)
def test_volcano_population_evacuation_impact(self): """Population impact from volcanic hazard is computed correctly.""" # Name file names for hazard level, exposure and expected fatalities hazard_filename = '%s/donut.shp' % TESTDATA exposure_filename = ('%s/pop_merapi_clip.tif' % TESTDATA) # Calculate impact using API hazard_layer = read_layer(hazard_filename) exposure_layer = read_layer(exposure_filename) plugin_name = 'Volcano Polygon Hazard Population' impact_function = get_plugin(plugin_name) impact_layer = calculate_impact( layers=[hazard_layer, exposure_layer], impact_fcn=impact_function) impact_filename = impact_layer.get_filename() impact_layer = read_layer(impact_filename) keywords = impact_layer.get_keywords() # Check for expected results: for value in ['Merapi', 192055, 56514, 68568, 66971]: if isinstance(value, int): x = format_int(population_rounding(value)) else: x = value summary = keywords['impact_summary'] msg = ('Did not find expected value %s in summary %s' % (x, summary)) assert x in summary, msg
def test_volcano_circle_population_impact(self): """Volcano function runs circular evacuation zone.""" # Name file names for hazard level, exposure and expected fatalities hazard_filename = '%s/Merapi_alert.shp' % TESTDATA exposure_filename = ('%s/glp10ag.asc' % EXPDATA) # Calculate impact using API hazard_layer = read_layer(hazard_filename) exposure_layer = read_layer(exposure_filename) plugin_name = 'Volcano Polygon Hazard Population' impact_function = get_plugin(plugin_name) impact_layer = calculate_impact( layers=[hazard_layer, exposure_layer], impact_fcn=impact_function) impact_filename = impact_layer.get_filename() impact_layer = read_layer(impact_filename) keywords = impact_layer.get_keywords() print keywords # This is the expected number of people affected # Distance [km] Total Cumulative # 3 15.800 15.800 # 5 17.300 33.100 # 10 125.000 158.000 message = 'Result not as expected' impact_summary = keywords['impact_summary'] self.assertTrue(format_int(15800) in impact_summary, message) self.assertTrue(format_int(17300) in impact_summary, message) self.assertTrue(format_int(125000) in impact_summary, message)
def test_pager_earthquake_fatality_estimation(self): """Fatalities from ground shaking can be computed correctly using the Pager fatality model.""" # Name file names for hazard level, exposure and expected fatalities hazard_filename = '%s/itb_test_mmi.asc' % TESTDATA exposure_filename = '%s/itb_test_pop.asc' % TESTDATA # Calculate impact using API hazard_layer = read_layer(hazard_filename) exposure_layer = read_layer(exposure_filename) plugin_name = 'PAG Fatality Function' impact_function = get_plugin(plugin_name) # Call calculation engine impact_layer = calculate_impact( layers=[hazard_layer, exposure_layer], impact_fcn=impact_function) impact_filename = impact_layer.get_filename() impact_layer = read_layer(impact_filename) keywords = impact_layer.get_keywords() population = keywords['total_population'] fatalities = keywords['total_fatalities'] # Check aggregated values expected_population = 85425000.0 msg = ('Expected population was %f, I got %f' % (expected_population, population)) self.assertEqual(population, expected_population, msg) expected_fatalities = 410000.0 msg = ('Expected fatalities was %f, I got %f' % (expected_fatalities, fatalities)) assert numpy.allclose( fatalities, expected_fatalities, rtol=1.0e-5), msg
def test_earthquake_building_impact_function(self): """Earthquake Building Impact Function works as expected.""" # Name file names for hazard level, exposure and expected fatalities hazard_filename = '%s/eq_yogya_2006.asc' % HAZDATA exposure_filename = '%s/OSM_building_polygons_20110905.shp' % TESTDATA # Calculate impact using API hazard_layer = read_layer(hazard_filename) exposure_layer = read_layer(exposure_filename) plugin_name = 'Earthquake Building Impact Function' impact_function = get_plugin(plugin_name) # Call calculation engine impact_layer = calculate_impact(layers=[hazard_layer, exposure_layer], impact_fcn=impact_function) impact_filename = impact_layer.get_filename() impact_layer = read_layer(impact_filename) # calculated_result = I.get_data() # print calculated_result.shape keywords = impact_layer.get_keywords() impact_summary = keywords['impact_summary'] # This is the expected number of building might be affected # Hazard Level - Buildings Affected # Low - 845 # Medium - 15524 # High - 122 message = 'Result not as expected' self.assertTrue(format_int(845) in impact_summary, message) self.assertTrue(format_int(15524) in impact_summary, message) self.assertTrue(format_int(122) in impact_summary, message)
def test_volcano_building_impact(self): """Building impact from volcanic hazard is computed correctly.""" # Name file names for hazard level, exposure and expected fatalities hazard_filename = os.path.join(TESTDATA, 'donut.shp') exposure_filename = test_data_path('exposure', 'bangunan.shp') # Calculate impact using API hazard = read_layer(hazard_filename) exposure = read_layer(exposure_filename) plugin_name = 'Volcano Building Impact' impact_function = get_plugin(plugin_name) impact_function.parameters['name attribute'] = 'GUNUNG' print 'Calculating' # Call calculation engine impact_layer = calculate_impact( layers=[hazard, exposure], impact_fcn=impact_function) impact_filename = impact_layer.get_filename() impact = read_layer(impact_filename) keywords = impact.get_keywords() # Check for expected results: for value in ['Merapi', 5, 86, 91, 1, 21, 22, 6, 107, 113]: if isinstance(value, int): x = format_int(value) else: x = value summary = keywords['impact_summary'] message = ( 'Did not find expected value %s in summary %s' % (x, summary)) self.assertIn(x, summary, message)
def test_flood_vector_building_impact_function(self): """Test flood building impact function works (flood is polygon).""" building = 'test_flood_building_impact_exposure.shp' flood_data = 'test_flood_building_impact_hazard.shp' plugin_name = 'FloodVectorBuildingImpactFunction' hazard_filename = os.path.join(TESTDATA, flood_data) exposure_filename = os.path.join(TESTDATA, building) # Calculate impact using API hazard_layer = read_layer(hazard_filename) exposure_layer = read_layer(exposure_filename) impact_function = get_plugin(plugin_name) # Call calculation engine impact_layer = calculate_impact( layers=[hazard_layer, exposure_layer], impact_fcn=impact_function) impact_filename = impact_layer.get_filename() impact_layer = read_layer(impact_filename) keywords = impact_layer.get_keywords() buildings_total = keywords['buildings_total'] buildings_affected = keywords['buildings_affected'] self.assertEqual(buildings_total, 67) self.assertEqual(buildings_affected, 41)
def test_flood_raster_building_impact_function(self): """Flood raster building impact function works This test also exercises interpolation of hazard level (raster) to building locations (vector data). """ for haz_filename in ['Flood_Current_Depth_Jakarta_geographic.asc', 'Flood_Design_Depth_Jakarta_geographic.asc']: # Name file names for hazard level and exposure hazard_filename = '%s/%s' % (HAZDATA, haz_filename) exposure_filename = ('%s/OSM_building_polygons_20110905.shp' % TESTDATA) # Calculate impact using API hazard_layer = read_layer(hazard_filename) exposure_layer = read_layer(exposure_filename) plugin_name = 'FloodRasterBuildingImpactFunction' impact_function = get_plugin(plugin_name) impact_vector = calculate_impact( layers=[hazard_layer, exposure_layer], impact_fcn=impact_function) # Extract calculated result icoordinates = impact_vector.get_geometry() iattributes = impact_vector.get_data() # Check assert len(icoordinates) == 34960 assert len(iattributes) == 34960
def test_earthquake_building_impact_function(self): """Earthquake Building Impact Function works as expected.""" # Name file names for hazard level, exposure and expected fatalities hazard_filename = '%s/eq_yogya_2006.asc' % HAZDATA exposure_filename = '%s/OSM_building_polygons_20110905.shp' % TESTDATA # Calculate impact using API hazard_layer = read_layer(hazard_filename) exposure_layer = read_layer(exposure_filename) plugin_name = 'Earthquake Building Impact Function' impact_function = get_plugin(plugin_name) # Call calculation engine impact_layer = calculate_impact( layers=[hazard_layer, exposure_layer], impact_fcn=impact_function) impact_filename = impact_layer.get_filename() impact_layer = read_layer(impact_filename) # calculated_result = I.get_data() # print calculated_result.shape keywords = impact_layer.get_keywords() impact_summary = keywords['impact_summary'] # This is the expected number of building might be affected # Hazard Level - Buildings Affected # Low - 845 # Medium - 15524 # High - 122 message = 'Result not as expected' self.assertTrue(format_int(845) in impact_summary, message) self.assertTrue(format_int(15524) in impact_summary, message) self.assertTrue(format_int(122) in impact_summary, message)
def test_itb_earthquake_fatality_estimation(self): """Fatalities from ground shaking can be computed correctly using the ITB fatality model (Test data from Hadi Ghasemi).""" # Name file names for hazard level, exposure and expected fatalities hazard_filename = '%s/itb_test_mmi.asc' % TESTDATA exposure_filename = '%s/itb_test_pop.asc' % TESTDATA # Calculate impact using API hazard_layer = read_layer(hazard_filename) exposure_layer = read_layer(exposure_filename) plugin_name = 'ITB Fatality Function' impact_function = get_plugin(plugin_name) # Call calculation engine impact_layer = calculate_impact( layers=[hazard_layer, exposure_layer], impact_fcn=impact_function) impact_filename = impact_layer.get_filename() impact_layer = read_layer(impact_filename) # calculated_result = I.get_data() # print calculated_result.shape keywords = impact_layer.get_keywords() # print "keywords", keywords population = float(keywords['total_population']) fatalities = float(keywords['total_fatalities']) # Check aggregated values expected_population = population_rounding(85424650.0) msg = ('Expected population was %f, I got %f' % (expected_population, population)) assert population == expected_population, msg expected_fatalities = population_rounding(40871.3028) msg = ('Expected fatalities was %f, I got %f' % (expected_fatalities, fatalities)) assert numpy.allclose(fatalities, expected_fatalities, rtol=1.0e-5), msg # Check that aggregated number of fatilites is as expected all_numbers = int(numpy.sum([31.8937368131, 2539.26369372, 1688.72362573, 17174.9261705, 19436.834531])) msg = ('Aggregated number of fatalities not as expected: %i' % all_numbers) assert all_numbers == 40871, msg x = population_rounding(all_numbers) msg = ('Did not find expected fatality value %i in summary %s' % (x, keywords['impact_summary'])) assert format_int(x) in keywords['impact_summary'], msg
def test_itb_earthquake_fatality_estimation(self): """Fatalities from ground shaking can be computed correctly using the ITB fatality model (Test data from Hadi Ghasemi).""" # Name file names for hazard level, exposure and expected fatalities hazard_filename = '%s/itb_test_mmi.asc' % TESTDATA exposure_filename = '%s/itb_test_pop.asc' % TESTDATA # Calculate impact using API hazard_layer = read_layer(hazard_filename) exposure_layer = read_layer(exposure_filename) plugin_name = 'ITB Fatality Function' impact_function = get_plugin(plugin_name) # Call calculation engine impact_layer = calculate_impact(layers=[hazard_layer, exposure_layer], impact_fcn=impact_function) impact_filename = impact_layer.get_filename() impact_layer = read_layer(impact_filename) # calculated_result = I.get_data() # print calculated_result.shape keywords = impact_layer.get_keywords() # print "keywords", keywords population = float(keywords['total_population']) fatalities = float(keywords['total_fatalities']) # Check aggregated values expected_population = population_rounding(85424650.0) msg = ('Expected population was %f, I got %f' % (expected_population, population)) assert population == expected_population, msg expected_fatalities = population_rounding(40871.3028) msg = ('Expected fatalities was %f, I got %f' % (expected_fatalities, fatalities)) assert numpy.allclose(fatalities, expected_fatalities, rtol=1.0e-5), msg # Check that aggregated number of fatilites is as expected all_numbers = int( numpy.sum([ 31.8937368131, 2539.26369372, 1688.72362573, 17174.9261705, 19436.834531 ])) msg = ('Aggregated number of fatalities not as expected: %i' % all_numbers) assert all_numbers == 40871, msg x = population_rounding(all_numbers) msg = ('Did not find expected fatality value %i in summary %s' % (x, keywords['impact_summary'])) assert format_int(x) in keywords['impact_summary'], msg
def test_flood_population_evacuation(self): """Flood population evacuation""" population = 'people_jakarta_clip.tif' flood_data = 'flood_jakarta_clip.tif' plugin_name = 'FloodEvacuationFunction' hazard_filename = os.path.join(TESTDATA, flood_data) exposure_filename = os.path.join(TESTDATA, population) # Calculate impact using API hazard_layer = read_layer(hazard_filename) exposure_layer = read_layer(exposure_filename) impact_function = get_plugin(plugin_name) # Call calculation engine impact_layer = calculate_impact( layers=[hazard_layer, exposure_layer], impact_fcn=impact_function) impact_filename = impact_layer.get_filename() impact_layer = read_layer(impact_filename) keywords = impact_layer.get_keywords() # print "keywords", keywords evacuated = float(keywords['evacuated']) total_needs_full = keywords['total_needs'] total_needs_weekly = OrderedDict([ [x['table name'], x['amount']] for x in total_needs_full['weekly'] ]) total_needs_single = OrderedDict([ [x['table name'], x['amount']] for x in total_needs_full['single'] ]) expected_evacuated = 63400 self.assertEqual(evacuated, expected_evacuated) self.assertEqual(total_needs_weekly['Rice [kg]'], 177520) self.assertEqual(total_needs_weekly['Family Kits'], 12680) self.assertEqual(total_needs_weekly['Drinking Water [l]'], 1109500) self.assertEqual(total_needs_weekly['Clean Water [l]'], 4247800) self.assertEqual(total_needs_single['Toilets'], 3170)
def test_classified_hazard_population_impact_function(self): """Categorised Hazard Population IF works as expected.""" # Name file names for hazard level, exposure and expected fatalities hazard_filename = '%s/jakarta_flood_category_123.asc' % HAZDATA exposure_filename = '%s/Population_Jakarta_geographic.asc' % TESTDATA # Calculate impact using API hazard_layer = read_layer(hazard_filename) exposure_layer = read_layer(exposure_filename) plugin_name = 'Classified Hazard Population Impact Function' impact_function = get_plugin(plugin_name) # Call calculation engine impact_layer = calculate_impact( layers=[hazard_layer, exposure_layer], impact_fcn=impact_function) impact_filename = impact_layer.get_filename() impact_layer = read_layer(impact_filename) keywords = impact_layer.get_keywords() impact_summary = keywords['impact_summary'] # This is the expected number # Akbar: I check the exposure data ( # Population_Jakarta_geographic.as), the total population is # 355bio. This results makes sense then # Total Population Affected - 99,219,000 # Population in High risk areas - 38,940,000 # Population in Medium risk areas - 29,341,000 # Population in Low risk areas - 30,939,000 # Population Not Affected - 256,770,000 message = 'Result not as expected' self.assertTrue(format_int(99219000) in impact_summary, message) self.assertTrue(format_int(38940000) in impact_summary, message) self.assertTrue(format_int(29341000) in impact_summary, message) self.assertTrue(format_int(30939000) in impact_summary, message) self.assertTrue(format_int(256770000) in impact_summary, message)
def test_classified_hazard_population_impact_function(self): """Categorised Hazard Population IF works as expected.""" # Name file names for hazard level, exposure and expected fatalities hazard_filename = '%s/jakarta_flood_category_123.asc' % HAZDATA exposure_filename = '%s/Population_Jakarta_geographic.asc' % TESTDATA # Calculate impact using API hazard_layer = read_layer(hazard_filename) exposure_layer = read_layer(exposure_filename) plugin_name = 'Classified Hazard Population Impact Function' impact_function = get_plugin(plugin_name) # Call calculation engine impact_layer = calculate_impact(layers=[hazard_layer, exposure_layer], impact_fcn=impact_function) impact_filename = impact_layer.get_filename() impact_layer = read_layer(impact_filename) keywords = impact_layer.get_keywords() impact_summary = keywords['impact_summary'] # This is the expected number # Akbar: I check the exposure data ( # Population_Jakarta_geographic.as), the total population is # 355bio. This results makes sense then # Total Population Affected - 99,219,000 # Population in High risk areas - 38,940,000 # Population in Medium risk areas - 29,341,000 # Population in Low risk areas - 30,939,000 # Population Not Affected - 256,770,000 message = 'Result not as expected' self.assertTrue(format_int(99219000) in impact_summary, message) self.assertTrue(format_int(38940000) in impact_summary, message) self.assertTrue(format_int(29341000) in impact_summary, message) self.assertTrue(format_int(30939000) in impact_summary, message) self.assertTrue(format_int(256770000) in impact_summary, message)
def test_flood_population_evacuation(self): """Flood population evacuation""" population = 'people_jakarta_clip.tif' flood_data = 'flood_jakarta_clip.tif' plugin_name = 'FloodEvacuationFunction' hazard_filename = os.path.join(TESTDATA, flood_data) exposure_filename = os.path.join(TESTDATA, population) # Calculate impact using API hazard_layer = read_layer(hazard_filename) exposure_layer = read_layer(exposure_filename) impact_function = get_plugin(plugin_name) # Call calculation engine impact_layer = calculate_impact(layers=[hazard_layer, exposure_layer], impact_fcn=impact_function) impact_filename = impact_layer.get_filename() impact_layer = read_layer(impact_filename) keywords = impact_layer.get_keywords() # print "keywords", keywords evacuated = float(keywords['evacuated']) total_needs_full = keywords['total_needs'] total_needs_weekly = OrderedDict([[x['table name'], x['amount']] for x in total_needs_full['weekly']]) total_needs_single = OrderedDict([[x['table name'], x['amount']] for x in total_needs_full['single']]) expected_evacuated = 63400 self.assertEqual(evacuated, expected_evacuated) self.assertEqual(total_needs_weekly['Rice [kg]'], 177520) self.assertEqual(total_needs_weekly['Family Kits'], 12680) self.assertEqual(total_needs_weekly['Drinking Water [l]'], 1109500) self.assertEqual(total_needs_weekly['Clean Water [l]'], 4247800) self.assertEqual(total_needs_single['Toilets'], 3170)