Exemplo n.º 1
0
    def test_run(self):
        function = ClassifiedRasterHazardBuildingFunction.instance()

        hazard_path = standard_data_path(
            'hazard', 'classified_hazard.tif')
        exposure_path = standard_data_path('exposure', 'small_building.shp')
        hazard_layer = read_layer(hazard_path)
        exposure_layer = read_layer(exposure_path)

        function.hazard = SafeLayer(hazard_layer)
        function.exposure = SafeLayer(exposure_layer)
        function.run_analysis()
        impact_layer = function.impact
        impact_data = impact_layer.get_data()

        # Count
        expected_impact = {
            'Not affected': 5,
            'Low hazard zone': 2,
            'Medium hazard zone': 9,
            'High hazard zone': 5
        }

        result_impact = {}
        for impact_feature in impact_data:
            hazard_class = impact_feature[function.target_field]
            if hazard_class in result_impact:
                result_impact[hazard_class] += 1
            else:
                result_impact[hazard_class] = 1
        self.assertDictEqual(expected_impact, result_impact)
    def test_run(self):
        """TestVolcanoPolygonBuildingFunction: Test running the IF."""
        volcano_path = standard_data_path('hazard', 'volcano_krb.shp')
        building_path = standard_data_path('exposure', 'buildings.shp')

        hazard_layer = read_layer(volcano_path)
        exposure_layer = read_layer(building_path)

        impact_function = VolcanoPolygonBuildingFunction.instance()
        impact_function.hazard = SafeLayer(hazard_layer)
        impact_function.exposure = SafeLayer(exposure_layer)
        impact_function.run()
        impact_layer = impact_function.impact

        # Check the question
        expected_question = (
            'In the event of volcano krb how many buildings might be '
            'affected?')
        self.assertEqual(expected_question, impact_function.question)

        # The buildings should all be categorised into 5000 zone
        zone_sum = impact_layer.get_data(
            attribute=impact_function.target_field)
        krb3_zone_count = zone_sum.count('High Hazard Zone')
        krb2_zone_count = zone_sum.count('Medium Hazard Zone')
        # The result (counted by hand)
        expected_krb3_count = 11
        expected_krb2_count = 161
        message = 'Expecting %s for KRB III zone, but it returns %s' % (
            krb3_zone_count, expected_krb3_count)
        self.assertEqual(krb3_zone_count, expected_krb3_count, message)
        message = 'Expecting %s for KRB II zone, but it returns %s' % (
            krb2_zone_count, expected_krb2_count)
        self.assertEqual(krb2_zone_count, expected_krb2_count, message)
    def test_run(self):
        """TestClassifiedPolygonPopulationFunction: Test running the IF."""
        generic_polygon_path = standard_data_path(
            'hazard', 'classified_generic_polygon.shp')
        population_path = standard_data_path(
            'exposure', 'pop_binary_raster_20_20.asc')

        generic_polygon_layer = read_layer(generic_polygon_path)
        population_layer = read_layer(population_path)

        impact_function = ClassifiedPolygonHazardPopulationFunction.instance()
        impact_function.hazard = SafeLayer(generic_polygon_layer)
        impact_function.exposure = SafeLayer(population_layer)
        impact_function.run()
        impact_layer = impact_function.impact
        # Check the question
        expected_question = ('In each of the hazard zones how many people '
                             'might be impacted.')
        message = 'The question should be %s, but it returns %s' % (
            expected_question, impact_function.question)
        self.assertEqual(expected_question, impact_function.question, message)
        # Count by hand
        expected_affected_population = 181
        result = numpy.nansum(impact_layer.get_data())
        self.assertEqual(expected_affected_population, result, message)
Exemplo n.º 4
0
    def test_run(self):
        function = AshRasterPopulationFunction.instance()

        hazard_path = standard_data_path('hazard', 'ash_raster_wgs84.tif')
        exposure_path = standard_data_path(
            'exposure', 'pop_binary_raster_20_20.asc')
        # We need clipping for both layers to be in the same dimension
        clipped_hazard, clipped_exposure = clip_layers(
            hazard_path, exposure_path)

        hazard_layer = read_layer(clipped_hazard.source())
        exposure_layer = read_layer(clipped_exposure.source())

        # Let's set the extent to the hazard extent
        function.hazard = SafeLayer(hazard_layer)
        function.exposure = SafeLayer(exposure_layer)
        function.run()
        impact = function.impact
        expected = [
            [u'Population in very low hazard zone', 0],
            [u'Population in medium hazard zone', 1374],
            [u'Population in high hazard zone', 20],
            [u'Population in very high hazard zone', 0],
            [u'Population in low hazard zone', 8443],
            [u'Total affected population', 9837],
            [u'Unaffected population', 0],
            [u'Total population', 9837],
            [u'Population needing evacuation <sup>1</sup>', 9837]
        ]
        self.assertListEqual(
            expected, impact.impact_data['impact summary']['fields'])
Exemplo n.º 5
0
    def test_run(self):
        """TestVolcanoPointPopulationFunction: Test running the IF."""
        merapi_point_path = standard_data_path('hazard', 'volcano_point.shp')
        population_path = standard_data_path('exposure',
                                             'pop_binary_raster_20_20.asc')

        merapi_point_layer = read_layer(merapi_point_path)
        population_layer = read_layer(population_path)

        impact_function = VolcanoPointPopulationFunction.instance()

        # Run merapi point
        impact_function.hazard = SafeLayer(merapi_point_layer)
        impact_function.exposure = SafeLayer(population_layer)
        impact_function._prepare()
        impact_function.run()
        impact_layer = impact_function.impact
        # Check the question
        expected_question = (
            'In the event of a volcano point how many people might be '
            'impacted?')
        self.assertEqual(expected_question, impact_function.question)
        # Count by hand
        expected_affected_population = 200
        result = numpy.nansum(impact_layer.get_data())
        self.assertEqual(expected_affected_population, result)
    def test_run(self):
        function = FloodRasterRoadsFunction.instance()

        hazard_path = standard_data_path('hazard',
                                         'continuous_flood_20_20.asc')
        exposure_path = standard_data_path('exposure', 'roads.shp')
        # noinspection PyCallingNonCallable
        hazard_layer = QgsRasterLayer(hazard_path, 'Flood')
        # noinspection PyCallingNonCallable
        exposure_layer = QgsVectorLayer(exposure_path, 'Roads', 'ogr')

        # Let's set the extent to the hazard extent
        extent = hazard_layer.extent()
        rect_extent = [
            extent.xMinimum(),
            extent.yMaximum(),
            extent.xMaximum(),
            extent.yMinimum()
        ]
        function.hazard = SafeLayer(hazard_layer)
        function.exposure = SafeLayer(exposure_layer)
        function.requested_extent = rect_extent
        function.run()
        impact = function.impact

        keywords = impact.get_keywords()
        self.assertEquals(function.target_field, keywords['target_field'])
        expected_inundated_feature = 182
        count = sum(impact.get_data(attribute=function.target_field))
        self.assertEquals(count, expected_inundated_feature)
