Beispiel #1
0
    def test_check_aggregation_single_attribute(self):
        """Aggregation attribute is chosen correctly when there is only
        one attr available."""
        layer_path = os.path.join(
            TESTDATA, 'kabupaten_jakarta_singlepart_1_good_attr.shp')
        file_list = [layer_path]
        # add additional layers
        load_layers(file_list, clear_flag=False)
        attribute_key = get_defaults('AGGR_ATTR_KEY')

        # with 1 good aggregation attribute using
        # kabupaten_jakarta_singlepart_1_good_attr.shp
        result, message = setup_scenario(
            self.DOCK,
            hazard='Continuous Flood',
            exposure='Population',
            function_id='FloodEvacuationRasterHazardFunction',
            aggregation_layer='kabupaten jakarta singlepart 1 good attr')
        set_jakarta_extent(dock=self.DOCK)
        assert result, message
        # Press RUN
        # noinspection PyCallByClass,PyTypeChecker
        self.DOCK.accept()
        aggregator = self.DOCK.impact_function.aggregator
        attribute = aggregator.attributes[attribute_key]
        message = ('The aggregation should be KAB_NAME. Found: %s' % attribute)
        self.assertEqual(attribute, 'KAB_NAME', message)
Beispiel #2
0
    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)
Beispiel #3
0
    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_check_aggregation_no_attributes(self):
     """Aggregation attribute chosen correctly when no attr available."""
     layer_path = os.path.join(
         TESTDATA, 'kabupaten_jakarta_singlepart_0_good_attr.shp')
     file_list = [layer_path]
     # add additional layers
     load_layers(file_list, clear_flag=False)
     attribute_key = get_defaults('AGGR_ATTR_KEY')
     # with no good aggregation attribute using
     # kabupaten_jakarta_singlepart_0_good_attr.shp
     result, message = setup_scenario(
         self.DOCK,
         hazard='Continuous Flood',
         exposure='Population',
         function_id='FloodEvacuationRasterHazardFunction',
         aggregation_layer='kabupaten jakarta singlepart 0 good attr')
     set_jakarta_extent(dock=self.DOCK)
     self.assertTrue(result, message)
     # Press RUN
     self.DOCK.accept()
     aggregator = self.DOCK.impact_function.aggregator
     attribute = aggregator.attributes[attribute_key]
     message = (
         'The aggregation should be None. Found: %s' % attribute)
     self.assertIsNone(attribute, message)
    def test_check_aggregation_single_attribute(self):
        """Aggregation attribute is chosen correctly when there is only
        one attr available."""
        layer_path = os.path.join(
            TESTDATA, 'kabupaten_jakarta_singlepart_1_good_attr.shp')
        file_list = [layer_path]
        # add additional layers
        load_layers(file_list, clear_flag=False)
        attribute_key = get_defaults('AGGR_ATTR_KEY')

        # with 1 good aggregation attribute using
        # kabupaten_jakarta_singlepart_1_good_attr.shp
        result, message = setup_scenario(
            self.DOCK,
            hazard='Continuous Flood',
            exposure='Population',
            function_id='FloodEvacuationRasterHazardFunction',
            aggregation_layer='kabupaten jakarta singlepart 1 good attr')
        set_jakarta_extent(dock=self.DOCK)
        assert result, message
        # Press RUN
        # noinspection PyCallByClass,PyTypeChecker
        self.DOCK.accept()
        aggregator = self.DOCK.impact_function.aggregator
        attribute = aggregator.attributes[attribute_key]
        message = (
            'The aggregation should be KAB_NAME. Found: %s' % attribute)
        self.assertEqual(attribute, 'KAB_NAME', message)
