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_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_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_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_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_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_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_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 _get_impact_function(self, qgis_hazard, qgis_exposure, plugin_name, impact_parameters): """Helper to run impact function This method is based on safe.engine.test_engine.test_flood_population_evacuation """ # Calculate impact using API # hazard = read_layer(hazard_filename) # exposure = read_layer(exposure_filename) hazard = Wrapper(qgis_hazard, 'hazard', 'Hazard') exposure = Wrapper(qgis_exposure, 'exposure', 'Exposure') plugin_list = get_plugins(plugin_name) assert len(plugin_list) == 1 assert plugin_list[0].keys()[0] == plugin_name impact_function = plugin_list[0][plugin_name] # Set up impact_function extent hazard_extent = hazard.get_layer().extent() hazard_extent = [ hazard_extent.xMinimum(), hazard_extent.yMinimum(), hazard_extent.xMaximum(), hazard_extent.yMaximum() ] exposure_extent = exposure.get_layer().extent() exposure_extent = [ exposure_extent.xMinimum(), exposure_extent.yMinimum(), exposure_extent.xMaximum(), exposure_extent.yMaximum() ] extent = [ max(hazard_extent[0], exposure_extent[0]), max(hazard_extent[1], exposure_extent[1]), min(hazard_extent[2], exposure_extent[2]), min(hazard_extent[3], exposure_extent[3]) ] impact_function.parameters = impact_parameters # Call calculation engine impact_layer = calculate_impact( layers=[hazard, exposure], impact_fcn=impact_function, extent=extent, check_integrity=False) impact_filename = impact_layer.get_filename() I = read_layer(impact_filename) return I
def run(self): """ Main function for hazard impact calculation thread. Requires three properties to be set before execution can take place: * Hazard layer - a path to a raster, * Exposure layer - a path to a vector points layer. * Function - a function that defines how the Hazard assessment will be computed. After the thread is complete, you can use the filename and result accessors to determine what the result of the analysis was:: calculator = ImpactCalculator() rasterPath = os.path.join(TESTDATA, 'xxx.asc') vector_path = os.path.join(TESTDATA, 'xxx.shp') calculator.setHazardLayer(self.rasterPath) calculator.setExposureLayer(self.vector_path) calculator.setFunction('Flood Building Impact Function') myRunner = calculator.getRunner() # wait till completion myRunner.join() myResult = myRunner.result() filename = myRunner.filename() :raises: InsufficientParametersError .. note:: a done signal is emitted when the analysis is complete. """ if (self._hazard_layer is None) or \ (self._exposure_layer is None) or \ (self._impact_function is None): message = self.tr( 'Ensure that hazard, exposure and function are all set before ' 'trying to run the analysis.') raise InsufficientParametersError(message) try: layers = [self._hazard_layer, self._exposure_layer] self._impact_layer = calculate_impact( layers=layers, impact_function=self._impact_function, extent=self._extent, check_integrity=self._check_integrity) except MemoryError, e: message = self.tr( 'An error occurred because it appears that your system does ' 'not have sufficient memory. Upgrading your computer so that ' 'it has more memory may help. Alternatively, consider using a ' 'smaller geographical area for your analysis, or using ' 'rasters with a larger cell size.') self._exception = e self._traceback = traceback.format_tb(sys.exc_info()[2]) self._result = message LOGGER.exception(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_calculate_impact(self): """Test calculating impact.""" eq_path = test_data_path('hazard', 'earthquake.tif') building_path = test_data_path('exposure', 'buildings.shp') eq_layer = read_layer(eq_path) building_layer = read_layer(building_path) impact_function = EarthquakeBuildingFunction.instance() impact_function.hazard = SafeLayer(eq_layer) impact_function.exposure = SafeLayer(building_layer) impact_layer = calculate_impact(impact_function) self.assertIsNotNone(impact_layer)
def direct_execution(): arg = AnalysisArguments.read_arguments() register_impact_functions() registry = ImpactFunctionManager().registry function = registry.get_instance(arg.impact_function_name) function.hazard = SafeLayer(read_layer(arg.hazard_filename)) function.exposure = SafeLayer(read_layer(arg.exposure_filename)) impact = calculate_impact(function) qgis_impact = safe_to_qgis_layer(impact) generate_styles(impact, qgis_impact) copy_impact_layer(impact, arg.impact_filename)
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)