Exemplo n.º 7
0
    def test_run(self):
        """TestVolcanoPolygonPopulationFunction: Test running the IF."""
        merapi_krb_path = standard_data_path('hazard', 'volcano_krb.shp')
        population_path = standard_data_path(
            'exposure', 'pop_binary_raster_20_20.asc')

        merapi_krb_layer = read_layer(merapi_krb_path)
        population_layer = read_layer(population_path)

        impact_function = VolcanoPolygonPopulationFunction.instance()

        # 2. Run merapi krb
        impact_function.hazard = SafeLayer(merapi_krb_layer)
        impact_function.exposure = SafeLayer(population_layer)
        impact_function.run()
        impact_layer = impact_function.impact
        # Check the question
        expected_question = (
            'In the event of volcano krb how many population might need '
            'evacuation?')
        self.assertEqual(expected_question, impact_function.question)
        # Count by hand
        expected_affected_population = 181
        result = numpy.nansum(impact_layer.get_data())
        self.assertEqual(expected_affected_population, result)
Exemplo n.º 8
0
    def test_run(self):
        """TestVolcanoPointPopulationFunction: Test running the IF."""
        merapi_point_path = standard_data_path('hazard', 'volcano_point.shp')
        population_path = standard_data_path(
            'exposure', 'pop_binary_raster_20_20.asc')

        merapi_point_layer = read_layer(merapi_point_path)
        population_layer = read_layer(population_path)

        impact_function = VolcanoPointPopulationFunction.instance()

        # Run merapi point
        impact_function.hazard = SafeLayer(merapi_point_layer)
        impact_function.exposure = SafeLayer(population_layer)
        impact_function._prepare()
        impact_function.run()
        impact_layer = impact_function.impact
        # Check the question
        expected_question = (
            'In the event of a volcano point how many people might be '
            'impacted?')
        self.assertEqual(expected_question, impact_function.question)
        # Count by hand
        expected_affected_population = 200
        result = numpy.nansum(impact_layer.get_data())
        self.assertEqual(expected_affected_population, result)
Exemplo n.º 9
0
    def test_run(self):
        impact_function = FloodRasterBuildingFunction.instance()

        hazard_path = standard_data_path("hazard", "continuous_flood_20_20.asc")
        exposure_path = standard_data_path("exposure", "buildings.shp")
        hazard_layer = read_layer(hazard_path)
        exposure_layer = read_layer(exposure_path)

        impact_function.hazard = SafeLayer(hazard_layer)
        impact_function.exposure = SafeLayer(exposure_layer)
        impact_function.run()
        impact_layer = impact_function.impact

        # Extract calculated result
        impact_data = impact_layer.get_data()
        self.assertEqual(len(impact_data), 181)

        # 1 = inundated, 2 = wet, 3 = dry
        expected_result = {1: 64, 2: 117, 3: 0}

        result = {1: 0, 2: 0, 3: 0}
        for feature in impact_data:
            inundated_status = feature[impact_function.target_field]
            result[inundated_status] += 1

        message = "Expecting %s, but it returns %s" % (expected_result, result)
        self.assertEqual(expected_result, result, message)
Exemplo n.º 10
0
    def test_run(self):
        function = FloodPolygonRoadsFunction.instance()

        hazard_path = standard_data_path('hazard',
                                         'flood_multipart_polygons.shp')
        exposure_path = standard_data_path('exposure', 'roads.shp')
        # noinspection PyCallingNonCallable
        hazard_layer = QgsVectorLayer(hazard_path, 'Flood', 'ogr')
        # noinspection PyCallingNonCallable
        exposure_layer = QgsVectorLayer(exposure_path, 'Roads', 'ogr')

        # Let's set the extent to the hazard extent
        extent = hazard_layer.extent()
        rect_extent = [
            extent.xMinimum(),
            extent.yMaximum(),
            extent.xMaximum(),
            extent.yMinimum()
        ]
        function.hazard = SafeLayer(hazard_layer)
        function.exposure = SafeLayer(exposure_layer)
        function.requested_extent = rect_extent
        function.run()
        impact = function.impact

        # Count of flooded objects is calculated "by the hands"
        # the count = 69
        expected_feature_total = 69
        count = sum(impact.get_data(attribute=function.target_field))
        message = 'Expecting %s, but it returns %s' % (expected_feature_total,
                                                       count)
        self.assertEquals(count, expected_feature_total, message)
Exemplo n.º 11
0
    def test_set_layers(self):
        """
        Test set up aggregator's layers work
        """

        hazard = QgsVectorLayer(
            standard_data_path('hazard', 'multipart_polygons_osm_4326.shp'),
            'hazard', 'ogr')
        exposure = QgsVectorLayer(
            standard_data_path('exposure', 'buildings_osm_4326.shp'), 'impact',
            'ogr')

        aggregation_layer = QgsVectorLayer(
            os.path.join(BOUNDDATA, 'kabupaten_jakarta.shp'),
            'test aggregation', 'ogr')

        # Test in
        #   aoi mode (use None)
        #   not aoi mode (use aggregation_layer)
        for agg_layer in [None, aggregation_layer]:
            aggregator = Aggregator(self.extent, None)
            aggregator.set_layers(hazard, exposure)
            self.assertEquals(aggregator.exposure_layer, exposure)
            self.assertEquals(aggregator.hazard_layer, hazard)
            layer = aggregator.layer
            extent = layer.extent()
            x_min, y_min, x_max, y_max = \
                extent.xMinimum(), extent.yMinimum(), \
                extent.xMaximum(), extent.yMaximum()
            self.assertAlmostEquals(self.extent[0], x_min)
            self.assertAlmostEquals(self.extent[1], y_min)
            self.assertAlmostEquals(self.extent[2], x_max)
            self.assertAlmostEquals(self.extent[3], y_max)
            self.assertTrue(aggregator.safe_layer.is_vector)
            _ = agg_layer