Beispiel #6
0
    def test_check_aggregation_single_attribute(self):
        """Aggregation attribute is chosen correctly when there is only
        one attr available."""
        layer_path = os.path.join(
            TESTDATA, 'kabupaten_jakarta_singlepart_1_good_attr.shp')
        file_list = [layer_path]
        # add additional layers
        load_layers(file_list, clear_flag=False)
        attribute_key = get_defaults('AGGR_ATTR_KEY')

        # with 1 good aggregation attribute using
        # kabupaten_jakarta_singlepart_1_good_attr.shp
        result, message = setup_scenario(
            DOCK,
            hazard='A flood in Jakarta like in 2007',
            exposure='People',
            function_id='Flood Evacuation Function',
            aggregation_layer='kabupaten jakarta singlepart 1 good attr')
        set_jakarta_extent(dock=DOCK)
        assert result, message
        # Press RUN
        # noinspection PyCallByClass,PyTypeChecker
        DOCK.accept()
        DOCK.runtime_keywords_dialog.accept()
        print attribute_key
        print DOCK.analysis.aggregator.attributes
        attribute = DOCK.analysis.aggregator.attributes[attribute_key]
        message = (
            'The aggregation should be KAB_NAME. Found: %s' % attribute)
        self.assertEqual(attribute, 'KAB_NAME', message)
Beispiel #7
0
 def test_check_aggregation_no_attributes(self):
     """Aggregation attribute chosen correctly when no attr available."""
     layer_path = os.path.join(
         TESTDATA, 'kabupaten_jakarta_singlepart_0_good_attr.shp')
     file_list = [layer_path]
     # add additional layers
     load_layers(file_list, clear_flag=False)
     attribute_key = get_defaults('AGGR_ATTR_KEY')
     # with no good aggregation attribute using
     # kabupaten_jakarta_singlepart_0_good_attr.shp
     result, message = setup_scenario(
         DOCK,
         hazard='A flood in Jakarta like in 2007',
         exposure='People',
         function_id='Flood Evacuation Function',
         aggregation_layer='kabupaten jakarta singlepart 0 good attr')
     set_jakarta_extent(dock=DOCK)
     assert result, message
     # Press RUN
     DOCK.accept()
     DOCK.runtime_keywords_dialog.accept()
     attribute = DOCK.analysis.aggregator.attributes[attribute_key]
     message = (
         'The aggregation should be None. Found: %s' % attribute)
     assert attribute is None, message
Beispiel #8
0
    def test_issue71(self):
        """Test issue #71 in github - cbo changes should update ok button."""
        # 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 = [
            join(HAZDATA, 'Flood_Current_Depth_Jakarta_geographic.asc'),
            join(TESTDATA, 'Population_Jakarta_geographic.asc')]
        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)
        # set exposure to : Population Count (5kmx5km)
        # by moving one down
        DOCK.cboExposure.setCurrentIndex(DOCK.cboExposure.currentIndex() + 1)
        actual_dict = get_ui_state(DOCK)
        expected_dict = {
            'Run Button Enabled': False,
            'Impact Function Id': '',
            'Impact Function Title': '',
            'Hazard': 'A flood in Jakarta like in 2007',
            'Exposure': 'Population Count (5kmx5km)'}
        message = ((
            'Run button was not disabled when exposure set to \n%s'
            '\nUI State: \n%s\nExpected State:\n%s\n%s') % (
                DOCK.cboExposure.currentText(),
                actual_dict,
                expected_dict,
                combos_to_string(DOCK)))

        self.assertTrue(expected_dict == actual_dict, message)

        # Now select again a valid layer and the run button
        # should be enabled
        DOCK.cboExposure.setCurrentIndex(DOCK.cboExposure.currentIndex() - 1)
        message = (
            'Run button was not enabled when exposure set to \n%s' %
            DOCK.cboExposure.currentText())
        self.assertTrue(button.isEnabled(), message)
Beispiel #9
0
    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)
