def test_run_point_exposure(self):
        """Run the IF for point exposure.

        See https://github.com/AIFDR/inasafe/issues/2156.
        """
        generic_polygon_path = test_data_path(
            'hazard', 'classified_generic_polygon.shp')
        building_path = test_data_path('exposure', 'building-points.shp')

        hazard_layer = QgsVectorLayer(generic_polygon_path, 'Hazard', 'ogr')
        exposure_layer = QgsVectorLayer(building_path, 'Buildings', 'ogr')

        # Let's set the extent to the hazard extent
        extent = hazard_layer.extent()
        rect_extent = [
            extent.xMinimum(),
            extent.yMaximum(),
            extent.xMaximum(),
            extent.yMinimum()
        ]

        impact_function = ClassifiedPolygonHazardBuildingFunction.instance()
        impact_function.hazard = SafeLayer(hazard_layer)
        impact_function.exposure = SafeLayer(exposure_layer)
        impact_function.requested_extent = rect_extent
        impact_function.run()
        impact_layer = impact_function.impact

        # Check the question
        expected_question = ('In each of the hazard zones how many buildings '
                             'might be affected.')
        message = 'The question should be %s, but it returns %s' % (
            expected_question, impact_function.question)
        self.assertEqual(expected_question, impact_function.question, message)

        zone_sum = impact_layer.get_data(
            attribute=impact_function.target_field)
        high_zone_count = zone_sum.count('High Hazard Zone')
        medium_zone_count = zone_sum.count('Medium Hazard Zone')
        low_zone_count = zone_sum.count('Low Hazard Zone')
        # The result
        expected_high_count = 12
        expected_medium_count = 172
        expected_low_count = 3
        message = 'Expecting %s for High Hazard Zone, but it returns %s' % (
            high_zone_count, expected_high_count)
        self.assertEqual(high_zone_count, expected_high_count, message)

        message = 'Expecting %s for Medium Hazard Zone, but it returns %s' % (
            expected_medium_count, medium_zone_count)
        self.assertEqual(medium_zone_count, expected_medium_count, message)

        message = 'Expecting %s for Low Hazard Zone, but it returns %s' % (
            expected_low_count, low_zone_count)
        self.assertEqual(expected_low_count, low_zone_count, message)
    def test_run_point_exposure(self):
        """Run the IF for point exposure.

        See https://github.com/AIFDR/inasafe/issues/2156.
        """
        generic_polygon_path = test_data_path(
            'hazard', 'classified_generic_polygon.shp')
        building_path = test_data_path('exposure', 'building-points.shp')

        hazard_layer = QgsVectorLayer(generic_polygon_path, 'Hazard', 'ogr')
        exposure_layer = QgsVectorLayer(building_path, 'Buildings', 'ogr')

        # Let's set the extent to the hazard extent
        extent = hazard_layer.extent()
        rect_extent = [
            extent.xMinimum(), extent.yMaximum(),
            extent.xMaximum(), extent.yMinimum()]

        impact_function = ClassifiedPolygonHazardBuildingFunction.instance()
        impact_function.hazard = SafeLayer(hazard_layer)
        impact_function.exposure = SafeLayer(exposure_layer)
        impact_function.requested_extent = rect_extent
        impact_function.run()
        impact_layer = impact_function.impact

        # Check the question
        expected_question = ('In each of the hazard zones how many buildings '
                             'might be affected.')
        message = 'The question should be %s, but it returns %s' % (
            expected_question, impact_function.question)
        self.assertEqual(expected_question, impact_function.question, message)

        zone_sum = impact_layer.get_data(
            attribute=impact_function.target_field)
        high_zone_count = zone_sum.count('High Hazard Zone')
        medium_zone_count = zone_sum.count('Medium Hazard Zone')
        low_zone_count = zone_sum.count('Low Hazard Zone')
        # The result
        expected_high_count = 12
        expected_medium_count = 172
        expected_low_count = 3
        message = 'Expecting %s for High Hazard Zone, but it returns %s' % (
            high_zone_count, expected_high_count)
        self.assertEqual(high_zone_count, expected_high_count, message)

        message = 'Expecting %s for Medium Hazard Zone, but it returns %s' % (
            expected_medium_count, medium_zone_count)
        self.assertEqual(medium_zone_count, expected_medium_count, message)

        message = 'Expecting %s for Low Hazard Zone, but it returns %s' % (
            expected_low_count, low_zone_count)
        self.assertEqual(expected_low_count, low_zone_count, message)
    def test_run(self):
        """TestGenericPolygonBuildingFunction: Test running the IF."""
        generic_polygon_path = test_data_path(
            'hazard', 'classified_generic_polygon.shp')
        building_path = test_data_path('exposure', 'buildings.shp')

        hazard_layer = read_layer(generic_polygon_path)
        exposure_layer = read_layer(building_path)

        impact_function = ClassifiedPolygonHazardBuildingFunction.instance()
        impact_function.hazard = hazard_layer
        impact_function.exposure = exposure_layer
        impact_function.parameters['hazard zone attribute'] = 'h_zone'
        impact_function.run()
        impact_layer = impact_function.impact

        # Check the question
        expected_question = ('In each of the hazard zones how many buildings '
                             'might be affected.')
        message = 'The question should be %s, but it returns %s' % (
            expected_question, impact_function.question)
        self.assertEqual(expected_question, impact_function.question, message)

        zone_sum = impact_layer.get_data(attribute='zone')
        high_zone_count = zone_sum.count('High Hazard Zone')
        medium_zone_count = zone_sum.count('Medium Hazard Zone')
        low_zone_count = zone_sum.count('Low Hazard Zone')
        # The result
        expected_high_count = 11
        expected_medium_count = 161
        expected_low_count = 0
        message = 'Expecting %s for High Hazard Zone, but it returns %s' % (
            high_zone_count, expected_high_count)
        self.assertEqual(high_zone_count, expected_high_count, message)

        message = 'Expecting %s for Medium Hazard Zone, but it returns %s' % (
            expected_medium_count, medium_zone_count)
        self.assertEqual(medium_zone_count, expected_medium_count, message)

        message = 'Expecting %s for Low Hazard Zone, but it returns %s' % (
            expected_low_count, low_zone_count)
        self.assertEqual(expected_low_count, low_zone_count, message)
    def test_run(self):
        """TestGenericPolygonBuildingFunction: Test running the IF."""
        generic_polygon_path = test_data_path("hazard", "classified_generic_polygon.shp")
        building_path = test_data_path("exposure", "buildings.shp")

        hazard_layer = read_layer(generic_polygon_path)
        exposure_layer = read_layer(building_path)

        impact_function = ClassifiedPolygonHazardBuildingFunction.instance()
        impact_function.hazard = hazard_layer
        impact_function.exposure = exposure_layer
        impact_function.parameters["hazard zone attribute"] = "h_zone"
        impact_function.run()
        impact_layer = impact_function.impact

        # Check the question
        expected_question = "In each of the hazard zones how many buildings " "might be affected."
        message = "The question should be %s, but it returns %s" % (expected_question, impact_function.question)
        self.assertEqual(expected_question, impact_function.question, message)

        zone_sum = impact_layer.get_data(attribute="zone")
        high_zone_count = zone_sum.count("High Hazard Zone")
        medium_zone_count = zone_sum.count("Medium Hazard Zone")
        low_zone_count = zone_sum.count("Low Hazard Zone")
        # The result
        expected_high_count = 11
        expected_medium_count = 161
        expected_low_count = 0
        message = "Expecting %s for High Hazard Zone, but it returns %s" % (high_zone_count, expected_high_count)
        self.assertEqual(high_zone_count, expected_high_count, message)

        message = "Expecting %s for Medium Hazard Zone, but it returns %s" % (expected_medium_count, medium_zone_count)
        self.assertEqual(medium_zone_count, expected_medium_count, message)

        message = "Expecting %s for Low Hazard Zone, but it returns %s" % (expected_low_count, low_zone_count)
        self.assertEqual(expected_low_count, low_zone_count, message)
    def test_run(self):
        """TestGenericPolygonBuildingFunction: Test running the IF."""

        # Expected results
        expected_high_count = 11
        expected_medium_count = 161
        expected_low_count = 2
        expected_total = 181

        # Computed expected results
        expected_total_affected = (
            expected_high_count + expected_medium_count + expected_low_count)
        expected_not_affected = expected_total - expected_total_affected

        generic_polygon_path = test_data_path(
            'hazard', 'classified_generic_polygon.shp')
        building_path = test_data_path('exposure', 'buildings.shp')

        hazard_layer = QgsVectorLayer(generic_polygon_path, 'Hazard', 'ogr')
        exposure_layer = QgsVectorLayer(building_path, 'Buildings', 'ogr')

        # Let's set the extent to the hazard extent
        extent = hazard_layer.extent()
        rect_extent = [
            extent.xMinimum(), extent.yMaximum(),
            extent.xMaximum(), extent.yMinimum()]

        impact_function = ClassifiedPolygonHazardBuildingFunction.instance()
        impact_function.hazard = SafeLayer(hazard_layer)
        impact_function.exposure = SafeLayer(exposure_layer)
        impact_function.requested_extent = rect_extent
        impact_function.run()
        impact_layer = impact_function.impact
        impact_data = impact_layer.impact_data

        # Check the question
        expected_question = ('In each of the hazard zones how many buildings '
                             'might be affected.')
        message = 'The question should be %s, but it returns %s' % (
            expected_question, impact_function.question)
        self.assertEqual(expected_question, impact_function.question, message)

        # Check the impact layer
        zone_sum = impact_layer.get_data(
            attribute=impact_function.target_field)
        high_zone_count = zone_sum.count('High Hazard Zone')
        medium_zone_count = zone_sum.count('Medium Hazard Zone')
        low_zone_count = zone_sum.count('Low Hazard Zone')
        not_affected_count = zone_sum.count('Not Affected')

        message = 'Expecting %s for High Hazard Zone, but it returns %s' % (
            high_zone_count, expected_high_count)
        self.assertEqual(high_zone_count, expected_high_count, message)

        message = 'Expecting %s for Medium Hazard Zone, but it returns %s' % (
            expected_medium_count, medium_zone_count)
        self.assertEqual(medium_zone_count, expected_medium_count, message)

        message = 'Expecting %s for Low Hazard Zone, but it returns %s' % (
            expected_low_count, low_zone_count)
        self.assertEqual(expected_low_count, low_zone_count, message)

        message = 'Expecting %s for Not Affected Zone, but it returns %s' % (
            expected_not_affected, not_affected_count)
        self.assertEqual(expected_not_affected, not_affected_count, message)

        # Test the JSON impact summary
        impact_summary = impact_data['impact summary']['fields']
        expected_impact_summary = [
            ['High Hazard Zone', expected_high_count],
            ['Medium Hazard Zone', expected_medium_count],
            ['Low Hazard Zone', expected_low_count],
            ['Affected buildings', expected_total_affected],
            ['Not affected buildings', expected_not_affected],
            ['Total', expected_total]
        ]
        for expected, data in zip(expected_impact_summary, impact_summary):
            self.assertListEqual(data, expected)

        # Test the JSON impact table headings
        impact_table = impact_data['impact table']
        expected_headings = [
            u'Building type',
            u'High Hazard Zone',
            u'Medium Hazard Zone',
            u'Low Hazard Zone',
            u'Not Affected',
            u'Total']
        self.assertListEqual(impact_table['attributes'], expected_headings)

        # Test the JSON impact table content
        expected_fields = [
            [u'Other', 0, 31, 2, 7, 40],
            [u'Residential', 11, 130, 0, 0, 141],
            [u'Total', 11, 161, 2, 7, 181]
        ]
        for expected, data in zip(expected_fields, impact_table['fields']):
            self.assertListEqual(data, expected)
    def test_run(self):
        """TestGenericPolygonBuildingFunction: Test running the IF."""

        # Expected results
        expected_high_count = 11
        expected_medium_count = 161
        expected_low_count = 2
        expected_total = 181

        # Computed expected results
        expected_total_affected = (expected_high_count +
                                   expected_medium_count + expected_low_count)
        expected_not_affected = expected_total - expected_total_affected

        generic_polygon_path = standard_data_path(
            'hazard', 'classified_generic_polygon.shp')
        building_path = standard_data_path('exposure', 'buildings.shp')

        hazard_layer = QgsVectorLayer(generic_polygon_path, 'Hazard', 'ogr')
        exposure_layer = QgsVectorLayer(building_path, 'Buildings', 'ogr')

        # Let's set the extent to the hazard extent
        extent = hazard_layer.extent()
        rect_extent = [
            extent.xMinimum(),
            extent.yMaximum(),
            extent.xMaximum(),
            extent.yMinimum()
        ]

        impact_function = ClassifiedPolygonHazardBuildingFunction.instance()
        impact_function.hazard = SafeLayer(hazard_layer)
        impact_function.exposure = SafeLayer(exposure_layer)
        impact_function.requested_extent = rect_extent
        impact_function.run()
        impact_layer = impact_function.impact
        impact_data = impact_layer.impact_data

        # Check the question
        expected_question = (
            'In each of the hazard zones how many buildings might be '
            'affected?')
        self.assertEqual(expected_question, impact_function.question)

        # Check the impact layer
        zone_sum = impact_layer.get_data(
            attribute=impact_function.target_field)
        high_zone_count = zone_sum.count('High Hazard Zone')
        medium_zone_count = zone_sum.count('Medium Hazard Zone')
        low_zone_count = zone_sum.count('Low Hazard Zone')
        not_affected_count = zone_sum.count('Not Affected')

        message = 'Expecting %s for High Hazard Zone, but it returns %s' % (
            high_zone_count, expected_high_count)
        self.assertEqual(high_zone_count, expected_high_count, message)

        message = 'Expecting %s for Medium Hazard Zone, but it returns %s' % (
            expected_medium_count, medium_zone_count)
        self.assertEqual(medium_zone_count, expected_medium_count, message)

        message = 'Expecting %s for Low Hazard Zone, but it returns %s' % (
            expected_low_count, low_zone_count)
        self.assertEqual(expected_low_count, low_zone_count, message)

        message = 'Expecting %s for Not Affected Zone, but it returns %s' % (
            expected_not_affected, not_affected_count)
        self.assertEqual(expected_not_affected, not_affected_count, message)

        # Test the JSON impact summary
        impact_summary = impact_data['impact summary']['fields']
        expected_impact_summary = [
            ['High Hazard Zone', expected_high_count],
            ['Medium Hazard Zone', expected_medium_count],
            ['Low Hazard Zone', expected_low_count],
            ['Affected buildings', expected_total_affected],
            ['Not affected buildings', expected_not_affected],
            ['Total', expected_total]
        ]
        for expected, data in zip(expected_impact_summary, impact_summary):
            self.assertListEqual(data, expected)

        # Test the JSON impact table headings
        impact_table = impact_data['impact table']
        expected_headings = [
            u'Building type', u'High Hazard Zone', u'Medium Hazard Zone',
            u'Low Hazard Zone', u'Not Affected', u'Total'
        ]
        self.assertListEqual(impact_table['attributes'], expected_headings)

        # Test the JSON impact table content
        expected_fields = [[u'Commercial', 6, 25, 0, 0, 31],
                           [u'Education', 0, 7, 0, 0, 7],
                           [u'Government', 0, 12, 2, 4, 18],
                           [u'Health', 0, 3, 0, 2, 5],
                           [u'Other', 0, 1, 0, 0, 1],
                           [u'Place of worship', 0, 8, 0, 1, 9],
                           [u'Residential', 5, 105, 0, 0, 110],
                           [u'Total', 11, 161, 2, 7, 181]]
        for expected, data in zip(expected_fields, impact_table['fields']):
            self.assertListEqual(data, expected)