Exemplo n.º 12
0
    def test_run(self):
        """TestVolcanoPointBuildingFunction: Test running the IF."""
        volcano_path = standard_data_path('hazard', 'volcano_point.shp')
        building_path = standard_data_path('exposure', 'buildings.shp')

        hazard_layer = read_layer(volcano_path)
        exposure_layer = read_layer(building_path)

        impact_function = VolcanoPointBuildingFunction.instance()
        impact_function.hazard = SafeLayer(hazard_layer)
        impact_function.exposure = SafeLayer(exposure_layer)
        impact_function._prepare()
        impact_function.run()
        impact_layer = impact_function.impact

        # Check the question
        expected_question = (
            'In the event of volcano point 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)

        # The buildings should all be categorised into 3000 zone
        zone_sum = sum(impact_layer.get_data(
            attribute=impact_function.target_field))
        expected_sum = 3 * 181
        message = 'Expecting %s, but it returns %s' % (expected_sum, zone_sum)
        self.assertEqual(zone_sum, expected_sum, message)
    def test_run(self):
        function = FloodRasterRoadsFunction.instance()

        hazard_path = standard_data_path(
            'hazard', 'continuous_flood_20_20.asc')
        exposure_path = standard_data_path('exposure', 'roads.shp')
        # noinspection PyCallingNonCallable
        hazard_layer = QgsRasterLayer(hazard_path, 'Flood')
        # noinspection PyCallingNonCallable
        exposure_layer = QgsVectorLayer(exposure_path, 'Roads', 'ogr')

        # Let's set the extent to the hazard extent
        extent = hazard_layer.extent()
        rect_extent = [
            extent.xMinimum(), extent.yMaximum(),
            extent.xMaximum(), extent.yMinimum()]
        function.hazard = SafeLayer(hazard_layer)
        function.exposure = SafeLayer(exposure_layer)
        function.requested_extent = rect_extent
        function.run()
        impact = function.impact

        keywords = impact.get_keywords()
        self.assertEquals(function.target_field, keywords['target_field'])
        expected_inundated_feature = 182
        count = sum(impact.get_data(attribute=function.target_field))
        self.assertEquals(count, expected_inundated_feature)
Exemplo n.º 14
0
    def test_run(self):
        function = ContinuousHazardPopulationFunction.instance()

        hazard_path = standard_data_path(
            'hazard', 'continuous_flood_20_20.asc')
        exposure_path = standard_data_path(
            'exposure', 'pop_binary_raster_20_20.asc')
        hazard_layer = read_layer(hazard_path)
        exposure_layer = read_layer(exposure_path)

        function.hazard = SafeLayer(hazard_layer)
        function.exposure = SafeLayer(exposure_layer)
        function.run()
        impact = function.impact

        # print "keywords", keywords
        keywords = impact.get_keywords()
        total_needs_full = keywords['total_needs']
        total_needs_weekly = OrderedDict([
            [x['table name'], x['amount']] for x in
            total_needs_full['weekly']
        ])
        total_needs_single = OrderedDict([
            [x['table name'], x['amount']] for x in
            total_needs_full['single']
        ])

        self.assertEqual(total_needs_weekly['Rice [kg]'], 336)
        self.assertEqual(total_needs_weekly['Drinking Water [l]'], 2100)
        self.assertEqual(total_needs_weekly['Clean Water [l]'], 8040)
        self.assertEqual(total_needs_weekly['Family Kits'], 24)
        self.assertEqual(total_needs_single['Toilets'], 6)
Exemplo n.º 15
0
    def test_is_polygonal_layer(self):
        """Test we can get the correct attributes back"""
        # Polygon layer
        layer = clone_shp_layer(
            name='district_osm_jakarta',
            include_keywords=True,
            source_directory=standard_data_path('boundaries'))
        message = 'isPolygonLayer, %s layer should be polygonal' % layer
        self.assertTrue(is_polygon_layer(layer), message)

        # Point layer
        layer = clone_shp_layer(
            name='volcano_point',
            include_keywords=True,
            source_directory=standard_data_path('hazard'))
        message = '%s layer should be polygonal' % layer
        self.assertFalse(is_polygon_layer(layer), message)

        layer = clone_raster_layer(
            name='padang_tsunami_mw8',
            extension='.tif',
            include_keywords=True,
            source_directory=standard_data_path('hazard')
        )
        message = ('%s raster layer should not be polygonal' % layer)
        self.assertFalse(is_polygon_layer(layer), message)
Exemplo n.º 16
0
    def test_run(self):
        impact_function = FloodRasterBuildingFunction.instance()

        hazard_path = standard_data_path('hazard',
                                         'continuous_flood_20_20.asc')
        exposure_path = standard_data_path('exposure', 'buildings.shp')
        hazard_layer = read_layer(hazard_path)
        exposure_layer = read_layer(exposure_path)

        impact_function.hazard = SafeLayer(hazard_layer)
        impact_function.exposure = SafeLayer(exposure_layer)
        impact_function.run()
        impact_layer = impact_function.impact

        # Extract calculated result
        impact_data = impact_layer.get_data()
        self.assertEqual(len(impact_data), 181)

        # 1 = inundated, 2 = wet, 3 = dry
        expected_result = {1: 64, 2: 117, 3: 0}

        result = {1: 0, 2: 0, 3: 0}
        for feature in impact_data:
            inundated_status = feature[impact_function.target_field]
            result[inundated_status] += 1

        message = 'Expecting %s, but it returns %s' % (expected_result, result)
        self.assertEqual(expected_result, result, message)
Exemplo n.º 17
0
    def test_run(self):
        """TestVolcanoPolygonPopulationFunction: Test running the IF."""
        merapi_krb_path = standard_data_path('hazard', 'volcano_krb.shp')
        population_path = standard_data_path('exposure',
                                             'pop_binary_raster_20_20.asc')

        merapi_krb_layer = read_layer(merapi_krb_path)
        population_layer = read_layer(population_path)

        impact_function = VolcanoPolygonPopulationFunction.instance()

        # 2. Run merapi krb
        impact_function.hazard = SafeLayer(merapi_krb_layer)
        impact_function.exposure = SafeLayer(population_layer)
        impact_function.run()
        impact_layer = impact_function.impact
        # Check the question
        expected_question = (
            'In the event of volcano krb how many population might need '
            'evacuation?')
        self.assertEqual(expected_question, impact_function.question)
        # Count by hand
        expected_affected_population = 181
        result = numpy.nansum(impact_layer.get_data())
        self.assertEqual(expected_affected_population, result)
Exemplo n.º 18
0
    def test_run(self):
        """TestVolcanoPointBuildingFunction: Test running the IF."""
        volcano_path = standard_data_path('hazard', 'volcano_point.shp')
        building_path = standard_data_path('exposure', 'buildings.shp')

        hazard_layer = read_layer(volcano_path)
        exposure_layer = read_layer(building_path)

        impact_function = VolcanoPointBuildingFunction.instance()
        impact_function.hazard = SafeLayer(hazard_layer)
        impact_function.exposure = SafeLayer(exposure_layer)
        impact_function._prepare()
        impact_function.run()
        impact_layer = impact_function.impact

        # Check the question
        expected_question = (
            'In the event of volcano point how many buildings might be '
            'affected?')
        self.assertEqual(expected_question, impact_function.question)

        # The buildings should all be categorised into 3000 zone
        zone_sum = sum(
            impact_layer.get_data(attribute=impact_function.target_field))
        expected_sum = 3 * 181
        message = 'Expecting %s, but it returns %s' % (expected_sum, zone_sum)
        self.assertEqual(zone_sum, expected_sum, message)
Exemplo n.º 19
0
    def test_run_without_population_field(self):
        impact_function = AshRasterPlacesFunction.instance()

        hazard_path = standard_data_path("hazard", "ash_raster_wgs84.tif")
        exposure_path = standard_data_path("exposure", "places.shp")
        hazard_layer = QgsRasterLayer(hazard_path, "Ash")
        exposure_layer = QgsVectorLayer(exposure_path, "Places", "ogr")
        impact_function.hazard = hazard_layer
        impact_function.exposure = exposure_layer

        # 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.requested_extent = rect_extent
        impact_function.run()
        impact_layer = impact_function.impact

        # Extract calculated result
        impact_data = impact_layer.get_data()

        # 1 = inundated, 2 = wet, 3 = dry
        expected_result = {0: 0, 1: 135, 2: 62, 3: 1, 4: 0}

        result = {0: 0, 1: 0, 2: 0, 3: 0, 4: 0}
        for feature in impact_data:
            inundated_status = feature[impact_function.target_field]
            result[inundated_status] += 1
        self.assertDictEqual(expected_result, result)
Exemplo n.º 20
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)
    def test_run_point_exposure(self):
        """Run the IF for point exposure.

        See https://github.com/AIFDR/inasafe/issues/2156.
        """
        generic_polygon_path = standard_data_path(
            'hazard', 'classified_generic_polygon.shp')
        building_path = standard_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?')
        self.assertEqual(expected_question, impact_function.question)

        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)
