def test_issue581(self): """Test issue #581 in github - Humanize can produce IndexError : list index out of range """ # See https://github.com/AIFDR/inasafe/issues/581 result, message = setup_scenario( self.dock, hazard='Continuous Flood', exposure='Population', function='Need evacuation', function_id='FloodEvacuationRasterHazardFunction') self.assertTrue(result, message) # Enable on-the-fly reprojection set_canvas_crs(GEOCRS, True) set_small_jakarta_extent(self.dock) # Press RUN self.dock.accept() result = self.dock.wvResults.page().currentFrame().toPlainText() message = 'Result not as expected: %s' % result self.assertTrue('IndexError' not in result, message) self.assertTrue( 'It appears that no Population are affected by Continuous Flood.' in result, message)
def test_post_processor_output(self): """Check that the post processor does not add spurious report rows.""" # with KAB_NAME aggregation attribute defined in .keyword using # kabupaten_jakarta_singlepart.shp result, message = setup_scenario( self.DOCK, hazard='Continuous Flood', exposure='Population', function_id='FloodEvacuationRasterHazardFunction') # Enable on-the-fly reprojection set_canvas_crs(GEOCRS, True) set_jakarta_extent() assert result, message # Press RUN self.DOCK.accept() message = 'Spurious 0 filled rows added to post processing report.' result = self.DOCK.wvResults.page().currentFrame().toPlainText() for line in result.split('\n'): if 'Entire area' in line: tokens = str(line).split('\t') tokens = tokens[1:] total = 0 for token in tokens: try: total += float(token.replace(',', '')) except ValueError: total += float(token[0]) assert total != 0, message
def test_issue1191(self): """Test setting a layer's title in the kw directly from qgis api""" DOCK.set_layer_from_title_flag = True set_canvas_crs(GEOCRS, True) set_yogya_extent(DOCK) result, message = setup_scenario( DOCK, hazard='An earthquake in Yogyakarta like in 2006', exposure='OSM Building Polygons', function='Be affected', function_id='Earthquake Building Impact Function') self.assertTrue(result, message) layer = DOCK.get_hazard_layer() keyword_io = KeywordIO() original_title = 'An earthquake in Yogyakarta like in 2006' title = keyword_io.read_keywords(layer, 'title') self.assertEqual(title, original_title) # change layer name as if done in the legend expected_title = 'TEST' layer.setLayerName(expected_title) title = keyword_io.read_keywords(layer, 'title') self.assertEqual(title, expected_title) # reset KW file to original state layer.setLayerName(original_title) title = keyword_io.read_keywords(layer, 'title') self.assertEqual(title, original_title) DOCK.set_layer_from_title_flag = False
def test_user_defined_extent(self): """Test that analysis honours user defined extents. Note that when testing on a desktop system this will overwrite your user defined analysis extent. """ settings = QtCore.QSettings() extents = '106.772279, -6.237576, 106.885165, -6.165415' settings.setValue('inasafe/analysis_extent', extents) settings.setValue('inasafe/analysis_extent_crs', 'EPSG:4326') DOCK.read_settings() setup_scenario( DOCK, hazard='A flood in Jakarta like in 2007', exposure='People', function='Need evacuation', function_id='Flood Evacuation Function', aggregation_layer='kabupaten jakarta singlepart', aggregation_enabled_flag=True) DOCK.extent.show_rubber_bands = True expected_vertex_count = 5 # 4326 with disabled on-the-fly reprojection set_canvas_crs(GEOCRS, True) # User extent should override this set_small_jakarta_extent(DOCK) DOCK.extent.show_user_analysis_extent() user_band = DOCK.extent.user_analysis_rubberband self.assertEqual(expected_vertex_count, user_band.numberOfVertices())
def test_preprocessing(self): """Preprocessing results are correct.""" # TODO - this needs to be fixed post dock refactor. layer_path = standard_data_path('hazard', 'flood_polygon_crosskabupaten.shp') # See qgis project in test data: vector_preprocessing_test.qgs # add additional layers file_list = [layer_path] load_layers(file_list, clear_flag=False) result, message = setup_scenario( self.DOCK, hazard='Flood Polygon Cross Kabupaten', exposure='Population', function_id='FloodEvacuationVectorHazardFunction', aggregation_layer=u"Dístríct's of Jakarta", aggregation_enabled_flag=True) assert result, message # Enable on-the-fly reprojection set_canvas_crs(GEOCRS, True) set_jakarta_extent(dock=self.DOCK) # Press RUN self.DOCK.accept() expected_feature_count = 5 aggregator = self.DOCK.impact_function.aggregator message = ( 'The preprocessing should have generated %s features, ' 'found %s' % (expected_feature_count, aggregator.preprocessed_feature_count)) self.assertEqual(expected_feature_count, aggregator.preprocessed_feature_count, message)
def test_issue581(self): """Test issue #581 in github - Humanize can produce IndexError : list index out of range """ # See https://github.com/AIFDR/inasafe/issues/581 result, message = setup_scenario( DOCK, hazard='A flood in Jakarta like in 2007', exposure='People', function='Need evacuation', function_id='Flood Evacuation Function') self.assertTrue(result, message) # Enable on-the-fly reprojection set_canvas_crs(GEOCRS, True) set_small_jakarta_extent(DOCK) # Press RUN DOCK.accept() result = DOCK.wvResults.page().currentFrame().toPlainText() message = 'Result not as expected: %s' % result self.assertTrue('IndexError' not in result, message) self.assertTrue( 'It appears that no People are affected by A flood in ' 'Jakarta like in 2007. You may want to consider:' in result)
def xtest_print_map(self): """Test print map, especially on Windows.""" result, message = setup_scenario( DOCK, hazard='Flood in Jakarta', exposure='Essential buildings', function='Be affected', function_id='Categorised Hazard Building Impact Function') self.assertTrue(result, message) # Enable on-the-fly reprojection set_canvas_crs(GEOCRS, True) set_jakarta_extent(DOCK) # Press RUN button = DOCK.pbnRunStop # noinspection PyCallByClass,PyTypeChecker button.click() print_button = DOCK.pbnPrint try: # noinspection PyCallByClass,PyTypeChecker print_button.click() except OSError: LOGGER.debug('OSError') # pass except Exception, e: raise Exception('Exception is not expected, %s' % e)
def test_result_styling(self): """Test that colours and opacity from a model are correctly styled.""" settings = QtCore.QSettings() settings.setValue('inasafe/analysis_extents_mode', 'HazardExposure') result, message = setup_scenario( self.dock, hazard='Continuous Flood', exposure='Population', function='Need evacuation', function_id='FloodEvacuationRasterHazardFunction') self.assertTrue(result, message) # Enable on-the-fly reprojection set_canvas_crs(GEOCRS, True) set_jakarta_extent(self.dock) self.dock.accept() safe_layer = self.dock.impact_function.impact qgis_layer = read_impact_layer(safe_layer) style = safe_layer.get_style_info() setRasterStyle(qgis_layer, style) # simple test for now - we could test explicitly for style state # later if needed. message = ('Raster layer was not assigned a Singleband pseudocolor ' 'renderer as expected.') self.assertEquals(qgis_layer.renderer().type(), 'singlebandpseudocolor', message)
def test_issue306(self): """Issue306: CANVAS doesnt add generated layers in tests. See https://github.com/AIFDR/inasafe/issues/306 """ settings = QtCore.QSettings() settings.setValue('inasafe/analysis_extents_mode', 'HazardExposure') result, message = setup_scenario( self.dock, hazard='Continuous Flood', exposure='Population', function='Need evacuation', function_id='FloodEvacuationRasterHazardFunction') self.assertTrue(result, message) # LOGGER.info("Canvas list before:\n%s" % canvas_list()) # Enable on-the-fly reprojection set_canvas_crs(GOOGLECRS, True) set_jakarta_google_extent(self.dock) before_count = len(CANVAS.layers()) # Press RUN self.dock.accept() # test issue #306 after_count = len(CANVAS.layers()) # LOGGER.info("Canvas list after:\n%s" % canvas_list()) message = ('Layer was not added to canvas (%s before, %s after)' % (before_count, after_count)) # print 'After count %s' % after_count self.assertEqual(before_count, after_count - 1, message)
def xtest_print_map(self): """Test print map, especially on Windows.""" settings = QtCore.QSettings() settings.setValue('inasafe/analysis_extents_mode', 'HazardExposure') result, message = setup_scenario( self.dock, hazard='Classified Flood', exposure='Buildings', function='Be impacted in each hazard class', function_id='ClassifiedRasterHazardBuildingFunction') # Enable on-the-fly reprojection set_canvas_crs(GEOCRS, True) set_jakarta_extent(self.dock) self.assertTrue(result, message) # Press RUN button = self.dock.pbnRunStop # noinspection PyCallByClass,PyTypeChecker button.click() print_button = self.dock.pbnPrint try: # noinspection PyCallByClass,PyTypeChecker print_button.click() except OSError: LOGGER.debug('OSError') # pass except Exception, e: raise Exception('Exception is not expected, %s' % e)
def test_full_run_pyzstats(self): """Aggregation results correct using our own python zonal stats code. """ result, message = setup_scenario( DOCK, hazard='Continuous Flood', exposure='Population', function='Need evacuation', function_id='FloodEvacuationRasterHazardFunction', aggregation_layer=u'D\xedstr\xedct\'s of Jakarta', aggregation_enabled_flag=True) self.assertTrue(result, message) # Enable on-the-fly reprojection set_canvas_crs(GEOCRS, True) set_jakarta_extent(DOCK) # Press RUN # noinspection PyCallByClass,PyTypeChecker DOCK.accept() result = DOCK.wvResults.page_to_text() control_file_path = test_data_path('control', 'files', 'test-full-run-results.txt') expected_result = codecs.open(control_file_path, mode='r', encoding='utf-8').readlines() result = result.replace('</td> <td>', ' ').replace('</td><td>', ' ') for line in expected_result: line = line.replace('\n', '') self.assertIn(line, result)
def test_user_defined_extent(self): """Test that analysis honours user defined extents. Note that when testing on a desktop system this will overwrite your user defined analysis extent. """ settings = QtCore.QSettings() extents = '106.772279, -6.237576, 106.885165, -6.165415' settings.setValue('inasafe/analysis_extent', extents) settings.setValue('inasafe/analysis_extent_crs', 'EPSG:4326') DOCK.read_settings() setup_scenario(DOCK, hazard='A flood in Jakarta like in 2007', exposure='People', function='Need evacuation', function_id='Flood Evacuation Function', aggregation_layer='kabupaten jakarta singlepart', aggregation_enabled_flag=True) DOCK.extent.show_rubber_bands = True expected_vertex_count = 5 # 4326 with disabled on-the-fly reprojection set_canvas_crs(GEOCRS, True) # User extent should override this set_small_jakarta_extent(DOCK) DOCK.extent.show_user_analysis_extent() user_band = DOCK.extent.user_analysis_rubberband self.assertEqual(expected_vertex_count, user_band.numberOfVertices())
def test_result_styling(self): """Test that colours and opacity from a model are correctly styled.""" settings = QtCore.QSettings() settings.setValue('inasafe/analysis_extents_mode', 'HazardExposure') result, message = setup_scenario( self.dock, hazard='Continuous Flood', exposure='Population', function='Need evacuation', function_id='FloodEvacuationRasterHazardFunction') self.assertTrue(result, message) # Enable on-the-fly reprojection set_canvas_crs(GEOCRS, True) set_jakarta_extent(self.dock) self.dock.accept() safe_layer = self.dock.impact_function.impact qgis_layer = read_impact_layer(safe_layer) style = safe_layer.get_style_info() setRasterStyle(qgis_layer, style) # simple test for now - we could test explicitly for style state # later if needed. message = ( 'Raster layer was not assigned a Singleband pseudocolor ' 'renderer as expected.') self.assertEquals( qgis_layer.renderer().type(), 'singlebandpseudocolor', message)
def test_result_styling(self): """Test that ouputs from a model are correctly styled (colours and opacity. """ # Push OK with the left mouse button print '--------------------' print combos_to_string(DOCK) result, message = setup_scenario( DOCK, hazard='A flood in Jakarta like in 2007', exposure='People', function='Need evacuation', function_id='Flood Evacuation Function') self.assertTrue(result, message) # Enable on-the-fly reprojection set_canvas_crs(GEOCRS, True) set_jakarta_extent(DOCK) DOCK.accept() # DOCK.analysis.get_impact_layer() safe_layer = DOCK.analysis.get_impact_layer() qgis_layer = read_impact_layer(safe_layer) style = safe_layer.get_style_info() setRasterStyle(qgis_layer, style) # simple test for now - we could test explicity for style state # later if needed. message = ('Raster layer was not assigned a Singleband pseudocolor' ' renderer as expected.') self.assertTrue( qgis_layer.renderer().type() == 'singlebandpseudocolor', message)
def test_issue1191(self): """Test setting a layer's title in the kw directly from qgis api""" settings = QtCore.QSettings() settings.setValue( 'inasafe/analysis_extents_mode', 'HazardExposure') self.dock.set_layer_from_title_flag = True set_canvas_crs(GEOCRS, True) set_yogya_extent(self.dock) result, message = setup_scenario( self.dock, hazard='Earthquake', exposure='Buildings', function='Be affected', function_id='EarthquakeBuildingFunction') self.assertTrue(result, message) layer = self.dock.get_hazard_layer() keyword_io = KeywordIO() original_title = 'Earthquake' title = keyword_io.read_keywords(layer, 'title') self.assertEqual(title, original_title) # change layer name as if done in the legend expected_title = 'TEST' layer.setLayerName(expected_title) title = keyword_io.read_keywords(layer, 'title') self.assertEqual(title, expected_title) # reset KW file to original state layer.setLayerName(original_title) title = keyword_io.read_keywords(layer, 'title') self.assertEqual(title, original_title) self.dock.set_layer_from_title_flag = False
def test_full_run_pyzstats(self): """Aggregation results correct using our own python zonal stats code. """ path = os.path.join(BOUNDDATA, 'kabupaten_jakarta.shp') file_list = [path] load_layers(file_list, clear_flag=False) result, message = setup_scenario( DOCK, hazard='A flood in Jakarta like in 2007', exposure='People', function='Need evacuation', function_id='Flood Evacuation Function', aggregation_layer='kabupaten jakarta', aggregation_enabled_flag=True) self.assertTrue(result, message) # Enable on-the-fly reprojection set_canvas_crs(GEOCRS, True) set_jakarta_extent(DOCK) # Press RUN # noinspection PyCallByClass,PyTypeChecker DOCK.accept() result = DOCK.wvResults.page_to_text() control_file_path = test_data_path('control', 'files', 'test-full-run-results.txt') expected_result = open(control_file_path, 'r').readlines() result = result.replace('</td> <td>', ' ').replace('</td><td>', ' ') for line in expected_result: line = line.replace('\n', '') self.assertIn(line, result)
def test_issue47(self): """Issue47: Hazard & exposure data are in different proj to viewport. See https://github.com/AIFDR/inasafe/issues/47""" result, message = setup_scenario( self.dock, hazard='Continuous Flood', exposure='Population', function='Need evacuation', function_id='FloodEvacuationRasterHazardFunction') self.assertTrue(result, message) # Enable on-the-fly reprojection set_canvas_crs(GOOGLECRS, True) set_jakarta_google_extent(self.dock) # Press RUN self.dock.accept() result = self.dock.wvResults.page_to_text() message = 'Result not as expected: %s' % result # searching for values 6700 clean water [l] in result self.assertTrue(format_int(6700) in result, message)
def test_issue306(self): """Issue306: CANVAS doesnt add generated layers in tests. See https://github.com/AIFDR/inasafe/issues/306""" result, message = setup_scenario( self.dock, hazard='Continuous Flood', exposure='Population', function='Need evacuation', function_id='FloodEvacuationRasterHazardFunction') self.assertTrue(result, message) LOGGER.info("Canvas list before:\n%s" % canvas_list()) # Enable on-the-fly reprojection set_canvas_crs(GOOGLECRS, True) set_jakarta_google_extent(self.dock) before_count = len(CANVAS.layers()) # print 'Before count %s' % before_count # Press RUN self.dock.accept() # test issue #306 after_count = len(CANVAS.layers()) LOGGER.info("Canvas list after:\n%s" % canvas_list()) message = ('Layer was not added to canvas (%s before, %s after)' % ( before_count, after_count)) # print 'After count %s' % after_count self.assertTrue(before_count == after_count - 1, message)
def xtest_print_map(self): """Test print map, especially on Windows.""" result, message = setup_scenario( self.dock, hazard='Flood in Jakarta', exposure='Essential buildings', function='Be affected', function_id='Categorised Hazard Building Impact Function') self.assertTrue(result, message) # Enable on-the-fly reprojection set_canvas_crs(GEOCRS, True) set_jakarta_extent(self.dock) # Press RUN button = self.dock.pbnRunStop # noinspection PyCallByClass,PyTypeChecker button.click() print_button = self.dock.pbnPrint try: # noinspection PyCallByClass,PyTypeChecker print_button.click() except OSError: LOGGER.debug('OSError') # pass except Exception, e: raise Exception('Exception is not expected, %s' % e)
def test_result_styling(self): """Test that colours and opacity from a model are correctly styled.""" # Push OK with the left mouse button print '--------------------' print combos_to_string(self.dock) result, message = setup_scenario( self.dock, hazard='Continuous Flood', exposure='Population', function='Need evacuation', function_id='FloodEvacuationRasterHazardFunction') self.assertTrue(result, message) # Enable on-the-fly reprojection set_canvas_crs(GEOCRS, True) set_jakarta_extent(self.dock) self.dock.accept() # self.dock.analysis.get_impact_layer() safe_layer = self.dock.analysis.impact_layer qgis_layer = read_impact_layer(safe_layer) style = safe_layer.get_style_info() setRasterStyle(qgis_layer, style) # simple test for now - we could test explicity for style state # later if needed. message = ( 'Raster layer was not assigned a Singleband pseudocolor' ' renderer as expected.') self.assertTrue( qgis_layer.renderer().type() == 'singlebandpseudocolor', message)
def test_user_defined_extent(self): """Test that analysis honours user defined extents. Note that when testing on a desktop system this will overwrite your user defined analysis extent. """ settings = QtCore.QSettings() extents = '106.772279, -6.237576, 106.885165, -6.165415' settings.setValue('inasafe/analysis_extent', extents) settings.setValue('inasafe/analysis_extent_crs', 'EPSG:4326') self.dock.read_settings() setup_scenario( self.dock, hazard='Continuous Flood', exposure='Population', function='Need evacuation', function_id='FloodEvacuationRasterHazardFunction', aggregation_layer=u'D\xedstr\xedct\'s of Jakarta', aggregation_enabled_flag=True) self.dock.extent.show_rubber_bands = True expected_vertex_count = 2 # 4326 with disabled on-the-fly reprojection set_canvas_crs(GEOCRS, True) # User extent should override this set_small_jakarta_extent(self.dock) self.dock.extent.show_user_analysis_extent() user_band = self.dock.extent.user_analysis_rubberband self.assertEqual(expected_vertex_count, user_band.numberOfVertices())
def test_insufficient_overlap(self): """Test Insufficient overlap errors are caught. ..note:: See https://github.com/AIFDR/inasafe/issues/372 """ # Push OK with the left mouse button button = self.dock.pbnRunStop message = 'Run button was not enabled' self.assertTrue(button.isEnabled(), message) set_jakarta_extent(self.dock) result, message = setup_scenario( self.dock, hazard='Continuous Flood', exposure='Population', function='Need evacuation', function_id='FloodEvacuationRasterHazardFunction') self.assertTrue(result, message) # Enable on-the-fly reprojection set_canvas_crs(GEOCRS, True) # Zoom to an area where there is no overlap with layers rectangle = QgsRectangle(106.849, -6.153, 106.866, -6.134) CANVAS.setExtent(rectangle) crs = QgsCoordinateReferenceSystem('EPSG:4326') self.dock.define_user_analysis_extent(rectangle, crs) self.dock.show_next_analysis_extent() # Check that run button is disabled because extents do not overlap message = 'Run button was not disabled' self.assertFalse(button.isEnabled(), message)
def test_user_defined_extent(self): """Test that analysis honours user defined extents. Note that when testing on a desktop system this will overwrite your user defined analysis extent. """ settings = QtCore.QSettings() extent = ('POLYGON ((' '106.772279 -6.237576, ' '106.772279 -6.165415, ' '106.885165 -6.165415, ' '106.885165 -6.237576, ' '106.772279 -6.237576' '))') settings.setValue('inasafe/user_extent', extent) settings.setValue('inasafe/user_extent_crs', 'EPSG:4326') self.dock.read_settings() setup_scenario(self.dock, hazard='Continuous Flood', exposure='Population', aggregation_layer=u'D\xedstr\xedct\'s of Jakarta', aggregation_enabled_flag=True) self.dock.extent.show_rubber_bands = True expected_vertex_count = 2 # 4326 with disabled on-the-fly reprojection set_canvas_crs(GEOCRS, True) # User extent should override this set_small_jakarta_extent(self.dock) self.dock.extent.display_user_extent() user_band = self.dock.extent._user_analysis_rubberband self.assertEqual(expected_vertex_count, user_band.numberOfVertices())
def test_issue306(self): """Issue306: CANVAS doesnt add generated layers in tests. See https://github.com/AIFDR/inasafe/issues/306""" result, message = setup_scenario( DOCK, hazard='A flood in Jakarta like in 2007', exposure='Penduduk Jakarta', function='Need evacuation', function_id='Flood Evacuation Function') self.assertTrue(result, message) LOGGER.info("Canvas list before:\n%s" % canvas_list()) # Enable on-the-fly reprojection set_canvas_crs(GOOGLECRS, True) set_jakarta_google_extent(DOCK) before_count = len(CANVAS.layers()) # print 'Before count %s' % before_count # Press RUN DOCK.accept() # test issue #306 after_count = len(CANVAS.layers()) LOGGER.info("Canvas list after:\n%s" % canvas_list()) message = ('Layer was not added to canvas (%s before, %s after)' % (before_count, after_count)) # print 'After count %s' % after_count self.assertTrue(before_count == after_count - 1, message)
def test_issue160(self): """Test that multipart features can be used in a scenario - issue #160 """ exposure = test_data_path('exposure', 'buildings.shp') hazard = test_data_path('hazard', 'flood_multipart_polygons.shp') # See https://github.com/AIFDR/inasafe/issues/71 # Push OK with the left mouse button # print 'Using QGIS: %s' % qgis_version() self.tearDown() button = DOCK.pbnRunStop # First part of scenario should have enabled run file_list = [hazard, exposure] hazard_layer_count, exposure_layer_count = load_layers(file_list) message = ( 'Incorrect number of Hazard layers: expected 1 got %s' % hazard_layer_count) self.assertTrue(hazard_layer_count == 1, message) message = ( 'Incorrect number of Exposure layers: expected 1 got %s' % exposure_layer_count) self.assertTrue(exposure_layer_count == 1, message) message = 'Run button was not enabled' self.assertTrue(button.isEnabled(), message) # Second part of scenario - run disabled when adding invalid layer # and select it - run should be disabled path = os.path.join(TESTDATA, 'issue71.tif') file_list = [path] # This layer has incorrect keywords clear_flag = False _, _ = load_layers(file_list, clear_flag) result, message = setup_scenario( DOCK, hazard='Flood Polygon', exposure='Buildings', function='Be flooded', function_id='FloodPolygonBuildingFunction') self.assertTrue(result, message) # Enable on-the-fly reprojection set_canvas_crs(GEOCRS, True) expected_extent = QgsRectangle( 106.80801, -6.19531, 106.83456946836641, -6.167526) CANVAS.setExtent(expected_extent) crs = QgsCoordinateReferenceSystem('EPSG:4326') DOCK.define_user_analysis_extent(expected_extent, crs) # Press RUN # noinspection PyCallByClass,PyCallByClass,PyTypeChecker DOCK.accept() result = DOCK.wvResults.page_to_text() message = 'Result not as expected: %s' % result self.assertTrue(format_int(33) in result, message)
def test_issue160(self): """Test that multipart features can be used in a scenario - issue #160 """ exposure = test_data_path('exposure', 'buildings.shp') hazard = test_data_path('hazard', 'flood_multipart_polygons.shp') # See https://github.com/AIFDR/inasafe/issues/71 # Push OK with the left mouse button # print 'Using QGIS: %s' % qgis_version() self.tearDown() button = DOCK.pbnRunStop # First part of scenario should have enabled run file_list = [hazard, exposure] hazard_layer_count, exposure_layer_count = load_layers(file_list) message = ('Incorrect number of Hazard layers: expected 1 got %s' % hazard_layer_count) self.assertTrue(hazard_layer_count == 1, message) message = ('Incorrect number of Exposure layers: expected 1 got %s' % exposure_layer_count) self.assertTrue(exposure_layer_count == 1, message) message = 'Run button was not enabled' self.assertTrue(button.isEnabled(), message) # Second part of scenario - run disabled when adding invalid layer # and select it - run should be disabled path = os.path.join(TESTDATA, 'issue71.tif') file_list = [path] # This layer has incorrect keywords clear_flag = False _, _ = load_layers(file_list, clear_flag) result, message = setup_scenario( DOCK, hazard='Flood Polygon', exposure='Buildings', function='Be flooded', function_id='FloodPolygonBuildingFunction') self.assertTrue(result, message) # Enable on-the-fly reprojection set_canvas_crs(GEOCRS, True) expected_extent = QgsRectangle(106.80801, -6.19531, 106.83456946836641, -6.167526) CANVAS.setExtent(expected_extent) crs = QgsCoordinateReferenceSystem('EPSG:4326') DOCK.define_user_analysis_extent(expected_extent, crs) # Press RUN # noinspection PyCallByClass,PyCallByClass,PyTypeChecker DOCK.accept() result = DOCK.wvResults.page_to_text() message = 'Result not as expected: %s' % result self.assertTrue(format_int(33) in result, 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_save_scenario(self): """Test saving Current scenario.""" result, message = setup_scenario( self.DOCK, hazard='Classified Flood', exposure='Population', function='Be affected', function_id='ClassifiedRasterHazardPopulationFunction') 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] 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_full_run_qgszstats(self): """Aggregation results are correct using native QGIS zonal stats. .. note:: We know this is going to fail (hence the decorator) as QGIS1.8 zonal stats are broken. We expect this to pass when we have ported to the QGIS 2.0 api at which time we can remove the decorator. TS July 2013 """ # TODO check that the values are similar enough to the python stats result, message = setup_scenario( self.dock, hazard='Continuous Flood', exposure='Population', function='Need evacuation', function_id='FloodEvacuationRasterHazardFunction', aggregation_layer=u'D\xedstr\xedct\'s of Jakarta', aggregation_enabled_flag=True) self.assertTrue(result, message) # Enable on-the-fly reprojection set_canvas_crs(GEOCRS, True) set_jakarta_extent(self.dock) # Press RUN # noinspection PyCallByClass,PyTypeChecker # use QGIS zonal stats only in the test qgis_zonal_flag = bool(QtCore.QSettings().value( 'inasafe/use_native_zonal_stats', False, type=bool)) QtCore.QSettings().setValue('inasafe/use_native_zonal_stats', True) self.dock.accept() QtCore.QSettings().setValue('inasafe/use_native_zonal_stats', qgis_zonal_flag) result = self.dock.wvResults.page_to_text() control_file_path = test_data_path( 'control', 'files', 'test-full-run-results-qgis.txt') expected_result = codecs.open( control_file_path, mode='r', encoding='utf-8').readlines() result = result.replace( '</td> <td>', ' ').replace('</td><td>', ' ') result = result.replace( '<th class="text-right">', ' ').replace('</th>', ' ') result = result.replace( '</td><td class="text-right">', ' ') for line in expected_result: line = line.replace('\n', '') self.assertIn(line, result)
def test_has_parameters_button_disabled(self): """Function configuration button is disabled when layers not compatible.""" set_canvas_crs(GEOCRS, True) setup_scenario(self.dock, hazard='Earthquake', exposure='Roads', function='', function_id='') tool_button = self.dock.toolFunctionOptions flag = tool_button.isEnabled() self.assertTrue( not flag, 'Expected configuration options button to be disabled')
def test_issue317(self): """Points near the edge of a raster hazard layer are interpolated OK""" set_canvas_crs(GEOCRS, True) set_jakarta_extent(self.dock) result, message = setup_scenario( self.dock, hazard='Continuous Flood', exposure='Buildings', function='Be flooded', function_id='FloodRasterBuildingFunction') self.dock.get_functions() self.assertTrue(result, message)
def test_issue317(self): """Points near the edge of a raster hazard layer are interpolated OK""" set_canvas_crs(GEOCRS, True) set_jakarta_extent(DOCK) result, message = setup_scenario( DOCK, hazard='A flood in Jakarta like in 2007', exposure='OSM Building Polygons', function='Be flooded', function_id='Flood Raster Building Impact Function') DOCK.get_functions() self.assertTrue(result, message)
def test_issue317(self): """Points near the edge of a raster hazard layer are interpolated OK""" set_canvas_crs(GEOCRS, True) set_jakarta_extent(DOCK) result, message = setup_scenario( DOCK, hazard='A flood in Jakarta like in 2007', exposure='OSM Building Polygons', function='Be flooded', function_id='Flood Building Impact Function') DOCK.get_functions() self.assertTrue(result, message)
def test_has_parameters_button_disabled(self): """Function configuration button is disabled when layers not compatible.""" set_canvas_crs(GEOCRS, True) setup_scenario(DOCK, hazard='An earthquake in Yogyakarta like in 2006', exposure='roads Maumere', function='', function_id='') tool_button = DOCK.toolFunctionOptions flag = tool_button.isEnabled() self.assertTrue( not flag, 'Expected configuration options button to be disabled')
def test_full_run_qgszstats(self): """Aggregation results are correct using native QGIS zonal stats. .. note:: We know this is going to fail (hence the decorator) as QGIS1.8 zonal stats are broken. We expect this to pass when we have ported to the QGIS 2.0 api at which time we can remove the decorator. TS July 2013 """ # TODO check that the values are similar enough to the python stats path = os.path.join(BOUNDDATA, 'kabupaten_jakarta.shp') file_list = [path] load_layers(file_list, clear_flag=False) result, message = setup_scenario( DOCK, hazard='A flood in Jakarta like in 2007', exposure='People', function='Need evacuation', function_id='Flood Evacuation Function', aggregation_layer='kabupaten jakarta', aggregation_enabled_flag=True) self.assertTrue(result, message) # Enable on-the-fly reprojection set_canvas_crs(GEOCRS, True) set_jakarta_extent(DOCK) # Press RUN # noinspection PyCallByClass,PyTypeChecker # use QGIS zonal stats only in the test qgis_zonal_flag = bool(QtCore.QSettings().value( 'inasafe/use_native_zonal_stats', False, type=bool)) QtCore.QSettings().setValue('inasafe/use_native_zonal_stats', True) DOCK.accept() QtCore.QSettings().setValue('inasafe/use_native_zonal_stats', qgis_zonal_flag) result = DOCK.wvResults.page_to_text() control_file_path = test_data_path( 'control', 'files', 'test-full-run-results-qgis.txt') expected_result = open(control_file_path, 'rb').readlines() result = result.replace( '</td> <td>', ' ').replace('</td><td>', ' ') for line in expected_result: line = line.replace('\n', '') self.assertIn(line, result)
def xtest_extents_changed(self): """Memory requirements are calculated correctly when extents change. """ set_canvas_crs(GEOCRS, True) set_jakarta_extent(DOCK) setup_scenario(DOCK, hazard='A flood in Jakarta like in 2007', exposure='Penduduk Jakarta', function='Need evacuation', function_id='Flood Evacuation Function') result = DOCK.checkMemoryUsage() message = 'Expected "3mb" to apear in : %s' % result self.assertTrue(result is not None, 'Check memory reported None') self.assertTrue('3mb' in result, message)
def test_has_parameters_button_enabled(self): """Function configuration button is enabled when layers are compatible. """ set_canvas_crs(GEOCRS, True) set_jakarta_extent(DOCK) setup_scenario(DOCK, hazard='A flood in Jakarta like in 2007', exposure='Penduduk Jakarta', function='Need evacuation', function_id='Flood Evacuation Function') tool_button = DOCK.toolFunctionOptions flag = tool_button.isEnabled() self.assertTrue(flag, 'Expected configuration options button to be enabled')
def test_rubber_bands(self): """Test that the rubber bands get updated.""" setup_scenario(DOCK, hazard='Continuous Flood', exposure='Population', function='Need evacuation', function_id='FloodEvacuationRasterHazardFunction', aggregation_layer=u'D\xedstr\xedct\'s of Jakarta', aggregation_enabled_flag=True) DOCK.extent.show_rubber_bands = True expected_vertex_count = 5 # 4326 with enabled on-the-fly reprojection - check next set_canvas_crs(GEOCRS, True) set_small_jakarta_extent(DOCK) DOCK.show_next_analysis_extent() next_band = DOCK.extent.next_analysis_rubberband self.assertEqual(expected_vertex_count, next_band.numberOfVertices()) # 4326 with disabled on-the-fly reprojection - check next set_canvas_crs(GEOCRS, False) set_small_jakarta_extent(DOCK) DOCK.show_next_analysis_extent() next_band = DOCK.extent.next_analysis_rubberband self.assertEqual(expected_vertex_count, next_band.numberOfVertices()) # 900913 with enabled on-the-fly reprojection - check next set_canvas_crs(GOOGLECRS, True) set_jakarta_google_extent(DOCK) next_band = DOCK.extent.next_analysis_rubberband self.assertEqual(expected_vertex_count, next_band.numberOfVertices()) # 900913 with enabled on-the-fly reprojection - check last set_canvas_crs(GOOGLECRS, True) set_jakarta_google_extent(DOCK) # Press RUN # noinspection PyCallByClass,PyTypeChecker DOCK.accept() # DOCK.show_extent() last_band = DOCK.extent.last_analysis_rubberband geometry = last_band.asGeometry().exportToWkt() expected = ( 'LINESTRING(11889533.87392097897827625 -691251.80866545776370913, ' '11893986.65355271473526955 -691251.80866545776370913, ' '11893986.65355271473526955 -686773.02196401008404791, ' '11889533.87392097897827625 -686773.02196401008404791, ' '11889533.87392097897827625 -691251.80866545776370913)') expected_list = self.wkt_to_coordinates(expected) actual_list = self.wkt_to_coordinates(geometry) for item in xrange(0, len(expected_list)): print item, expected_list[item], actual_list[item] self.assertAlmostEqual(expected_list[item][0], actual_list[item][0]) self.assertAlmostEqual(expected_list[item][1], actual_list[item][1]) self.assertEqual(expected_vertex_count, last_band.numberOfVertices())
def test_rubber_bands(self): """Test that the rubber bands get updated.""" setup_scenario(DOCK, hazard='A flood in Jakarta like in 2007', exposure='People', function='Need evacuation', function_id='Flood Evacuation Function', aggregation_layer='kabupaten jakarta singlepart', aggregation_enabled_flag=True) DOCK.extent.show_rubber_bands = True expected_vertex_count = 5 # 4326 with enabled on-the-fly reprojection - check next set_canvas_crs(GEOCRS, True) set_small_jakarta_extent(DOCK) DOCK.show_next_analysis_extent() next_band = DOCK.extent.next_analysis_rubberband self.assertEqual(expected_vertex_count, next_band.numberOfVertices()) # 4326 with disabled on-the-fly reprojection - check next set_canvas_crs(GEOCRS, False) set_small_jakarta_extent(DOCK) DOCK.show_next_analysis_extent() next_band = DOCK.extent.next_analysis_rubberband self.assertEqual(expected_vertex_count, next_band.numberOfVertices()) # 900913 with enabled on-the-fly reprojection - check next set_canvas_crs(GOOGLECRS, True) set_jakarta_google_extent(DOCK) next_band = DOCK.extent.next_analysis_rubberband self.assertEqual(expected_vertex_count, next_band.numberOfVertices()) # 900913 with enabled on-the-fly reprojection - check last set_canvas_crs(GOOGLECRS, True) set_jakarta_google_extent(DOCK) # Press RUN # noinspection PyCallByClass,PyTypeChecker DOCK.accept() # DOCK.show_extent() last_band = DOCK.extent.last_analysis_rubberband geometry = last_band.asGeometry().exportToWkt() expected = ( 'LINESTRING(11876228.33329810947179794 -695807.82839082507416606, ' '11908350.67106631398200989 -695807.82839082507416606, ' '11908350.67106631398200989 -678083.54461829655338079, ' '11876228.33329810947179794 -678083.54461829655338079, ' '11876228.33329810947179794 -695807.82839082507416606)') expected_list = self.wkt_to_coordinates(expected) actual_list = self.wkt_to_coordinates(geometry) for item in xrange(0, len(expected_list)): print item, expected_list[item], actual_list[item] self.assertAlmostEqual(expected_list[item][0], actual_list[item][0]) self.assertAlmostEqual(expected_list[item][1], actual_list[item][1]) self.assertEqual(expected_vertex_count, last_band.numberOfVertices())
def test_has_parameters_button_disabled(self): """Function configuration button is disabled when layers not compatible.""" set_canvas_crs(GEOCRS, True) setup_scenario( DOCK, hazard='An earthquake in Yogyakarta like in 2006', exposure='roads Maumere', function='', function_id='') tool_button = DOCK.toolFunctionOptions flag = tool_button.isEnabled() self.assertTrue( not flag, 'Expected configuration options button to be disabled')
def test_has_parameters_button_disabled(self): """Function configuration button is disabled when layers not compatible.""" set_canvas_crs(GEOCRS, True) setup_scenario( self.dock, hazard='Earthquake', exposure='Roads', function='', function_id='') tool_button = self.dock.toolFunctionOptions flag = tool_button.isEnabled() self.assertTrue( not flag, 'Expected configuration options button to be disabled')
def xtest_extents_changed(self): """Memory requirements are calculated correctly when extents change. """ set_canvas_crs(GEOCRS, True) set_jakarta_extent(self.dock) setup_scenario( self.dock, hazard='A flood in Jakarta like in 2007', exposure='Penduduk Jakarta', function='Need evacuation', function_id='FloodEvacuationRasterHazardFunction') result = self.dock.checkMemoryUsage() message = 'Expected "3mb" to apear in : %s' % result self.assertTrue(result is not None, 'Check memory reported None') self.assertTrue('3mb' in result, message)
def test_has_parameters_button_enabled(self): """Function configuration button is enabled when layers are compatible. """ set_canvas_crs(GEOCRS, True) set_jakarta_extent(self.dock) setup_scenario( self.dock, hazard='A flood in Jakarta like in 2007', exposure='Penduduk Jakarta', function='Need evacuation', function_id='FloodEvacuationRasterHazardFunction') tool_button = self.dock.toolFunctionOptions flag = tool_button.isEnabled() self.assertTrue( flag, 'Expected configuration options button to be enabled')
def test_vector_projections(self): """Test that vector input data is reprojected properly during clip.""" # Input data is OSM in GOOGLE CRS # We are reprojecting to GEO and expecting the output shp to be in GEO # see https://github.com/AIFDR/inasafe/issues/119 # and https://github.com/AIFDR/inasafe/issues/95 vector_layer = QgsVectorLayer( VECTOR_PATH2, 'OSM Buildings', 'ogr') message = 'Failed to load osm buildings' assert vector_layer is not None, message assert vector_layer.isValid() set_canvas_crs(GEOCRS, True) set_jakarta_extent() clip_rectangle = [106.52, -6.38, 107.14, -6.07] # Clip the vector to the bbox result = clip_layer(vector_layer, clip_rectangle) assert(os.path.exists(result.source()))
def test_preprocessing(self): """Preprocessing results are correct. TODO - this needs to be fixed post dock refactor. """ layer_path = os.path.join( TESTDATA, 'jakarta_crosskabupaten_polygons.shp') # See qgis project in test data: vector_preprocessing_test.qgs # add additional layers file_list = [layer_path] load_layers(file_list, clear_flag=False) layer_path = os.path.join( BOUNDDATA, 'kabupaten_jakarta.shp') file_list = [layer_path] load_layers(file_list, clear_flag=False) result, message = setup_scenario( DOCK, hazard='jakarta_crosskabupaten_polygons', exposure='People', function_id='Flood Evacuation Function Vector Hazard', aggregation_layer='kabupaten jakarta', aggregation_enabled_flag=True) assert result, message # Enable on-the-fly reprojection set_canvas_crs(GEOCRS, True) set_jakarta_extent(dock=DOCK) # Press RUN DOCK.accept() DOCK.runtime_keywords_dialog.accept() expected_feature_count = 20 message = ( 'The preprocessing should have generated %s features, ' 'found %s' % ( expected_feature_count, DOCK.analysis.aggregator.preprocessed_feature_count)) self.assertEqual( expected_feature_count, DOCK.analysis.aggregator.preprocessed_feature_count, message)
def test_clip_vector_hard(self): """Vector layers can be hard clipped. Hard clipping will remove any dangling, non intersecting elements. """ vector_layer = QgsVectorLayer( VECTOR_PATH3, 'OSM Buildings', 'ogr') message = 'Failed to load osm buildings' assert vector_layer is not None, message assert vector_layer.isValid() set_canvas_crs(GEOCRS, True) set_jakarta_extent() clip_rectangle = [106.8218, -6.1842, 106.8232, -6.1830] # Clip the vector to the bbox result = clip_layer(vector_layer, clip_rectangle, hard_clip_flag=True) # Check the output is valid assert(os.path.exists(result.source()))
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)
def test_full_run_pyzstats(self): """Aggregation results correct using our own python zonal stats code. """ settings = QtCore.QSettings() settings.setValue( 'inasafe/analysis_extents_mode', 'HazardExposure') result, message = setup_scenario( self.dock, hazard='Continuous Flood', exposure='Population', function='Need evacuation', function_id='FloodEvacuationRasterHazardFunction', aggregation_layer=u'D\xedstr\xedct\'s of Jakarta', aggregation_enabled_flag=True) self.assertTrue(result, message) # Enable on-the-fly reprojection set_canvas_crs(GEOCRS, True) set_jakarta_extent(self.dock) # Press RUN # noinspection PyCallByClass,PyTypeChecker self.dock.accept() result = self.dock.wvResults.page_to_text() control_file_path = test_data_path( 'control', 'files', 'test-full-run-results.txt') expected_result = codecs.open( control_file_path, mode='r', encoding='utf-8').readlines() result = result.replace( '</td> <td>', ' ').replace('</td><td>', ' ') result = result.replace( '<th class="text-right">', ' ').replace('</th>', ' ') result = result.replace( '</td><td class="text-right">', ' ') for line in expected_result: line = line.replace('\n', '') self.assertIn(line, result)
def test_insufficient_overlap(self): """Test Insufficient overlap errors are caught. ..note:: See https://github.com/AIFDR/inasafe/issues/372 """ # Push OK with the left mouse button button = DOCK.pbnRunStop message = 'Run button was not enabled' self.assertTrue(button.isEnabled(), message) result, message = setup_scenario( DOCK, hazard='Continuous Flood', exposure='Population', function='Need evacuation', function_id='FloodEvacuationRasterHazardFunction') self.assertTrue(result, message) # Enable on-the-fly reprojection set_canvas_crs(GEOCRS, True) # Zoom to an area where there is no overlap with layers rectangle = QgsRectangle( 106.635434302702, -6.101567666986, 106.635434302817, -6.101567666888) CANVAS.setExtent(rectangle) crs = QgsCoordinateReferenceSystem('EPSG:4326') DOCK.define_user_analysis_extent(rectangle, crs) # Press RUN DOCK.accept() result = DOCK.wvResults.page_to_text() # Check for an error containing InsufficientOverlapError expected_string = 'InsufficientOverlapError' message = 'Result not as expected %s not in: %s' % ( expected_string, result) # This is the expected impact number self.assertIn(expected_string, result, message)