Beispiel #10
0
    def test_issue71(self):
        """Test issue #71 in github - cbo changes should update ok button."""
        # 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 = [
            join(HAZDATA, 'Flood_Current_Depth_Jakarta_geographic.asc'),
            join(TESTDATA, 'Population_Jakarta_geographic.asc')]
        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)
        # set exposure to : Population Count (5kmx5km)
        # by moving one down
        DOCK.cboExposure.setCurrentIndex(DOCK.cboExposure.currentIndex() + 1)
        actual_dict = get_ui_state(DOCK)
        expected_dict = {
            'Run Button Enabled': False,
            'Impact Function Id': '',
            'Impact Function Title': '',
            'Hazard': 'A flood in Jakarta like in 2007',
            'Exposure': 'Population Count (5kmx5km)'}
        message = ((
            'Run button was not disabled when exposure set to \n%s'
            '\nUI State: \n%s\nExpected State:\n%s\n%s') % (
                DOCK.cboExposure.currentText(),
                actual_dict,
                expected_dict,
                combos_to_string(DOCK)))

        self.assertTrue(expected_dict == actual_dict, message)

        # Now select again a valid layer and the run button
        # should be enabled
        DOCK.cboExposure.setCurrentIndex(DOCK.cboExposure.currentIndex() - 1)
        message = (
            'Run button was not enabled when exposure set to \n%s' %
            DOCK.cboExposure.currentText())
        self.assertTrue(button.isEnabled(), message)
Beispiel #11
0
    def test_issue71(self):
        """Test issue #71 in github - cbo changes should update ok button."""
        # See https://github.com/AIFDR/inasafe/issues/71
        settings = QtCore.QSettings()
        settings.setValue('inasafe/analysis_extents_mode', 'HazardExposure')
        self.tearDown()
        button = self.dock.pbnRunStop
        # First part of scenario should have enabled run
        file_list = [
            standard_data_path('hazard', 'continuous_flood_20_20.asc'),
            standard_data_path('exposure', 'pop_binary_raster_20_20.asc')
        ]
        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)
        # set exposure to : Population Count (5kmx5km)
        # by moving one down
        self.dock.cboExposure.setCurrentIndex(
            self.dock.cboExposure.currentIndex() + 1)
        actual_dict = get_ui_state(self.dock)
        expected_dict = {
            'Run Button Enabled': False,
            'Impact Function Id': '',
            'Impact Function Title': '',
            'Hazard': 'Continuous Flood',
            'Exposure': 'Population Count (5kmx5km)'
        }
        message = (('Run button was not disabled when exposure set to \n%s'
                    '\nUI State: \n%s\nExpected State:\n%s\n%s') %
                   (self.dock.cboExposure.currentText(), actual_dict,
                    expected_dict, combos_to_string(self.dock)))

        self.assertTrue(expected_dict == actual_dict, message)

        # Now select again a valid layer and the run button
        # should be enabled
        self.dock.cboExposure.setCurrentIndex(
            self.dock.cboExposure.currentIndex() - 1)
        message = ('Run button was not enabled when exposure set to \n%s' %
                   self.dock.cboExposure.currentText())
        self.assertTrue(button.isEnabled(), message)
Beispiel #12
0
    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)
Beispiel #13
0
    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)