Exemplo n.º 22
0
    def test_load_layer_from_uri(self):
        """Test we can load a layer with different parameters."""
        # Without provider
        path = standard_data_path(
            'gisv4', 'aggregation', 'small_grid.geojson')
        layer, purpose = load_layer(path)
        self.assertTrue(layer.isValid())
        self.assertEqual(layer.name(), 'small_grid')
        self.assertEqual(purpose, 'aggregation')

        layer, purpose = load_layer(path, 'foo')
        self.assertEqual(layer.name(), 'foo')

        # With internal URI
        internal_uri = full_layer_uri(layer)
        self.assertTrue(
            internal_uri.endswith('small_grid.geojson|qgis_provider=ogr'),
            internal_uri
        )
        layer, purpose = load_layer(full_layer_uri(layer))
        self.assertTrue(layer.isValid())
        self.assertEqual(layer.name(), 'small_grid')
        self.assertEqual(purpose, 'aggregation')

        # path plus extra layer parameter
        path = standard_data_path(
            'gisv4', 'aggregation', 'small_grid.geojson')
        path += '|layerid=0'
        layer, purpose = load_layer(path)
        self.assertTrue(layer.isValid())
        self.assertEqual(layer.name(), 'small_grid')
        self.assertEqual(purpose, 'aggregation')

        # CSV
        path = standard_data_path(
            'gisv4', 'impacts', 'exposure_summary_table.csv')
        layer, purpose = load_layer(path)
        self.assertTrue(layer.isValid())
        self.assertEqual(layer.name(), 'exposure_summary_table')
        self.assertEqual(purpose, 'undefined')
        self.assertEqual(len(layer.fields()), 4)

        # QLR
        # This QLR contains a file based reference layer to
        # small_grid.geojson, and it should work the same way
        # as if we load small_grid.geojson
        # In practice we could put the non file-based layer (PostGIS/WFS)
        # and load it from QLR
        path = standard_data_path(
            'gisv4', 'aggregation', 'small_grid.qlr')
        layer, purpose = load_layer(path)
        self.assertTrue(layer.isValid())
        self.assertEqual(layer.name(), 'small_grid')
        self.assertEqual(purpose, 'aggregation')
    def test_run_point_exposure(self):
        """Run the IF for point exposure.

        See https://github.com/AIFDR/inasafe/issues/2156.
        """
        generic_polygon_path = standard_data_path(
            'hazard', 'classified_generic_polygon.shp')
        building_path = standard_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)
Exemplo n.º 24
0
    def test_raster_to_vector_and_line_intersection(self):
        """Test the core part of the analysis.

        1. Test creation of spatial index of flood cells
        2. Test intersection of flood cells with roads layer
        """

        raster_name = standard_data_path(
            'hazard',
            'tsunami_wgs84.tif')
        exposure_name = standard_data_path(
            'exposure',
            'roads_osm_4326.shp')

        raster = QgsRasterLayer(raster_name, 'Flood')
        exposure = QgsVectorLayer(exposure_name, 'Exposure', 'ogr')

        ranges = OrderedDict()
        ranges[0] = [0, 1]
        ranges[1] = [1, 2]
        ranges[2] = [2, 100]
        index, flood_cells_map = _raster_to_vector_cells(
            raster, ranges, exposure.crs())

        self.assertEqual(len(flood_cells_map), 4198)
        rect_with_all_cells = raster.extent()
        rect_with_4_cells = QgsRectangle(106.824, -6.177, 106.825, -6.179)
        rect_with_0_cells = QgsRectangle(106.818, -6.168, 106.828, -6.175)
        self.assertEqual(len(index.intersects(rect_with_all_cells)), 4198)
        self.assertEqual(len(index.intersects(rect_with_4_cells)), 43)
        self.assertEqual(len(index.intersects(rect_with_0_cells)), 504)

        layer = create_layer(exposure)
        new_field = QgsField('flooded', QVariant.Int)
        layer.dataProvider().addAttributes([new_field])

        request = QgsFeatureRequest()
        _intersect_lines_with_vector_cells(
            exposure, request, index, flood_cells_map, layer, 'flooded')

        feature_count = layer.featureCount()
        self.assertEqual(feature_count, 388)

        flooded = 0
        iterator = layer.getFeatures()
        for feature in iterator:
            attributes = feature.attributes()
            if attributes[3] == 1:
                flooded += 1
        self.assertEqual(flooded, 40)
Exemplo n.º 25
0
    def test_write_read_iso_19115_metadata(self):
        """Test for write_read_iso_19115_metadata."""
        keywords = {
            # 'date': '26-03-2015 14:03',
            'exposure': 'structure',
            'keyword_version': inasafe_keyword_version,
            'layer_geometry': 'polygon',
            'layer_mode': 'classified',
            'layer_purpose': 'exposure',
            'license': 'Open Data Commons Open Database License (ODbL)',
            'source': 'OpenStreetMap - www.openstreetmap.org',
            'title': 'Buildings',
            'inasafe_fields': {
                'youth_count_field': [
                    'POP_F_0-4',
                    'POP_F_5-6',
                    'POP_F_7-12',
                ]
            }
        }
        layer = clone_shp_layer(
            name='buildings',
            include_keywords=False,
            source_directory=standard_data_path('exposure'))
        write_iso19115_metadata(layer.source(), keywords)

        read_metadata = read_iso19115_metadata(layer.source())
        self.assertDictEqual(keywords, read_metadata)

        # Version 3.5
        keywords = {
            # 'date': '26-03-2015 14:03',
            'exposure': 'structure',
            'keyword_version': '3.2',
            'layer_geometry': 'polygon',
            'layer_mode': 'classified',
            'layer_purpose': 'exposure',
            'license': 'Open Data Commons Open Database License (ODbL)',
            'source': 'OpenStreetMap - www.openstreetmap.org',
            'structure_class_field': 'TYPE',
            'title': 'Buildings'
        }
        layer = clone_shp_layer(
            name='buildings',
            include_keywords=False,
            source_directory=standard_data_path('exposure'))
        write_iso19115_metadata(layer.source(), keywords, version_35=True)
        read_metadata = read_iso19115_metadata(layer.source(), version_35=True)
        self.assertDictEqual(keywords, read_metadata)
