def save_scenario(self): """Save current scenario to text file""" from safe_qgis.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' 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' 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)
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) 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='Flood in Jakarta', exposure='Penduduk Jakarta', function='Be impacted', function_id='Categorised Hazard Population Impact Function') 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='Flood in Jakarta', exposure='Penduduk Jakarta', function='Be impacted', function_id='Categorised Hazard Population Impact Function') self.assertTrue(result, message) # Enable on-the-fly reprojection set_canvas_crs(GEOCRS, True) set_jakarta_extent() # 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 == '[Flood in Jakarta]', 'Title is not the same') self.assertTrue( exposure.startswith('exposure =') and exposure.endswith( 'Population_Jakarta_geographic.asc'), 'Exposure is not the same') self.assertTrue( hazard.startswith('hazard =') and hazard.endswith( 'jakarta_flood_category_123.asc'), 'Hazard is not the same') self.assertTrue( function == ( 'function = Categorised Hazard Population Impact Function'), 'Impact function is not same') expected_extent = ( 'extent = 106.313333, -6.380000, 107.346667, -6.070000') self.assertEqual(expected_extent, extent) def test_relative_path(self): """Test we calculate the relative paths correctly when saving scenario. """ result, message = setup_scenario( DOCK, hazard='Flood in Jakarta', exposure='Penduduk Jakarta', function='Be impacted', function_id='Categorised Hazard Population Impact Function') self.assertTrue(result, message) fake_dir = os.path.dirname(TESTDATA) 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( 'test\\Population_Jakarta_geographic.asc', relative_exposure) self.assertEqual( 'hazard\\jakarta_flood_category_123.asc', relative_hazard) else: self.assertEqual( 'test/Population_Jakarta_geographic.asc', relative_exposure) self.assertEqual( 'hazard/jakarta_flood_category_123.asc', relative_hazard)
def save_scenario(self): """Save current scenario to text file""" from safe_qgis.tools.save_scenario import SaveScenarioDialog dialog = SaveScenarioDialog(iface=self.iface, dock=self.dock_widget) dialog.save_scenario()
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) 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='Flood in Jakarta', exposure='Penduduk Jakarta', function='Be impacted', function_id='Categorised Hazard Population Impact Function') 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='Flood in Jakarta', exposure='Penduduk Jakarta', function='Be impacted', function_id='Categorised Hazard Population Impact Function') 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 == '[Flood in Jakarta]', 'Title is not the same') self.assertTrue( exposure.startswith('exposure =') and exposure.endswith( 'Population_Jakarta_geographic.asc'), 'Exposure is not the same') self.assertTrue( hazard.startswith('hazard =') and hazard.endswith( 'jakarta_flood_category_123.asc'), 'Hazard is not the same') self.assertTrue( function == ( 'function = Categorised Hazard Population Impact Function'), '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='Flood in Jakarta', exposure='Penduduk Jakarta', function='Be impacted', function_id='Categorised Hazard Population Impact Function') self.assertTrue(result, message) fake_dir = os.path.dirname(TESTDATA) 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( 'test\\Population_Jakarta_geographic.asc', relative_exposure) self.assertEqual( 'hazard\\jakarta_flood_category_123.asc', relative_hazard) else: self.assertEqual( 'test/Population_Jakarta_geographic.asc', relative_exposure) self.assertEqual( 'hazard/jakarta_flood_category_123.asc', relative_hazard)