Beispiel #14
0
    def test_state(self):
        """Check if the save/restore state methods work. See also
        https://github.com/AIFDR/inasafe/issues/58
        """
        # default selected layer is the third layer exposure
        # so, decrease the index by one to change it
        DOCK.cboExposure.setCurrentIndex(DOCK.cboExposure.currentIndex() - 1)
        DOCK.save_state()
        expected_dict = get_ui_state(DOCK)
        # myState = DOCK.state
        # Now reset and restore and check that it gets the old state
        # Html is not considered in restore test since the ready
        # message overwrites it in dock implementation
        DOCK.cboExposure.setCurrentIndex(DOCK.cboExposure.currentIndex() - 1)
        DOCK.restore_state()
        result_dict = get_ui_state(DOCK)
        message = 'Got unexpected state: %s\nExpected: %s\n%s' % (
            result_dict, expected_dict, combos_to_string(DOCK))
        self.assertTrue(expected_dict == result_dict, message)

        # Corner case test when two layers can have the
        # same functions - when switching layers the selected function should
        # remain unchanged
        self.tearDown()
        file_list = [
            test_data_path('hazard', 'jakarta_flood_design.tif'),
            test_data_path('hazard', 'continuous_flood_20_20.asc'),
            test_data_path('exposure', 'pop_binary_raster_20_20.asc')
        ]
        hazard_layer_count, exposure_layer_count = load_layers(file_list)
        self.assertTrue(hazard_layer_count == 2)
        self.assertTrue(exposure_layer_count == 1)
        # we will have 2 impact function available right now:
        # - ContinuousHazardPopulationFunction, titled: 'Be impacted'
        # - FloodEvacuationRasterHazardFunction, titled: 'Need evacuation'
        # set it to the second for testing purposes
        DOCK.cboFunction.setCurrentIndex(1)
        DOCK.cboHazard.setCurrentIndex(0)
        DOCK.cboExposure.setCurrentIndex(0)
        expected_function = str(DOCK.cboFunction.currentText())
        # Now move down one hazard in the combo then verify
        # the function remains unchanged
        DOCK.cboHazard.setCurrentIndex(1)
        current_function = str(DOCK.cboFunction.currentText())
        message = (
            'Expected selected impact function to remain unchanged when '
            'choosing a different hazard of the same category:'
            ' %s\nExpected: %s\n%s' %
            (expected_function, current_function, combos_to_string(DOCK)))

        self.assertTrue(expected_function == current_function, message)
        DOCK.cboHazard.setCurrentIndex(0)
        # Selected function should remain the same
        # RM: modified it, because there is generic one right now as the first.
        # the selected one should be FloodEvacuationRasterHazardFunction
        DOCK.cboFunction.setCurrentIndex(1)
        expected = 'Need evacuation'
        function = DOCK.cboFunction.currentText()
        message = 'Expected: %s, Got: %s' % (expected, function)
        self.assertTrue(function == expected, message)
Beispiel #15
0
    def test_state(self):
        """Check if the save/restore state methods work. See also
        https://github.com/AIFDR/inasafe/issues/58
        """
        # default selected layer is the third layer exposure
        # so, decrease the index by one to change it
        DOCK.cboExposure.setCurrentIndex(DOCK.cboExposure.currentIndex() - 1)
        DOCK.save_state()
        expected_dict = get_ui_state(DOCK)
        # myState = DOCK.state
        # Now reset and restore and check that it gets the old state
        # Html is not considered in restore test since the ready
        # message overwrites it in dock implementation
        DOCK.cboExposure.setCurrentIndex(DOCK.cboExposure.currentIndex() - 1)
        DOCK.restore_state()
        result_dict = get_ui_state(DOCK)
        message = 'Got unexpected state: %s\nExpected: %s\n%s' % (
            result_dict, expected_dict, combos_to_string(DOCK))
        self.assertTrue(expected_dict == result_dict, message)

        # Corner case test when two layers can have the
        # same functions - when switching layers the selected function should
        # remain unchanged
        self.tearDown()
        file_list = [
            test_data_path('hazard', 'jakarta_flood_design.tif'),
            test_data_path('hazard', 'continuous_flood_20_20.asc'),
            test_data_path('exposure', 'pop_binary_raster_20_20.asc')
        ]
        hazard_layer_count, exposure_layer_count = load_layers(file_list)
        self.assertTrue(hazard_layer_count == 2)
        self.assertTrue(exposure_layer_count == 1)
        # we will have 2 impact function available right now:
        # - ContinuousHazardPopulationFunction, titled: 'Be impacted'
        # - FloodEvacuationRasterHazardFunction, titled: 'Need evacuation'
        # set it to the second for testing purposes
        DOCK.cboFunction.setCurrentIndex(1)
        DOCK.cboHazard.setCurrentIndex(0)
        DOCK.cboExposure.setCurrentIndex(0)
        expected_function = str(DOCK.cboFunction.currentText())
        # Now move down one hazard in the combo then verify
        # the function remains unchanged
        DOCK.cboHazard.setCurrentIndex(1)
        current_function = str(DOCK.cboFunction.currentText())
        message = (
            'Expected selected impact function to remain unchanged when '
            'choosing a different hazard of the same category:'
            ' %s\nExpected: %s\n%s' % (
                expected_function, current_function, combos_to_string(DOCK)))

        self.assertTrue(expected_function == current_function, message)
        DOCK.cboHazard.setCurrentIndex(0)
        # Selected function should remain the same
        # RM: modified it, because there is generic one right now as the first.
        # the selected one should be FloodEvacuationRasterHazardFunction
        DOCK.cboFunction.setCurrentIndex(1)
        expected = 'Need evacuation'
        function = DOCK.cboFunction.currentText()
        message = 'Expected: %s, Got: %s' % (expected, function)
        self.assertTrue(function == expected, message)