Exemplo n.º 26
0
    def setUp(self):
        self.keyword_io = KeywordIO()

        # SQLite Layer
        uri = QgsDataSourceURI()
        sqlite_building_path = standard_data_path(
            'exposure', 'exposure.sqlite')
        uri.setDatabase(sqlite_building_path)
        uri.setDataSource('', 'buildings_osm_4326', 'Geometry')
        self.sqlite_layer = QgsVectorLayer(
            uri.uri(), 'OSM Buildings', 'spatialite')
        self.expected_sqlite_keywords = {
            'datatype': 'OSM'
        }

        # Raster Layer keywords
        hazard_path = standard_data_path('hazard', 'tsunami_wgs84.tif')
        self.raster_layer, _ = load_layer(hazard_path)
        self.expected_raster_keywords = {
            'hazard_category': 'single_event',
            'title': 'Generic Continuous Flood',
            'hazard': 'flood',
            'continuous_hazard_unit': 'generic',
            'layer_geometry': 'raster',
            'layer_purpose': 'hazard',
            'layer_mode': 'continuous',
            'keyword_version': '3.5'
        }

        # Vector Layer keywords
        vector_path = standard_data_path('exposure', 'buildings_osm_4326.shp')
        self.vector_layer, _ = load_layer(vector_path)
        self.expected_vector_keywords = {
            'keyword_version': '3.5',
            'structure_class_field': 'FLOODED',
            'value_mapping': {},
            'title': 'buildings_osm_4326',
            'layer_geometry': 'polygon',
            'layer_purpose': 'exposure',
            'layer_mode': 'classified',
            'exposure': 'structure',
        }
        # Keyword less layer
        keywordless_path = standard_data_path('other', 'keywordless_layer.shp')
        self.keywordless_layer, _ = load_layer(keywordless_path)
        # Keyword file
        self.keyword_path = standard_data_path(
            'exposure', 'buildings_osm_4326.xml')
Exemplo n.º 27
0
    def setUp(self):
        self.keyword_io = KeywordIO()

        # SQLite Layer
        uri = QgsDataSourceUri()
        sqlite_building_path = standard_data_path(
            'exposure', 'exposure.sqlite')
        uri.setDatabase(sqlite_building_path)
        uri.setDataSource('', 'buildings_osm_4326', 'Geometry')
        self.sqlite_layer = QgsVectorLayer(
            uri.uri(), 'OSM Buildings', 'spatialite')
        self.expected_sqlite_keywords = {
            'datatype': 'OSM'
        }

        # Raster Layer keywords
        hazard_path = standard_data_path('hazard', 'tsunami_wgs84.tif')
        self.raster_layer, _ = load_layer(hazard_path, provider='gdal')
        self.expected_raster_keywords = {
            'hazard_category': 'single_event',
            'title': 'Generic Continuous Flood',
            'hazard': 'flood',
            'continuous_hazard_unit': 'generic',
            'layer_geometry': 'raster',
            'layer_purpose': 'hazard',
            'layer_mode': 'continuous',
            'keyword_version': '3.5'
        }

        # Vector Layer keywords
        vector_path = standard_data_path('exposure', 'buildings_osm_4326.shp')
        self.vector_layer, _ = load_layer(vector_path, provider='ogr')
        self.expected_vector_keywords = {
            'keyword_version': '3.5',
            'value_map': {},
            'title': 'buildings_osm_4326',
            'layer_geometry': 'polygon',
            'layer_purpose': 'exposure',
            'layer_mode': 'classified',
            'exposure': 'structure',
        }
        # Keyword less layer
        keywordless_path = standard_data_path('other', 'keywordless_layer.shp')
        self.keywordless_layer, _ = load_layer(
            keywordless_path, provider='ogr')
        # Keyword file
        self.keyword_path = standard_data_path(
            'exposure', 'buildings_osm_4326.xml')
    def test_converting(self):
        """Test converting grif file to tiff."""
        dialog = ShakemapConverterDialog(PARENT, IFACE)
        dialog.use_output_default.setEnabled(False)
        grid_path = standard_data_path(
            'hazard',
            'shake_data',
            '20131105060809',
            'output',
            'grid.xml')
        output_raster = unique_filename(
            prefix='result_grid',
            suffix='.tif',
            dir=temp_dir('test'))
        dialog.load_result.setEnabled(True)
        dialog.load_result.setChecked(False)
        dialog.input_path.setText(grid_path)
        dialog.nearest_mode.setChecked(True)
        dialog.output_path.setText(output_raster)
        button = dialog.button_box.button(QDialogButtonBox.Ok)
        button.click()

        msg = 'Raster is not created'
        output_path = '%s-nearest.tif' % output_raster[:-4]
        self.assertTrue(os.path.exists(output_path), msg)
Exemplo n.º 29
0
    def test_print_impact_table(self):
        """Test print impact table to pdf."""
        impact_layer_path = standard_data_path(
            'impact', 'population_affected_entire_area.shp')
        layer, _ = load_layer(impact_layer_path)
        # noinspection PyUnresolvedReferences,PyArgumentList
        QgsMapLayerRegistry.instance().addMapLayer(layer)
        # noinspection PyCallingNonCallable
        rect = QgsRectangle(106.8194, -6.2108, 106.8201, -6.1964)
        CANVAS.setExtent(rect)
        CANVAS.refresh()

        template = resources_path(
            'qgis-composer-templates', 'a4-portrait-blue.qpt')
        report = ImpactReport(IFACE, template, layer)
        report.template = template  # just to cover set template
        out_path = unique_filename(
            prefix='test_print_impact_table',
            suffix='.pdf',
            dir=temp_dir('test'))
        report.print_impact_table(out_path)

        # Check the file exists
        message = 'Rendered output does not exist: %s' % out_path
        self.assertTrue(os.path.exists(out_path), message)

        # Check the file is not corrupt
        message = 'The output file %s is corrupt' % out_path
        out_size = os.stat(out_path).st_size
        self.assertTrue(out_size > 0, message)
Exemplo n.º 30
0
    def test_transparency_of_minimum_value(self):
        """Test that transparency of minimum value works when set to 100%
        """
        # This dataset has all cells with value 1.3
        data_path = standard_data_path('other', 'issue126.tif')
        layer, _ = load_layer(data_path)

        # Note the float quantity values below
        style_info = {
            'style_classes': [
                {'colour': '#FFFFFF', 'transparency': 100, 'quantity': 0.0},
                {'colour': '#38A800', 'quantity': 0.038362596547925065,
                 'transparency': 0, 'label': u'Rendah [0 orang/sel]'},
                {'colour': '#79C900', 'transparency': 0,
                 'quantity': 0.07672519309585013},
                {'colour': '#CEED00', 'transparency': 0,
                 'quantity': 0.1150877896437752},
                {'colour': '#FFCC00', 'quantity': 0.15345038619170026,
                 'transparency': 0, 'label': u'Sedang [0 orang/sel]'},
                {'colour': '#FF6600', 'transparency': 0,
                 'quantity': 0.19181298273962533},
                {'colour': '#FF0000', 'transparency': 0,
                 'quantity': 0.23017557928755039},
                {'colour': '#7A0000', 'quantity': 0.26853817583547546,
                 'transparency': 0, 'label': u'Tinggi [0 orang/sel]'}]}

        try:
            setRasterStyle(layer, style_info)
        except Exception, e:
            message = '\nCould not create raster style'
            e.args = (e.args[0] + message,)
            raise
