def save_scenario(self): """Save current scenario to text file""" from safe.gui.tools.save_scenario import SaveScenarioDialog dialog = SaveScenarioDialog( iface=self.iface, dock=self.dock_widget) dialog.save_scenario()
def setUp(self): """Fixture run before all tests.""" os.environ['LANG'] = 'en' self.DOCK.show_only_visible_layers_flag = True load_standard_layers(self.DOCK) self.DOCK.hazard_layer_combo.setCurrentIndex(0) self.DOCK.exposure_layer_combo.setCurrentIndex(0) self.DOCK.run_in_thread_flag = False self.DOCK.show_only_visible_layers_flag = False self.DOCK.set_layer_from_title_flag = False self.DOCK.zoom_to_impact_flag = False self.DOCK.hide_exposure_flag = False # Create scenario dialog self.save_scenario_dialog = SaveScenarioDialog(IFACE, self.DOCK)
def setUp(self): """Fixture run before all tests.""" os.environ['LANG'] = 'en' DOCK.show_only_visible_layers_flag = True load_standard_layers(DOCK) DOCK.cboHazard.setCurrentIndex(0) DOCK.cboExposure.setCurrentIndex(0) DOCK.cboFunction.setCurrentIndex(0) DOCK.run_in_thread_flag = False DOCK.show_only_visible_layers_flag = False DOCK.set_layer_from_title_flag = False DOCK.zoom_to_impact_flag = False DOCK.hide_exposure_flag = False DOCK.show_intermediate_layers = False # Create scenario dialog self.save_scenario_dialog = SaveScenarioDialog(IFACE, DOCK)
def setUp(self): """Fixture run before all tests.""" os.environ['LANG'] = 'en' self.DOCK.show_only_visible_layers_flag = True load_standard_layers(self.DOCK) self.DOCK.hazard_layer_combo.setCurrentIndex(0) self.DOCK.exposure_layer_combo.setCurrentIndex(0) self.DOCK.run_in_thread_flag = False self.DOCK.show_only_visible_layers_flag = False self.DOCK.set_layer_from_title_flag = False self.DOCK.zoom_to_impact_flag = False self.DOCK.hide_exposure_flag = False # Check that the default layers have been loaded! # FIXME: this fails if the test is run inside QGIS3 # assert (self.DOCK.hazard_layer_combo.itemText(1) == 'Classified Flood') # Create scenario dialog self.save_scenario_dialog = SaveScenarioDialog(IFACE, self.DOCK)
class SaveScenarioTest(unittest.TestCase): """Test save scenario tool.""" def setUp(self): """Fixture run before all tests.""" os.environ['LANG'] = 'en' DOCK.show_only_visible_layers_flag = True load_standard_layers(DOCK) DOCK.cboHazard.setCurrentIndex(0) DOCK.cboExposure.setCurrentIndex(0) DOCK.cboFunction.setCurrentIndex(0) DOCK.run_in_thread_flag = False DOCK.show_only_visible_layers_flag = False DOCK.set_layer_from_title_flag = False DOCK.zoom_to_impact_flag = False DOCK.hide_exposure_flag = False DOCK.show_intermediate_layers = False # Create scenario dialog self.save_scenario_dialog = SaveScenarioDialog(IFACE, DOCK) # register impact functions register_impact_functions() self.impact_function_manager = ImpactFunctionManager() def tearDown(self): """Fixture run after each test""" # noinspection PyArgumentList QgsMapLayerRegistry.instance().removeAllMapLayers() DOCK.cboHazard.clear() DOCK.cboExposure.clear() # DOCK.cboAggregation.clear() #dont do this because the cboAggregation # need to be able to react to the status changes of the other combos self.save_scenario_dialog = None def test_validate_input(self): """Test validate input.""" # Valid Case result, message = setup_scenario( DOCK, hazard='Classified Flood', exposure='Population', function='Be affected by each hazard class', function_id='ClassifiedRasterHazardPopulationFunction') self.assertTrue(result, message) is_valid, message = self.save_scenario_dialog.validate_input() self.assertTrue(is_valid) self.assertIsNone(message) # Change the hazard layer to None self.save_scenario_dialog.dock.cboHazard.setCurrentIndex(-1) is_valid, message = self.save_scenario_dialog.validate_input() self.assertFalse(is_valid) self.assertIsNotNone(message) def test_save_scenario(self): """Test saving Current scenario.""" result, message = setup_scenario( DOCK, hazard='Classified Flood', exposure='Population', function='Be affected by each hazard class', function_id='ClassifiedRasterHazardPopulationFunction') self.assertTrue(result, message) # Enable on-the-fly reprojection set_canvas_crs(GEOCRS, True) set_jakarta_extent(dock=DOCK) # create unique file scenario_file = unique_filename( prefix='scenarioTest', suffix='.txt', dir=temp_dir('test')) self.save_scenario_dialog.save_scenario( scenario_file_path=scenario_file) with open(scenario_file) as f: data = f.readlines() title = data[0][:-1] exposure = data[1][:-1] hazard = data[2][:-1] function = data[3][:-1] extent = data[4][:-1] self.assertTrue( os.path.exists(scenario_file), 'File %s does not exist' % scenario_file) self.assertTrue( title == '[Classified Flood]', 'Title is not the same') self.assertTrue( exposure.startswith('exposure =') and exposure.endswith( 'pop_binary_raster_20_20.asc'), 'Exposure is not the same') self.assertTrue( hazard.startswith('hazard =') and hazard.endswith( 'classified_flood_20_20.asc'), 'Hazard is not the same') self.assertTrue( function == ( 'function = ClassifiedRasterHazardPopulationFunction'), 'Impact function is not same') # TODO: figure out why this changed between releases if qgis_version() < 20400: # For QGIS 2.0 expected_extent = ( 'extent = 106.313333, -6.380000, 107.346667, -6.070000') self.assertEqual(expected_extent, extent) else: # for QGIS 2.4 expected_extent = ( 'extent = 106.287500, -6.380000, 107.372500, -6.070000') self.assertEqual(expected_extent, expected_extent) def test_relative_path(self): """Test we calculate the relative paths correctly when saving scenario. """ result, message = setup_scenario( DOCK, hazard='Classified Flood', exposure='Population', function='Be affected by each hazard class', function_id='ClassifiedRasterHazardPopulationFunction') self.assertTrue(result, message) fake_dir = test_data_path() scenario_file = unique_filename( prefix='scenarioTest', suffix='.txt', dir=fake_dir) exposure_layer = str(DOCK.get_exposure_layer().publicSource()) hazard_layer = str(DOCK.get_hazard_layer().publicSource()) relative_exposure = self.save_scenario_dialog.relative_path( scenario_file, exposure_layer) relative_hazard = self.save_scenario_dialog.relative_path( scenario_file, hazard_layer) if 'win32' in sys.platform: # windows self.assertEqual( 'exposure\\pop_binary_raster_20_20.asc', relative_exposure) self.assertEqual( 'hazard\\classified_flood_20_20.asc', relative_hazard) else: self.assertEqual( 'exposure/pop_binary_raster_20_20.asc', relative_exposure) self.assertEqual( 'hazard/classified_flood_20_20.asc', relative_hazard)
class SaveScenarioTest(unittest.TestCase): """Test save scenario tool.""" @classmethod def setUpClass(cls): cls.DOCK = get_dock() def setUp(self): """Fixture run before all tests.""" os.environ['LANG'] = 'en' self.DOCK.show_only_visible_layers_flag = True load_standard_layers(self.DOCK) self.DOCK.hazard_layer_combo.setCurrentIndex(0) self.DOCK.exposure_layer_combo.setCurrentIndex(0) self.DOCK.run_in_thread_flag = False self.DOCK.show_only_visible_layers_flag = False self.DOCK.set_layer_from_title_flag = False self.DOCK.zoom_to_impact_flag = False self.DOCK.hide_exposure_flag = False # Create scenario dialog self.save_scenario_dialog = SaveScenarioDialog(IFACE, self.DOCK) def tearDown(self): """Fixture run after each test""" # noinspection PyArgumentList QgsMapLayerRegistry.instance().removeAllMapLayers() self.DOCK.hazard_layer_combo.clear() self.DOCK.exposure_layer_combo.clear() # self.DOCK.aggregation_layer_combo.clear() # #dont do this because the aggregation_layer_combo # need to be able to react to the status changes of the other combos self.save_scenario_dialog = None @unittest.expectedFailure def test_validate_input(self): """Test validate input.""" # Valid Case result, message = setup_scenario(self.DOCK, hazard='Classified Flood', exposure='Population') self.assertTrue(result, message) is_valid, message = self.save_scenario_dialog.validate_input() self.assertTrue(is_valid) self.assertIsNone(message) # Change the hazard layer to None self.save_scenario_dialog.dock.hazard_layer_combo.setCurrentIndex(-1) is_valid, message = self.save_scenario_dialog.validate_input() self.assertFalse(is_valid) self.assertIsNotNone(message) @unittest.expectedFailure def test_save_scenario(self): """Test saving Current scenario.""" result, message = setup_scenario(self.DOCK, hazard='Classified Flood', exposure='Population') self.assertTrue(result, message) # Enable on-the-fly reprojection set_canvas_crs(GEOCRS, True) set_jakarta_extent(dock=self.DOCK) # create unique file scenario_file = unique_filename(prefix='scenarioTest', suffix='.txt', dir=temp_dir('test')) self.save_scenario_dialog.save_scenario( scenario_file_path=scenario_file) with open(scenario_file) as f: data = f.readlines() title = data[0][:-1] exposure = data[1][:-1] hazard = data[2][:-1] extent = data[4][:-1] self.assertTrue(os.path.exists(scenario_file), 'File %s does not exist' % scenario_file) self.assertTrue(title == '[Classified Flood]', 'Title is not the same') self.assertTrue( exposure.startswith('exposure =') and exposure.endswith('pop_binary_raster_20_20.asc'), 'Exposure is not the same') self.assertTrue( hazard.startswith('hazard =') and hazard.endswith('classified_flood_20_20.asc'), 'Hazard is not the same') expected_extent = ( 'extent = 106.287500, -6.380000, 107.372500, -6.070000') self.assertEqual(expected_extent, expected_extent) @unittest.expectedFailure def test_relative_path(self): """Test we calculate the relative paths correctly when saving scenario. """ result, message = setup_scenario(self.DOCK, hazard='Classified Flood', exposure='Population') self.assertTrue(result, message) fake_dir = standard_data_path() scenario_file = unique_filename(prefix='scenarioTest', suffix='.txt', dir=fake_dir) exposure_layer = self.DOCK.get_exposure_layer().publicSource() hazard_layer = self.DOCK.get_hazard_layer().publicSource() relative_exposure = self.save_scenario_dialog.relative_path( scenario_file, exposure_layer) relative_hazard = self.save_scenario_dialog.relative_path( scenario_file, hazard_layer) if 'win32' in sys.platform: # windows self.assertEqual('exposure\\pop_binary_raster_20_20.asc', relative_exposure) self.assertEqual('hazard\\classified_flood_20_20.asc', relative_hazard) else: self.assertEqual('exposure/pop_binary_raster_20_20.asc', relative_exposure) self.assertEqual('hazard/classified_flood_20_20.asc', relative_hazard)
class SaveScenarioTest(unittest.TestCase): """Test save scenario tool.""" @classmethod def setUpClass(cls): cls.DOCK = get_dock() def setUp(self): """Fixture run before all tests.""" os.environ['LANG'] = 'en' self.DOCK.show_only_visible_layers_flag = True load_standard_layers(self.DOCK) self.DOCK.hazard_layer_combo.setCurrentIndex(0) self.DOCK.exposure_layer_combo.setCurrentIndex(0) self.DOCK.run_in_thread_flag = False self.DOCK.show_only_visible_layers_flag = False self.DOCK.set_layer_from_title_flag = False self.DOCK.zoom_to_impact_flag = False self.DOCK.hide_exposure_flag = False # Create scenario dialog self.save_scenario_dialog = SaveScenarioDialog(IFACE, self.DOCK) def tearDown(self): """Fixture run after each test""" # noinspection PyArgumentList QgsMapLayerRegistry.instance().removeAllMapLayers() self.DOCK.hazard_layer_combo.clear() self.DOCK.exposure_layer_combo.clear() # self.DOCK.aggregation_layer_combo.clear() # #dont do this because the aggregation_layer_combo # need to be able to react to the status changes of the other combos self.save_scenario_dialog = None @unittest.expectedFailure def test_validate_input(self): """Test validate input.""" # Valid Case result, message = setup_scenario( self.DOCK, hazard='Classified Flood', exposure='Population') self.assertTrue(result, message) is_valid, message = self.save_scenario_dialog.validate_input() self.assertTrue(is_valid) self.assertIsNone(message) # Change the hazard layer to None self.save_scenario_dialog.dock.hazard_layer_combo.setCurrentIndex(-1) is_valid, message = self.save_scenario_dialog.validate_input() self.assertFalse(is_valid) self.assertIsNotNone(message) @unittest.expectedFailure def test_save_scenario(self): """Test saving Current scenario.""" result, message = setup_scenario( self.DOCK, hazard='Classified Flood', exposure='Population') self.assertTrue(result, message) # Enable on-the-fly reprojection set_canvas_crs(GEOCRS, True) set_jakarta_extent(dock=self.DOCK) # create unique file scenario_file = unique_filename( prefix='scenarioTest', suffix='.txt', dir=temp_dir('test')) self.save_scenario_dialog.save_scenario( scenario_file_path=scenario_file) with open(scenario_file) as f: data = f.readlines() title = data[0][:-1] exposure = data[1][:-1] hazard = data[2][:-1] extent = data[4][:-1] self.assertTrue( os.path.exists(scenario_file), 'File %s does not exist' % scenario_file) self.assertTrue( title == '[Classified Flood]', 'Title is not the same') self.assertTrue( exposure.startswith('exposure =') and exposure.endswith( 'pop_binary_raster_20_20.asc'), 'Exposure is not the same') self.assertTrue( hazard.startswith('hazard =') and hazard.endswith( 'classified_flood_20_20.asc'), 'Hazard is not the same') expected_extent = ( 'extent = 106.287500, -6.380000, 107.372500, -6.070000') self.assertEqual(expected_extent, expected_extent) @unittest.expectedFailure def test_relative_path(self): """Test we calculate the relative paths correctly when saving scenario. """ result, message = setup_scenario( self.DOCK, hazard='Classified Flood', exposure='Population') self.assertTrue(result, message) fake_dir = standard_data_path() scenario_file = unique_filename( prefix='scenarioTest', suffix='.txt', dir=fake_dir) exposure_layer = self.DOCK.get_exposure_layer().publicSource() hazard_layer = self.DOCK.get_hazard_layer().publicSource() relative_exposure = self.save_scenario_dialog.relative_path( scenario_file, exposure_layer) relative_hazard = self.save_scenario_dialog.relative_path( scenario_file, hazard_layer) if 'win32' in sys.platform: # windows self.assertEqual( 'exposure\\pop_binary_raster_20_20.asc', relative_exposure) self.assertEqual( 'hazard\\classified_flood_20_20.asc', relative_hazard) else: self.assertEqual( 'exposure/pop_binary_raster_20_20.asc', relative_exposure) self.assertEqual( 'hazard/classified_flood_20_20.asc', relative_hazard)
class SaveScenarioTest(unittest.TestCase): """Test save scenario tool.""" def setUp(self): """Fixture run before all tests.""" os.environ['LANG'] = 'en' DOCK.show_only_visible_layers_flag = True load_standard_layers(DOCK) DOCK.cboHazard.setCurrentIndex(0) DOCK.cboExposure.setCurrentIndex(0) DOCK.cboFunction.setCurrentIndex(0) DOCK.run_in_thread_flag = False DOCK.show_only_visible_layers_flag = False DOCK.set_layer_from_title_flag = False DOCK.zoom_to_impact_flag = False DOCK.hide_exposure_flag = False DOCK.show_intermediate_layers = False # Create scenario dialog self.save_scenario_dialog = SaveScenarioDialog(IFACE, DOCK) # register impact functions register_impact_functions() self.impact_function_manager = ImpactFunctionManager() def tearDown(self): """Fixture run after each test""" # noinspection PyArgumentList QgsMapLayerRegistry.instance().removeAllMapLayers() DOCK.cboHazard.clear() DOCK.cboExposure.clear() # DOCK.cboAggregation.clear() #dont do this because the cboAggregation # need to be able to react to the status changes of the other combos self.save_scenario_dialog = None def test_validate_input(self): """Test validate input.""" # Valid Case result, message = setup_scenario( DOCK, hazard='Classified Flood', exposure='Population', function='Be affected in each hazard class', function_id='ClassifiedRasterHazardPopulationFunction') self.assertTrue(result, message) is_valid, message = self.save_scenario_dialog.validate_input() self.assertTrue(is_valid) self.assertIsNone(message) # Change the hazard layer to None self.save_scenario_dialog.dock.cboHazard.setCurrentIndex(-1) is_valid, message = self.save_scenario_dialog.validate_input() self.assertFalse(is_valid) self.assertIsNotNone(message) def test_save_scenario(self): """Test saving Current scenario.""" result, message = setup_scenario( DOCK, hazard='Classified Flood', exposure='Population', function='Be affected in each hazard class', function_id='ClassifiedRasterHazardPopulationFunction') self.assertTrue(result, message) # Enable on-the-fly reprojection set_canvas_crs(GEOCRS, True) set_jakarta_extent(dock=DOCK) # create unique file scenario_file = unique_filename(prefix='scenarioTest', suffix='.txt', dir=temp_dir('test')) self.save_scenario_dialog.save_scenario( scenario_file_path=scenario_file) with open(scenario_file) as f: data = f.readlines() title = data[0][:-1] exposure = data[1][:-1] hazard = data[2][:-1] function = data[3][:-1] extent = data[4][:-1] self.assertTrue(os.path.exists(scenario_file), 'File %s does not exist' % scenario_file) self.assertTrue(title == '[Classified Flood]', 'Title is not the same') self.assertTrue( exposure.startswith('exposure =') and exposure.endswith('pop_binary_raster_20_20.asc'), 'Exposure is not the same') self.assertTrue( hazard.startswith('hazard =') and hazard.endswith('classified_flood_20_20.asc'), 'Hazard is not the same') self.assertTrue( function == ( 'function = ClassifiedRasterHazardPopulationFunction'), 'Impact function is not same') # TODO: figure out why this changed between releases if qgis_version() < 20400: # For QGIS 2.0 expected_extent = ( 'extent = 106.313333, -6.380000, 107.346667, -6.070000') self.assertEqual(expected_extent, extent) else: # for QGIS 2.4 expected_extent = ( 'extent = 106.287500, -6.380000, 107.372500, -6.070000') self.assertEqual(expected_extent, expected_extent) def test_relative_path(self): """Test we calculate the relative paths correctly when saving scenario. """ result, message = setup_scenario( DOCK, hazard='Classified Flood', exposure='Population', function='Be affected in each hazard class', function_id='ClassifiedRasterHazardPopulationFunction') self.assertTrue(result, message) fake_dir = test_data_path() scenario_file = unique_filename(prefix='scenarioTest', suffix='.txt', dir=fake_dir) exposure_layer = str(DOCK.get_exposure_layer().publicSource()) hazard_layer = str(DOCK.get_hazard_layer().publicSource()) relative_exposure = self.save_scenario_dialog.relative_path( scenario_file, exposure_layer) relative_hazard = self.save_scenario_dialog.relative_path( scenario_file, hazard_layer) if 'win32' in sys.platform: # windows self.assertEqual('exposure\\pop_binary_raster_20_20.asc', relative_exposure) self.assertEqual('hazard\\classified_flood_20_20.asc', relative_hazard) else: self.assertEqual('exposure/pop_binary_raster_20_20.asc', relative_exposure) self.assertEqual('hazard/classified_flood_20_20.asc', relative_hazard)