Beispiel #16
0
    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)
Beispiel #17
0
    def test_state(self):
        """Check if the save/restore state methods work. See also
        https://github.com/AIFDR/inasafe/issues/58
        """
        DOCK.cboExposure.setCurrentIndex(DOCK.cboExposure.currentIndex() + 1)
        DOCK.save_state()
        expected_dict = get_ui_state(DOCK)
        # myState = DOCK.state
        # Now reset and restore and check that it gets the old state
        # Html is not considered in restore test since the ready
        # message overwrites it in dock implementation
        DOCK.cboExposure.setCurrentIndex(DOCK.cboExposure.currentIndex() + 1)
        DOCK.restore_state()
        result_dict = get_ui_state(DOCK)
        message = 'Got unexpected state: %s\nExpected: %s\n%s' % (
            result_dict, expected_dict, combos_to_string(DOCK))
        self.assertTrue(expected_dict == result_dict, message)

        # Corner case test when two layers can have the
        # same functions - when switching layers the selected function should
        # remain unchanged
        self.tearDown()
        file_list = [
            join(HAZDATA, 'Flood_Design_Depth_Jakarta_geographic.asc'),
            join(HAZDATA, 'Flood_Current_Depth_Jakarta_geographic.asc'),
            join(TESTDATA, 'Population_Jakarta_geographic.asc')
        ]
        hazard_layer_count, exposure_layer_count = load_layers(file_list)
        self.assertTrue(hazard_layer_count == 2)
        self.assertTrue(exposure_layer_count == 1)
        DOCK.cboFunction.setCurrentIndex(0)
        DOCK.cboHazard.setCurrentIndex(0)
        DOCK.cboExposure.setCurrentIndex(0)
        expected_function = str(DOCK.cboFunction.currentText())
        # Now move down one hazard in the combo then verify
        # the function remains unchanged
        DOCK.cboHazard.setCurrentIndex(1)
        current_function = str(DOCK.cboFunction.currentText())
        message = (
            'Expected selected impact function to remain unchanged when '
            'choosing a different hazard of the same category:'
            ' %s\nExpected: %s\n%s' %
            (expected_function, current_function, combos_to_string(DOCK)))

        self.assertTrue(expected_function == current_function, message)
        DOCK.cboHazard.setCurrentIndex(0)
        # Selected function should remain the same
        expected = 'Need evacuation'
        function = DOCK.cboFunction.currentText()
        message = 'Expected: %s, Got: %s' % (expected, function)
        self.assertTrue(function == expected, message)
    def test_preprocessing(self):
        """Preprocessing results are correct.

        TODO - this needs to be fixed post dock refactor.

        """
        layer_path = test_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)