Exemplo n.º 31
0
 def test_update_keywords(self):
     """Test append file keywords with update_keywords method."""
     self.maxDiff = None
     layer = clone_raster_layer(
         name='tsunami_wgs84',
         extension='.tif',
         include_keywords=True,
         source_directory=standard_data_path('hazard'))
     new_keywords = {'hazard_category': 'multiple_event'}
     self.keyword_io.update_keywords(layer, new_keywords)
     keywords = self.keyword_io.read_keywords(layer)
     expected_keywords = {
         'hazard_category': 'multiple_event',
         'title': 'Tsunami',
         'hazard': 'tsunami',
         'continuous_hazard_unit': 'metres',
         'layer_geometry': 'raster',
         'layer_purpose': 'hazard',
         'layer_mode': 'continuous',
         'keyword_version': inasafe_keyword_version
     }
     expected_keywords = {
         k: get_unicode(v)
         for k, v in expected_keywords.iteritems()
     }
     self.assertDictEqual(keywords, expected_keywords)
Exemplo n.º 32
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)
Exemplo n.º 33
0
    def test_relative_path(self):
        """Test we calculate the relative paths correctly when saving scenario.
        """
        result, message = setup_scenario(self.DOCK,
                                         hazard='Classified Flood',
                                         exposure='Population')
        self.assertTrue(result, message)
        fake_dir = standard_data_path()
        scenario_file = unique_filename(prefix='scenarioTest',
                                        suffix='.txt',
                                        dir=fake_dir)
        exposure_layer = self.DOCK.get_exposure_layer().publicSource()
        hazard_layer = self.DOCK.get_hazard_layer().publicSource()

        relative_exposure = self.save_scenario_dialog.relative_path(
            scenario_file, exposure_layer)
        relative_hazard = self.save_scenario_dialog.relative_path(
            scenario_file, hazard_layer)

        if 'win32' in sys.platform:
            # windows
            self.assertEqual('exposure\\pop_binary_raster_20_20.asc',
                             relative_exposure)
            self.assertEqual('hazard\\classified_flood_20_20.asc',
                             relative_hazard)

        else:
            self.assertEqual('exposure/pop_binary_raster_20_20.asc',
                             relative_exposure)
            self.assertEqual('hazard/classified_flood_20_20.asc',
                             relative_hazard)
Exemplo n.º 34
0
    def test_retrieve_exposure_classes_lists(self):
        """Test retrieve_exposure_classes_lists method.

        .. versionadded:: 4.0
        """
        layer_paths = self.layer_paths_list
        expected_classes_lists = [
            None, None, None, None, generic_structure_classes['classes'],
            generic_structure_classes['classes'], None,
            generic_road_classes['classes']
        ]

        for layer_path, expected_classes in zip(layer_paths,
                                                expected_classes_lists):
            path = standard_data_path(*layer_path)
            layer, _ = load_layer(path)
            actual_classes = retrieve_exposure_classes_lists(layer)
            try:
                self.assertEqual(expected_classes, actual_classes)
            except Exception as e:
                LOGGER.error('Layer path: {path}'.format(path=path))
                LOGGER.error(
                    'Expected {classes}'.format(classes=expected_classes))
                LOGGER.error('Actual {classes}'.format(classes=actual_classes))
                raise e
Exemplo n.º 35
0
    def test_retrieve_exposure_classes_lists(self):
        """Test retrieve_exposure_classes_lists method.

        .. versionadded:: 4.0
        """
        layer_paths = self.layer_paths_list
        expected_classes_lists = [
            None,
            None,
            None,
            None,
            generic_structure_classes['classes'],
            generic_structure_classes['classes'],
            None,
            generic_road_classes['classes']
        ]

        for layer_path, expected_classes in zip(
                layer_paths, expected_classes_lists):
            path = standard_data_path(*layer_path)
            layer, _ = load_layer(path)
            actual_classes = retrieve_exposure_classes_lists(layer)
            try:
                self.assertEqual(
                    expected_classes, actual_classes)
            except Exception as e:
                LOGGER.error('Layer path: {path}'.format(
                    path=path))
                LOGGER.error('Expected {classes}'.format(
                    classes=expected_classes))
                LOGGER.error('Actual {classes}'.format(
                    classes=actual_classes))
                raise e
Exemplo n.º 36
0
 def test_error_message(self):
     """Test we can send error messages to the message viewer."""
     text = self.fake_error()
     control_file_path = standard_data_path('control', 'files',
                                            'test-error-message.txt')
     expected_result = open(control_file_path).read().replace('\n', '')
     self.assertEqual(text, expected_result)
Exemplo n.º 37
0
 def test_update_keywords(self):
     """Test append file keywords with update_keywords method."""
     self.maxDiff = None
     layer = clone_raster_layer(
         name='tsunami_wgs84',
         extension='.tif',
         include_keywords=True,
         source_directory=standard_data_path('hazard'))
     new_keywords = {
         'hazard_category': 'multiple_event'
     }
     self.keyword_io.update_keywords(layer, new_keywords)
     keywords = self.keyword_io.read_keywords(layer)
     expected_keywords = {
         'hazard_category': 'multiple_event',
         'title': 'Tsunami',
         'hazard': 'tsunami',
         'continuous_hazard_unit': 'metres',
         'layer_geometry': 'raster',
         'layer_purpose': 'hazard',
         'layer_mode': 'continuous',
         'keyword_version': inasafe_keyword_version
     }
     expected_keywords = {
         k: get_unicode(v) for k, v in expected_keywords.iteritems()
     }
     self.assertDictEqual(keywords, expected_keywords)
Exemplo n.º 38
0
    def test_converting(self):
        """Test converting grif file to tiff."""
        dialog = ShakemapConverterDialog(PARENT, IFACE)
        dialog.use_output_default.setEnabled(False)
        grid_path = standard_data_path(
            'hazard',
            'shake_data',
            '20131105060809',
            'output',
            'grid.xml')
        output_raster = unique_filename(
            prefix='result_grid',
            suffix='.tif',
            dir=temp_dir('test'))
        dialog.load_result.setEnabled(True)
        dialog.load_result.setChecked(False)
        dialog.input_path.setText(grid_path)
        dialog.nearest_mode.setChecked(True)
        dialog.output_path.setText(output_raster)
        button = dialog.button_box.button(QDialogButtonBox.Ok)
        button.click()

        msg = 'Raster is not created'
        output_path = '%s-nearest.tif' % output_raster[:-4]
        self.assertTrue(os.path.exists(output_path), msg)
Exemplo n.º 39
0
    def test_is_polygonal_layer(self):
        """Test we can get the correct attributes back"""
        # Polygon layer
        layer = load_test_vector_layer(
            'aggregation',
            'district_osm_jakarta.geojson',
            clone=True
        )
        message = 'isPolygonLayer, %s layer should be polygonal' % layer
        self.assertTrue(is_polygon_layer(layer), message)

        # Point layer
        layer = load_test_vector_layer('hazard', 'volcano_point.geojson')
        message = '%s layer should be polygonal' % layer
        self.assertFalse(is_polygon_layer(layer), message)

        # Raster layer
        layer = clone_raster_layer(
            name='earthquake',
            extension='.tif',
            include_keywords=True,
            source_directory=standard_data_path('hazard')
        )
        message = ('%s raster layer should not be polygonal' % layer)
        self.assertFalse(is_polygon_layer(layer), message)
Exemplo n.º 40
0
    def test_stacktrace_html(self):
        """Stack traces can be caught and rendered as html
        """

        # This is about general exception handling, so ok to use catch-all
        # pylint: disable=W0703
        try:
            bbox_intersection('aoeu', 'oaeu', [])
        except Exception, e:
            # Display message and traceback

            message = get_error_message(e)
            # print message
            message = message.to_text()
            self.assertIn(str(e), message)
            self.assertIn('line', message)
            self.assertIn('file', message)

            message = get_error_message(e)
            message = message.to_html()
            assert str(e) in message

            message = message.decode('string_escape')
            control_file_path = standard_data_path(
                'control',
                'files',
                'test-stacktrace-html.txt')
            expected_results = open(control_file_path).read().replace('\n', '')
            self.assertIn(expected_results, message)
Exemplo n.º 41
0
    def test_read_existing_geopackage(self):
        """Test we can read an existing geopackage."""
        path = standard_data_path('other', 'jakarta.gpkg')
        import os
        path = os.path.normpath(os.path.normcase(os.path.abspath(path)))
        geopackage = QFileInfo(path)
        data_store = GeoPackage(geopackage)

        # We should have 3 layers in this geopackage.
        self.assertEqual(len(data_store.layers()), 3)

        # Test we can load a vector layer.
        roads = QgsVectorLayer(
            data_store.layer_uri('roads'),
            'Test',
            'ogr'
        )
        self.assertTrue(roads.isValid())

        # Test we can load a raster layers.
        # This currently fails on windows...
        # So we have decorated it with expected fail on windows
        # Should pass on other platforms.
        path = data_store.layer_uri('flood')
        flood = QgsRasterLayer(path, 'flood')
        self.assertTrue(flood.isValid())
Exemplo n.º 42
0
    def test_layer_hazard_classification(self):
        """Test layer_hazard_classification method.

        .. versionadded:: 4.0
        """
        layer_paths = self.layer_paths_list
        expected_classifications = [
            generic_hazard_classes,
            earthquake_mmi_scale,
            tsunami_hazard_classes,
            cyclone_au_bom_hazard_classes,
            None,
            None,
            None,
            None,
        ]

        for layer_path, expected_classification in zip(
                layer_paths, expected_classifications):
            path = standard_data_path(*layer_path)
            layer, _ = load_layer(path)
            # inject classification keyword
            if expected_classification:
                layer.keywords['classification'] = (
                    expected_classification['key'])
            actual_classification = layer_hazard_classification(layer)
            try:
                self.assertEqual(expected_classification,
                                 actual_classification)
            except Exception as e:
                LOGGER.error('Layer path: {path}'.format(path=path))
                LOGGER.error(
                    'Expected {name}'.format(**expected_classification))
                LOGGER.error('Actual {name}'.format(**actual_classification))
                raise e
Exemplo n.º 43
0
    def test_read_iso19115_metadata(self):
        """Test for read_iso19115_metadata method."""
        exposure_layer = clone_shp_layer(
            name='buildings',
            include_keywords=False,
            source_directory=standard_data_path('exposure'))
        keywords = {
            # 'date': '26-03-2015 14:03',
            'exposure': 'structure',
            'keyword_version': inasafe_keyword_version,
            'layer_geometry': 'polygon',
            'layer_mode': 'classified',
            'layer_purpose': 'exposure',
            'license': 'Open Data Commons Open Database License (ODbL)',
            'source': 'OpenStreetMap - www.openstreetmap.org',
            'title': 'Buildings'
        }
        write_iso19115_metadata(exposure_layer.source(), keywords)

        read_metadata = read_iso19115_metadata(exposure_layer.source())

        for key in set(keywords.keys()) & set(read_metadata.keys()):
            self.assertEqual(read_metadata[key], keywords[key])
        for key in set(keywords.keys()) - set(read_metadata.keys()):
            message = 'key %s is not found in ISO metadata' % key
            self.assertEqual(read_metadata[key], keywords[key], message)
        for key in set(read_metadata.keys()) - set(keywords.keys()):
            message = 'key %s is not found in old keywords' % key
            self.assertEqual(read_metadata[key], keywords[key], message)
Exemplo n.º 44
0
    def test_polygonize_thresholds(self):
        """Test polygonize raster using gdal with some thresholds."""

        raster_path = standard_data_path('hazard', 'jakarta_flood_design.tif')

        inside_file_name, inside_layer_name, outside_file_name, \
            outside_layer_name = polygonize_thresholds(
                raster_path, 0.5)

        # Syntactic sugar to ignore unused vars.
        _ = inside_layer_name
        _ = outside_layer_name

        driver = ogr.GetDriverByName('ESRI Shapefile')

        data_source = driver.Open(inside_file_name, 0)
        layer = data_source.GetLayer()
        feature_count = layer.GetFeatureCount()
        # print 'inside %s' % (inside_file_name)
        self.assertEquals(feature_count, 3)

        data_source2 = driver.Open(outside_file_name, 0)
        layer2 = data_source2.GetLayer()
        feature_count2 = layer2.GetFeatureCount()
        # print 'outside %s' % (outside_file_name)
        self.assertEquals(feature_count2, 1)
Exemplo n.º 45
0
 def setUp(self):
     """Setup before each test."""
     # Download files (which are local files) to realtime-test temp folder
     local_path = os.path.join(temp_dir('realtime-test'))
     shake_data = standard_data_path('hazard', 'shake_data', SHAKE_ID)
     shutil.copytree(
         shake_data, os.path.join(local_path, 'shakemaps', SHAKE_ID))
Exemplo n.º 46
0
    def test_read_existing_geopackage(self):
        """Test we can read an existing geopackage."""
        path = standard_data_path('other', 'jakarta.gpkg')
        import os
        path = os.path.normpath(os.path.normcase(os.path.abspath(path)))
        geopackage = QFileInfo(path)
        data_store = GeoPackage(geopackage)

        # We should have 3 layers in this geopackage.
        self.assertEqual(len(data_store.layers()), 3)

        # Test we can load a vector layer.
        roads = QgsVectorLayer(
            data_store.layer_uri('roads'),
            'Test',
            'ogr'
        )
        self.assertTrue(roads.isValid())

        # Test we can load a raster layers.
        # This currently fails on windows...
        # So we have decorated it with expected fail on windows
        # Should pass on other platforms.
        path = data_store.layer_uri('flood')
        flood = QgsRasterLayer(path, 'flood')
        self.assertTrue(flood.isValid())