Beispiel #19
0
    def test_state(self):
        """Check if the save/restore state methods work. See also
        https://github.com/AIFDR/inasafe/issues/58
        """
        DOCK.cboExposure.setCurrentIndex(DOCK.cboExposure.currentIndex() + 1)
        DOCK.save_state()
        expected_dict = get_ui_state(DOCK)
        # myState = DOCK.state
        # Now reset and restore and check that it gets the old state
        # Html is not considered in restore test since the ready
        # message overwrites it in dock implementation
        DOCK.cboExposure.setCurrentIndex(DOCK.cboExposure.currentIndex() + 1)
        DOCK.restore_state()
        result_dict = get_ui_state(DOCK)
        message = 'Got unexpected state: %s\nExpected: %s\n%s' % (
            result_dict, expected_dict, combos_to_string(DOCK))
        self.assertTrue(expected_dict == result_dict, message)

        # Corner case test when two layers can have the
        # same functions - when switching layers the selected function should
        # remain unchanged
        self.tearDown()
        file_list = [
            join(HAZDATA, 'Flood_Design_Depth_Jakarta_geographic.asc'),
            join(HAZDATA, 'Flood_Current_Depth_Jakarta_geographic.asc'),
            join(TESTDATA, 'Population_Jakarta_geographic.asc')]
        hazard_layer_count, exposure_layer_count = load_layers(file_list)
        self.assertTrue(hazard_layer_count == 2)
        self.assertTrue(exposure_layer_count == 1)
        DOCK.cboFunction.setCurrentIndex(0)
        DOCK.cboHazard.setCurrentIndex(0)
        DOCK.cboExposure.setCurrentIndex(0)
        expected_function = str(DOCK.cboFunction.currentText())
        # Now move down one hazard in the combo then verify
        # the function remains unchanged
        DOCK.cboHazard.setCurrentIndex(1)
        current_function = str(DOCK.cboFunction.currentText())
        message = (
            'Expected selected impact function to remain unchanged when '
            'choosing a different hazard of the same category:'
            ' %s\nExpected: %s\n%s' % (
                expected_function, current_function, combos_to_string(DOCK)))

        self.assertTrue(expected_function == current_function, message)
        DOCK.cboHazard.setCurrentIndex(0)
        # Selected function should remain the same
        expected = 'Need evacuation'
        function = DOCK.cboFunction.currentText()
        message = 'Expected: %s, Got: %s' % (expected, function)
        self.assertTrue(function == expected, message)
Beispiel #20
0
 def test_check_aggregation_none_in_keywords(self):
     """Aggregation attribute is chosen correctly when None in keywords."""
     layer_path = os.path.join(
         TESTDATA, 'kabupaten_jakarta_singlepart_with_None_keyword.shp')
     file_list = [layer_path]
     # add additional layers
     load_layers(file_list, clear_flag=False)
     attribute_key = get_defaults('AGGR_ATTR_KEY')
     # with None aggregation attribute defined in .keyword using
     # kabupaten_jakarta_singlepart_with_None_keyword.shp
     result, message = setup_scenario(
         self.DOCK,
         hazard='Continuous Flood',
         exposure='Population',
         function_id='FloodEvacuationRasterHazardFunction',
         aggregation_layer='kabupaten jakarta singlepart with None keyword')
     set_jakarta_extent(dock=self.DOCK)
     assert result, message
     # Press RUN
     self.DOCK.accept()
     attribute = self.DOCK.analysis.aggregator.attributes[attribute_key]
     message = ('The aggregation should be None. Found: %s' % attribute)
     assert attribute is None, message
Beispiel #21
0
 def test_check_aggregation_no_attributes(self):
     """Aggregation attribute chosen correctly when no attr available."""
     layer_path = os.path.join(
         TESTDATA, 'kabupaten_jakarta_singlepart_0_good_attr.shp')
     file_list = [layer_path]
     # add additional layers
     load_layers(file_list, clear_flag=False)
     attribute_key = get_defaults('AGGR_ATTR_KEY')
     # with no good aggregation attribute using
     # kabupaten_jakarta_singlepart_0_good_attr.shp
     result, message = setup_scenario(
         self.DOCK,
         hazard='Continuous Flood',
         exposure='Population',
         function_id='FloodEvacuationRasterHazardFunction',
         aggregation_layer='kabupaten jakarta singlepart 0 good attr')
     set_jakarta_extent(dock=self.DOCK)
     self.assertTrue(result, message)
     # Press RUN
     self.DOCK.accept()
     aggregator = self.DOCK.impact_function.aggregator
     attribute = aggregator.attributes[attribute_key]
     message = ('The aggregation should be None. Found: %s' % attribute)
     self.assertIsNone(attribute, message)
Beispiel #22
0
    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)
Beispiel #23
0
    def test_issue160(self):
        """Test that multipart features can be used in a scenario - GH #160"""
        exposure_layer = clone_shp_layer(
            name='buildings',
            include_keywords=True,
            source_directory=standard_data_path('exposure'))

        hazard_layer = clone_shp_layer(
            name='flood_multipart_polygons',
            include_keywords=True,
            source_directory=standard_data_path('hazard'))

        exposure_path = exposure_layer.source()
        hazard_path = hazard_layer.source()

        self.tearDown()
        button = self.dock.pbnRunStop
        # First part of scenario should have enabled run
        file_list = [hazard_path, exposure_path]
        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(
            self.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')
        self.dock.define_user_analysis_extent(expected_extent, crs)

        # Press RUN
        # noinspection PyCallByClass,PyCallByClass,PyTypeChecker
        self.dock.accept()
        result = self.dock.wvResults.page_to_text()

        message = 'Result not as expected: %s' % result
        self.assertTrue(format_int(33) in result, message)
Beispiel #24
0
    def test_issue71(self):
        """Test issue #71 in github - cbo changes should update ok button."""
        # See https://github.com/AIFDR/inasafe/issues/71
        # Push OK with the left mouse button
        print 'Using QGIS: %s' % qgis_version()
        settings = QtCore.QSettings()
        settings.setValue(
            'inasafe/analysis_extents_mode', 'HazardExposure')
        self.tearDown()
        button = self.dock.pbnRunStop
        # First part of scenario should have enabled run
        file_list = [
            test_data_path('hazard', 'continuous_flood_20_20.asc'),
            test_data_path('exposure', 'pop_binary_raster_20_20.asc')]
        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)
        # set exposure to : Population Count (5kmx5km)
        # by moving one down
        self.dock.cboExposure.setCurrentIndex(
            self.dock.cboExposure.currentIndex() + 1)
        actual_dict = get_ui_state(self.dock)
        expected_dict = {
            'Run Button Enabled': False,
            'Impact Function Id': '',
            'Impact Function Title': '',
            'Hazard': 'Continuous Flood',
            'Exposure': 'Population Count (5kmx5km)'}
        message = ((
            'Run button was not disabled when exposure set to \n%s'
            '\nUI State: \n%s\nExpected State:\n%s\n%s') % (
                self.dock.cboExposure.currentText(),
                actual_dict,
                expected_dict,
                combos_to_string(self.dock)))

        self.assertTrue(expected_dict == actual_dict, message)

        # Now select again a valid layer and the run button
        # should be enabled
        self.dock.cboExposure.setCurrentIndex(
            self.dock.cboExposure.currentIndex() - 1)
        message = (
            'Run button was not enabled when exposure set to \n%s' %
            self.dock.cboExposure.currentText())
        self.assertTrue(button.isEnabled(), message)