Exemplo n.º 47
0
    def test_layer_definition_type(self):
        """Test layer_definition_type method.

        .. versionadded:: 4.0
        """
        layer_paths = self.layer_paths_list
        expected_definitions = [
            hazard_generic,
            hazard_earthquake,
            hazard_tsunami,
            hazard_cyclone,
            exposure_structure,
            exposure_structure,
            exposure_population,
            exposure_road,
        ]
        for layer_path, expected_definition in zip(layer_paths,
                                                   expected_definitions):
            path = standard_data_path(*layer_path)
            layer, _ = load_layer(path)
            actual_definition = layer_definition_type(layer)
            try:
                self.assertEqual(expected_definition, actual_definition)
            except Exception as e:
                LOGGER.error('Layer path: {path}'.format(path=path))
                LOGGER.error('Expected {name}'.format(**expected_definition))
                LOGGER.error('Actual {name}'.format(**actual_definition))
                raise e
Exemplo n.º 48
0
    def test_relative_path(self):
        """Test we calculate the relative paths correctly when saving scenario.
        """
        result, message = setup_scenario(
            self.DOCK,
            hazard='Classified Flood',
            exposure='Population')
        self.assertTrue(result, message)
        fake_dir = standard_data_path()
        scenario_file = unique_filename(
            prefix='scenarioTest', suffix='.txt', dir=fake_dir)
        exposure_layer = self.DOCK.get_exposure_layer().publicSource()
        hazard_layer = self.DOCK.get_hazard_layer().publicSource()

        relative_exposure = self.save_scenario_dialog.relative_path(
            scenario_file, exposure_layer)
        relative_hazard = self.save_scenario_dialog.relative_path(
            scenario_file, hazard_layer)

        if 'win32' in sys.platform:
            # windows
            self.assertEqual(
                'exposure\\pop_binary_raster_20_20.asc',
                relative_exposure)
            self.assertEqual(
                'hazard\\classified_flood_20_20.asc',
                relative_hazard)

        else:
            self.assertEqual(
                'exposure/pop_binary_raster_20_20.asc',
                relative_exposure)
            self.assertEqual(
                'hazard/classified_flood_20_20.asc',
                relative_hazard)
Exemplo n.º 49
0
    def test_write_read_iso_19115_metadata(self):
        """Test for write_read_iso_19115_metadata."""
        keywords = {
            # 'date': '26-03-2015 14:03',
            'exposure': 'structure',
            'keyword_version': inasafe_keyword_version,
            'layer_geometry': 'polygon',
            'layer_mode': 'classified',
            'layer_purpose': 'exposure',
            'license': 'Open Data Commons Open Database License (ODbL)',
            'source': 'OpenStreetMap - www.openstreetmap.org',
            'title': 'Buildings',
            'inasafe_fields': {
                'youth_count_field': [
                    'POP_F_0-4', 'POP_F_5-6', 'POP_F_7-12',
                ]
            }
        }
        layer = clone_shp_layer(
            name='buildings',
            include_keywords=False,
            source_directory=standard_data_path('exposure'))
        write_iso19115_metadata(layer.source(), keywords)

        read_metadata = read_iso19115_metadata(layer.source())
        self.assertDictEqual(keywords, read_metadata)
Exemplo n.º 50
0
    def test_polygonize(self):
        """Test if we can polygonize a raster using GDAL."""

        raster_path = standard_data_path(
            'hazard', 'classified_flood_20_20.asc')
        driver = ogr.GetDriverByName('ESRI Shapefile')
        expected_field_name = 'my_field'

        shapefile = polygonize(raster_path, 1, expected_field_name)
        data_source = driver.Open(shapefile, 0)
        layer = data_source.GetLayer()

        layer_definition = layer.GetLayerDefn()
        field_name = layer_definition.GetFieldDefn(0).GetName()
        self.assertEqual(field_name, expected_field_name)

        self.assertEquals(layer.GetFeatureCount(), 400)

        layer.SetAttributeFilter('%s = 1' % expected_field_name)
        self.assertEquals(layer.GetFeatureCount(), 133)

        layer.SetAttributeFilter('%s = 2' % expected_field_name)
        self.assertEquals(layer.GetFeatureCount(), 134)

        layer.SetAttributeFilter('%s = 3' % expected_field_name)
        self.assertEquals(layer.GetFeatureCount(), 133)
Exemplo n.º 51
0
    def test_layer_definition_type(self):
        """Test layer_definition_type method.

        .. versionadded:: 4.0
        """
        layer_paths = self.layer_paths_list
        expected_definitions = [
            hazard_generic,
            hazard_earthquake,
            hazard_tsunami,
            hazard_cyclone,
            exposure_structure,
            exposure_structure,
            exposure_population,
            exposure_road,
        ]
        for layer_path, expected_definition in zip(
                layer_paths, expected_definitions):
            path = standard_data_path(*layer_path)
            layer, _ = load_layer(path)
            actual_definition = layer_definition_type(layer)
            try:
                self.assertEqual(expected_definition, actual_definition)
            except Exception as e:
                LOGGER.error('Layer path: {path}'.format(
                    path=path))
                LOGGER.error('Expected {name}'.format(
                    **expected_definition))
                LOGGER.error('Actual {name}'.format(
                    **actual_definition))
                raise e
Exemplo n.º 52
0
    def test_read_iso19115_metadata(self):
        """Test for read_iso19115_metadata method."""
        exposure_layer = clone_shp_layer(
            name='buildings',
            include_keywords=False,
            source_directory=standard_data_path('exposure'))
        keywords = {
            # 'date': '26-03-2015 14:03',
            'exposure': 'structure',
            'keyword_version': inasafe_keyword_version,
            'layer_geometry': 'polygon',
            'layer_mode': 'classified',
            'layer_purpose': 'exposure',
            'license': 'Open Data Commons Open Database License (ODbL)',
            'source': 'OpenStreetMap - www.openstreetmap.org',
            'title': 'Buildings'
        }
        write_iso19115_metadata(exposure_layer.source(), keywords)

        read_metadata = read_iso19115_metadata(exposure_layer.source())

        for key in set(keywords.keys()) & set(read_metadata.keys()):
            self.assertEqual(read_metadata[key], keywords[key])
        for key in set(keywords.keys()) - set(read_metadata.keys()):
            message = 'key %s is not found in ISO metadata' % key
            self.assertEqual(read_metadata[key], keywords[key], message)
        for key in set(read_metadata.keys()) - set(keywords.keys()):
            message = 'key %s is not found in old keywords' % key
            self.assertEqual(read_metadata[key], keywords[key], message)