Beispiel #25
0
    def test_issue160(self):
        """Test that multipart features can be used in a scenario - GH #160"""
        exposure_layer = clone_shp_layer(
            name='buildings',
            include_keywords=True,
            source_directory=standard_data_path('exposure'))

        hazard_layer = clone_shp_layer(
            name='flood_multipart_polygons',
            include_keywords=True,
            source_directory=standard_data_path('hazard'))

        exposure_path = exposure_layer.source()
        hazard_path = hazard_layer.source()

        self.tearDown()
        button = self.dock.pbnRunStop
        # First part of scenario should have enabled run
        file_list = [hazard_path, exposure_path]
        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(
            self.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')
        self.dock.define_user_analysis_extent(expected_extent, crs)

        # Press RUN
        # noinspection PyCallByClass,PyCallByClass,PyTypeChecker
        self.dock.accept()
        result = self.dock.wvResults.page_to_text()

        message = 'Result not as expected: %s' % result
        self.assertTrue(format_int(33) in result, message)
Beispiel #26
0
    def test_state(self):
        """Check if the save/restore state methods work. See also
        https://github.com/AIFDR/inasafe/issues/58
        """
        # default selected layer is the third layer exposure
        # so, decrease the index by one to change it
        self.dock.cboExposure.setCurrentIndex(
            self.dock.cboExposure.currentIndex() - 1)
        self.dock.save_state()
        expected_dict = get_ui_state(self.dock)
        # myState = self.dock.state
        # Now reset and restore and check that it gets the old state
        # Html is not considered in restore test since the ready
        # message overwrites it in dock implementation
        self.dock.cboExposure.setCurrentIndex(
            self.dock.cboExposure.currentIndex() - 1)
        self.dock.restore_state()
        result_dict = get_ui_state(self.dock)
        message = 'Got unexpected state: %s\nExpected: %s\n%s' % (
            result_dict, expected_dict, combos_to_string(self.dock))
        self.assertTrue(expected_dict == result_dict, message)

        # Corner case test when two layers can have the
        # same functions - when switching layers the selected function should
        # remain unchanged
        self.tearDown()
        file_list = [
            test_data_path('hazard', 'jakarta_flood_design.tif'),
            test_data_path('hazard', 'continuous_flood_20_20.asc'),
            test_data_path('exposure', 'pop_binary_raster_20_20.asc')
        ]
        hazard_layer_count, exposure_layer_count = load_layers(file_list)
        message = 'Expecting 2 hazard layers, got %s' % hazard_layer_count
        self.assertTrue(hazard_layer_count == 2, message)
        message = 'Expecting 1 exposure layer, got %s' % exposure_layer_count
        self.assertTrue(exposure_layer_count == 1, message)
        # we will have 1 impact function available right now:
        # - FloodEvacuationRasterHazardFunction, titled: 'Need evacuation'
        # set it to the second for testing purposes
        self.dock.cboFunction.setCurrentIndex(0)
        self.dock.cboHazard.setCurrentIndex(0)
        self.dock.cboExposure.setCurrentIndex(0)
        expected_function = str(self.dock.cboFunction.currentText())

        hazard_layer = self.dock.get_hazard_layer()
        hazard_keywords = self.dock.keyword_io.read_keywords(hazard_layer)
        exposure_layer = self.dock.get_exposure_layer()
        exposure_keywords = self.dock.keyword_io.read_keywords(exposure_layer)

        # Now move down one hazard in the combo then verify
        # the function remains unchanged
        self.dock.cboHazard.setCurrentIndex(1)
        current_function = str(self.dock.cboFunction.currentText())

        hazard_layer = self.dock.get_hazard_layer()
        hazard_keywords = self.dock.keyword_io.read_keywords(hazard_layer)
        exposure_layer = self.dock.get_exposure_layer()
        exposure_keywords = self.dock.keyword_io.read_keywords(exposure_layer)
        from pprint import pprint
        print 'Exposure keywords'
        pprint(exposure_keywords)
        print 'Hazard keywords'
        pprint(hazard_keywords)
        message = (
            'Expected selected impact function to remain unchanged when '
            'choosing a different hazard of the same category.\n')
        message += 'Expected IF: "%s"\n' % expected_function
        message += 'Current IF: "%s"\n' % current_function
        message += 'Current Dock State: %s' % combos_to_string(self.dock)
        message += 'Hazard Keywords\n'
        message += str(hazard_keywords) + '\n'
        message += 'Exposure Keywords\n'
        message += str(exposure_keywords) + '\n'

        self.assertTrue(expected_function == current_function, message)
        self.dock.cboHazard.setCurrentIndex(0)
        # Selected function should remain the same
        expected = 'Need evacuation'
        function = self.dock.cboFunction.currentText()
        message = 'Expected: %s, Got: %s' % (expected, function)

        self.assertTrue(function == expected, message)