예제 #1
0
 def test_zonal_with_exact_cell_boundaries(self):
     """Test that zonal stats returns the expected output."""
     raster_layer, _ = load_layer(
         test_data_path('other', 'tenbytenraster.asc'))
     # Note this is a matrix of 11x11 polygons - one per cell
     # and one poly extending beyond to the right of each row
     # and one poly extending beyond the bottom of each col
     vector_layer, _ = load_layer(
         test_data_path('other', 'ten_by_ten_raster_as_polys.shp'))
     result = calculate_zonal_stats(
         raster_layer=raster_layer,
         polygon_layer=vector_layer)
     expected_result = {
         0L: {'count': 1.0, 'sum': 0.0, 'mean': 0.0},  # TL polygon
         9L: {'count': 1.0, 'sum': 9.0, 'mean': 9.0},  # TR polygon
         25L: {'count': 1.0, 'sum': 3.0, 'mean': 3.0},  # Central polygon
         88L: {'count': 1.0, 'sum': 0.0, 'mean': 0.0},  # BL polygon
         108L: {'count': 1.0, 'sum': 9.0, 'mean': 9.0}}  # BR polygon
     # We will just check TL, TR, Middle, BL and BR cells
     result = {
         0L: result[0L],
         9L: result[9L],
         25L: result[25L],
         88L: result[88L],
         108L: result[108L]}
     # noinspection PyPep8Naming
     self.maxDiff = None
     self.assertDictEqual(expected_result, result)
예제 #2
0
 def test_zonal_with_exact_cell_boundaries(self):
     """Test that zonal stats returns the expected output."""
     raster_layer, _ = load_layer(
         test_data_path('other', 'tenbytenraster.asc'))
     # Note this is a matrix of 11x11 polygons - one per cell
     # and one poly extending beyond to the right of each row
     # and one poly extending beyond the bottom of each col
     vector_layer, _ = load_layer(
         test_data_path('other', 'ten_by_ten_raster_as_polys.shp'))
     result = calculate_zonal_stats(raster_layer=raster_layer,
                                    polygon_layer=vector_layer)
     expected_result = {
         0L: {
             'count': 1.0,
             'sum': 0.0,
             'mean': 0.0
         },  # TL polygon
         9L: {
             'count': 1.0,
             'sum': 9.0,
             'mean': 9.0
         },  # TR polygon
         25L: {
             'count': 1.0,
             'sum': 3.0,
             'mean': 3.0
         },  # Central polygon
         88L: {
             'count': 1.0,
             'sum': 0.0,
             'mean': 0.0
         },  # BL polygon
         108L: {
             'count': 1.0,
             'sum': 9.0,
             'mean': 9.0
         }
     }  # BR polygon
     # We will just check TL, TR, Middle, BL and BR cells
     result = {
         0L: result[0L],
         9L: result[9L],
         25L: result[25L],
         88L: result[88L],
         108L: result[108L]
     }
     # noinspection PyPep8Naming
     self.maxDiff = None
     self.assertDictEqual(expected_result, result)
예제 #3
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')
예제 #4
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')
예제 #5
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
예제 #6
0
 def test_convert_grid_to_raster_with_ascii(self):
     """Test converting grid.xml to raster (tif file)"""
     grid_title = 'Earthquake'
     grid_source = 'USGS'
     output_raster = unique_filename(
         prefix='result_grid',
         suffix='.tif',
         dir=temp_dir('test'))
     result = convert_mmi_data(
         GRID_PATH,
         grid_title,
         grid_source,
         algorithm=USE_ASCII,
         output_path=output_raster)
     expected_result = output_raster.replace('.tif', '-%s.tif' % USE_ASCII)
     self.assertEqual(
         result, expected_result,
         'Result path not as expected')
     exists = os.path.exists(result)
     self.assertTrue(exists, 'File result : %s does not exist' % result)
     exists = os.path.exists(result[:-3] + 'xml')
     self.assertTrue(
         exists,
         'File result : %s does not exist' % result[:-3] + 'xml')
     exists = os.path.exists(result[:-3] + 'qml')
     self.assertTrue(
         exists,
         'File result : %s does not exist' % result[:-3] + 'qml')
     tif_file = load_layer(result)[0]
     keywords = tif_file.keywords
     self.assertEqual(keywords['hazard'], hazard_earthquake['key'])
     population_classification = list(keywords['thresholds'][
         exposure_population['key']].keys())[0]
     self.assertEqual(
         population_classification, earthquake_mmi_scale['key'])
예제 #7
0
    def Xtest_print_impact_table(self):
        """Test print impact table to pdf."""
        impact_layer_path = test_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)
예제 #8
0
    def test_issue126(self):
        """Test that non integer transparency ranges fail gracefully.
        .. seealso:: https://github.com/AIFDR/inasafe/issues/126
        """
        # This dataset has all cells with value 1.3
        data_path = test_data_path('other', 'issue126.tif')
        layer, _ = load_layer(data_path)

        # Note the float quantity values below
        style_info = {
            'style_classes': [
                dict(colour='#38A800', quantity=1.1, transparency=100),
                dict(colour='#38A800', quantity=1.4, transparency=0),
                dict(colour='#79C900', quantity=10.1, transparency=0)
            ]
        }

        try:
            setRasterStyle(layer, style_info)
        except Exception, e:
            message = (
                'Setting style info with float based ranges should fail '
                'gracefully.')
            e.args = (e.args[0] + message, )
            raise
예제 #9
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
예제 #10
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 = test_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
예제 #11
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
예제 #12
0
    def test_print_default_template(self):
        """Test printing report to pdf using default template works."""
        impact_layer_path = test_data_path(
            'impact', 'population_affected_entire_area.shp')
        layer, _ = load_layer(impact_layer_path)
        # noinspection PyUnresolvedReferences
        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', 'inasafe-portrait-a4.qpt')
        report = ImpactReport(IFACE, template, layer)
        out_path = unique_filename(
            prefix='map_default_template_test',
            suffix='.pdf',
            dir=temp_dir('test'))
        report.print_map_to_pdf(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)

        # Check the components in composition are default components
        if qgis_version() < 20500:
            safe_logo = report.composition.getComposerItemById(
                'safe-logo').pictureFile()
            north_arrow = report.composition.getComposerItemById(
                'north-arrow').pictureFile()
            org_logo = report.composition.getComposerItemById(
                'organisation-logo').pictureFile()
        else:
            safe_logo = report.composition.getComposerItemById(
                'safe-logo').picturePath()
            north_arrow = report.composition.getComposerItemById(
                'north-arrow').picturePath()
            org_logo = report.composition.getComposerItemById(
                'organisation-logo').picturePath()

        expected_safe_logo = resources_path(
            'img', 'logos', 'inasafe-logo-url.svg')
        expected_north_arrow = resources_path(
            'img', 'north_arrows', 'simple_north_arrow.png')
        expected_org_logo = resources_path('img', 'logos', 'supporters.png')

        message = 'The safe logo path is not the default one'
        self.assertEqual(expected_safe_logo, safe_logo, message)

        message = 'The north arrow path is not the default one'
        self.assertEqual(expected_north_arrow, north_arrow, message)

        message = 'The organisation logo path is not the default one'
        self.assertEqual(expected_org_logo, org_logo, message)
예제 #13
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
예제 #14
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
예제 #15
0
    def test_print_impact_table(self):
        """Test print impact table to pdf."""
        impact_layer_path = test_data_path(
            'impact', 'population_affected_entire_area.shp')
        layer, _ = load_layer(impact_layer_path)
        # noinspection PyUnresolvedReferences
        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',
                                  'inasafe-portrait-a4.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)
예제 #16
0
 def test_convert_grid_to_raster_with_ascii(self):
     """Test converting grid.xml to raster (tif file)"""
     grid_title = 'Earthquake'
     grid_source = 'USGS'
     output_raster = unique_filename(prefix='result_grid',
                                     suffix='.tif',
                                     dir=temp_dir('test'))
     result = convert_mmi_data(GRID_PATH,
                               grid_title,
                               grid_source,
                               algorithm=USE_ASCII,
                               output_path=output_raster)
     expected_result = output_raster.replace('.tif', '-%s.tif' % USE_ASCII)
     self.assertEqual(result, expected_result,
                      'Result path not as expected')
     exists = os.path.exists(result)
     self.assertTrue(exists, 'File result : %s does not exist' % result)
     exists = os.path.exists(result[:-3] + 'xml')
     self.assertTrue(
         exists, 'File result : %s does not exist' % result[:-3] + 'xml')
     exists = os.path.exists(result[:-3] + 'qml')
     self.assertTrue(
         exists, 'File result : %s does not exist' % result[:-3] + 'qml')
     tif_file = load_layer(result)[0]
     keywords = tif_file.keywords
     self.assertEqual(keywords['hazard'], hazard_earthquake['key'])
     population_classification = keywords['thresholds'][
         exposure_population['key']].keys()[0]
     self.assertEqual(population_classification,
                      earthquake_mmi_scale['key'])
예제 #17
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
예제 #18
0
    def test_print_default_template(self):
        """Test printing report to pdf using default template works."""
        impact_layer_path = test_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)
        out_path = unique_filename(prefix='map_default_template_test',
                                   suffix='.pdf',
                                   dir=temp_dir('test'))
        report.print_map_to_pdf(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)

        # Check the components in composition are default components
        if qgis_version() < 20500:
            safe_logo = report.composition.getComposerItemById(
                'inasafe-logo').pictureFile()
            north_arrow = report.composition.getComposerItemById(
                'north-arrow').pictureFile()
            org_logo = report.composition.getComposerItemById(
                'organisation-logo').pictureFile()
        else:
            safe_logo = report.composition.getComposerItemById(
                'white-inasafe-logo').picturePath()
            north_arrow = report.composition.getComposerItemById(
                'north-arrow').picturePath()
            org_logo = report.composition.getComposerItemById(
                'organisation-logo').picturePath()

        expected_safe_logo = resources_path('img', 'logos',
                                            'inasafe-logo-url-white.svg')
        expected_north_arrow = resources_path('img', 'north_arrows',
                                              'simple_north_arrow.png')
        expected_org_logo = resources_path('img', 'logos', 'supporters.png')

        message = ('The safe logo path is not the default one: %s isn\'t %s' %
                   (expected_safe_logo, safe_logo))
        self.assertEqual(expected_safe_logo, safe_logo, message)

        message = 'The north arrow path is not the default one'
        self.assertEqual(expected_north_arrow, north_arrow, message)

        message = 'The organisation logo path is not the default one'
        self.assertEqual(expected_org_logo, org_logo, message)
예제 #19
0
    def setUp(self):
        self.keyword_io = KeywordIO()

        # SQLite Layer
        uri = QgsDataSourceURI()
        sqlite_building_path = test_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 = {
            'category': 'exposure',
            'datatype': 'OSM',
            'subcategory': 'building'
        }

        # Raster Layer keywords
        hazard_path = test_data_path('hazard', 'tsunami_wgs84.tif')
        self.raster_layer, _ = load_layer(hazard_path)
        self.expected_raster_keywords = {
            'hazard_category': 'single_event',
            'title': 'Tsunami',
            'hazard': 'tsunami',
            'continuous_hazard_unit': 'metres',
            'layer_geometry': 'raster',
            'layer_purpose': 'hazard',
            'layer_mode': 'continuous',
            'keyword_version': inasafe_keyword_version
        }

        # Vector Layer keywords
        vector_path = test_data_path('exposure', 'buildings_osm_4326.shp')
        self.vector_layer, _ = load_layer(vector_path)
        self.expected_vector_keywords = {
            'keyword_version': inasafe_keyword_version,
            'structure_class_field': 'FLOODED',
            'title': 'buildings_osm_4326',
            'layer_geometry': 'polygon',
            'layer_purpose': 'exposure',
            'layer_mode': 'classified',
            'exposure': 'structure'
        }
        # Keyword less layer
        keywordless_path = test_data_path('other', 'keywordless_layer.shp')
        self.keywordless_layer, _ = load_layer(keywordless_path)
예제 #20
0
 def test_zonal(self):
     """Test that zonal stats returns the expected output."""
     raster_layer, _ = load_layer(
         test_data_path('other', 'tenbytenraster.asc'))
     vector_layer, _ = load_layer(
         test_data_path('other', 'zonal_polygons.shp'))
     result = calculate_zonal_stats(
         raster_layer=raster_layer,
         polygon_layer=vector_layer)
     expected_result = {
         0L: {'count': 4, 'sum': 34.0, 'mean': 8.5},  # BR polygon
         1L: {'count': 9, 'sum': 36.0, 'mean': 4.0},  # center polygon
         2L: {'count': 4, 'sum': 2.0, 'mean': 0.5},  # TL polygon
         3L: {'count': 4, 'sum': 2.0, 'mean': 0.5},  # BL Polygon
         4L: {'count': 4, 'sum': 34.0, 'mean': 8.5}}  # TR polygon
     # noinspection PyPep8Naming
     self.maxDiff = None
     self.assertDictEqual(expected_result, result)
예제 #21
0
 def test_handle_missing_map_title(self):
     """Missing map title from the keywords fails gracefully"""
     # Use hazard layer as it won't have 'map_title' keyword
     layer_path = test_data_path('hazard', 'tsunami_wgs84.tif')
     layer, _ = load_layer(layer_path)
     template = resources_path('qgis-composer-templates',
                               'inasafe-portrait-a4.qpt')
     report = ImpactReport(IFACE, template, layer)
     title = report.map_title
     expected_title = None
     message = 'Expected: %s\nGot:\n %s' % (expected_title, title)
     self.assertEqual(title, expected_title, message)
예제 #22
0
 def test_handle_missing_map_title(self):
     """Missing map title from the keywords fails gracefully"""
     # Use hazard layer as it won't have 'map_title' keyword
     layer_path = test_data_path('hazard', 'tsunami_wgs84.tif')
     layer, _ = load_layer(layer_path)
     template = resources_path(
         'qgis-composer-templates', 'a4-portrait-blue.qpt')
     report = ImpactReport(IFACE, template, layer)
     title = report.map_title
     expected_title = None
     message = 'Expected: %s\nGot:\n %s' % (expected_title, title)
     self.assertEqual(title, expected_title, message)
예제 #23
0
파일: test_gis.py 프로젝트: easmetz/inasafe
    def test_buffer_points(self):
        """Test if we can make buffers correctly, whatever the projection."""
        # Original data in 3857.
        data_path = standard_data_path('other', 'buffer_points_3857.shp')
        layer, _ = load_layer(data_path)

        output_crs = qgis.core.QgsCoordinateReferenceSystem('EPSG:4326')

        # Wrong radii order.
        radii = [1, 5, 3]
        self.assertRaises(
            RadiiException, buffer_points, layer, radii, 'test', output_crs)

        # Wrong projection
        radii = [1, 2, 3]
        output_crs = qgis.core.QgsCoordinateReferenceSystem('EPSG:3857')
        result = buffer_points(layer, radii, 'test', output_crs)
        data_path = standard_data_path(
            'other', 'buffer_points_expected_4326.shp')
        control_layer, _ = load_layer(data_path)
        is_equal, msg = compare_two_vector_layers(control_layer, result)
        self.assertFalse(is_equal, msg)

        # Expected result in 4326.
        output_crs = qgis.core.QgsCoordinateReferenceSystem('EPSG:4326')
        result = buffer_points(layer, radii, 'test', output_crs)
        data_path = standard_data_path(
            'other', 'buffer_points_expected_4326.shp')
        control_layer, _ = load_layer(data_path)
        is_equal, msg = compare_two_vector_layers(control_layer, result)
        self.assertTrue(is_equal, msg)

        # Expected result in 3857.
        output_crs = qgis.core.QgsCoordinateReferenceSystem('EPSG:3857')
        result = buffer_points(layer, radii, 'test', output_crs)
        data_path = standard_data_path(
            'other', 'buffer_points_expected_3857.shp')
        control_layer, _ = load_layer(data_path)
        is_equal, msg = compare_two_vector_layers(control_layer, result)
        self.assertTrue(is_equal, msg)
예제 #24
0
    def setUp(self):
        self.keyword_io = KeywordIO()

        # SQLite Layer
        uri = QgsDataSourceURI()
        sqlite_building_path = test_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 = {
            'category': 'exposure',
            'datatype': 'OSM',
            'subcategory': 'building'
        }

        # Raster Layer keywords
        hazard_path = test_data_path('hazard', 'padang_tsunami_mw8.tif')
        self.raster_layer, _ = load_layer(hazard_path)
        self.expected_raster_keywords = {
            'category': 'hazard',
            'subcategory': 'tsunami',
            'unit': 'm',
            'title': 'A tsunami in Padang (Mw 8.8)'
        }

        # Vector Layer keywords
        vector_path = test_data_path('exposure', 'buildings_osm_4326.shp')
        self.vector_layer, _ = load_layer(vector_path)
        self.expected_vector_keywords = {
            'category': 'exposure',
            'datatype': 'osm',
            'subcategory': 'structure',
            'title': 'buildings_osm_4326',
            'purpose': 'dki'
        }

        # Keyword less layer
        keywordless_path = test_data_path('other', 'keywordless_layer.shp')
        self.keywordless_layer, _ = load_layer(keywordless_path)
예제 #25
0
    def test_buffer_points(self):
        """Test if we can make buffers correctly, whatever the projection."""
        # Original data in 3857.
        data_path = standard_data_path('other', 'buffer_points_3857.shp')
        layer, _ = load_layer(data_path)

        output_crs = qgis.core.QgsCoordinateReferenceSystem('EPSG:4326')

        # Wrong radii order.
        radii = [1, 5, 3]
        self.assertRaises(RadiiException, buffer_points, layer, radii, 'test',
                          output_crs)

        # Wrong projection
        radii = [1, 2, 3]
        output_crs = qgis.core.QgsCoordinateReferenceSystem('EPSG:3857')
        result = buffer_points(layer, radii, 'test', output_crs)
        data_path = standard_data_path('other',
                                       'buffer_points_expected_4326.shp')
        control_layer, _ = load_layer(data_path)
        is_equal, msg = compare_two_vector_layers(control_layer, result)
        self.assertFalse(is_equal, msg)

        # Expected result in 4326.
        output_crs = qgis.core.QgsCoordinateReferenceSystem('EPSG:4326')
        result = buffer_points(layer, radii, 'test', output_crs)
        data_path = standard_data_path('other',
                                       'buffer_points_expected_4326.shp')
        control_layer, _ = load_layer(data_path)
        is_equal, msg = compare_two_vector_layers(control_layer, result)
        self.assertTrue(is_equal, msg)

        # Expected result in 3857.
        output_crs = qgis.core.QgsCoordinateReferenceSystem('EPSG:3857')
        result = buffer_points(layer, radii, 'test', output_crs)
        data_path = standard_data_path('other',
                                       'buffer_points_expected_3857.shp')
        control_layer, _ = load_layer(data_path)
        is_equal, msg = compare_two_vector_layers(control_layer, result)
        self.assertTrue(is_equal, msg)
예제 #26
0
    def register_layers(self):
        """Register needed layers to QgsMapLayerRegistry."""
        # Register 4 impact layers and aggregation layer
        self.population_entire_jakarta_layer, _ = load_layer(
            population_entire_jakarta_impact_path)
        self.building_entire_jakarta_layer, _ = load_layer(
            building_entire_jakarta_impact_path)
        self.population_district_jakarta_layer, _ = load_layer(
            population_district_jakarta_impact_path)
        self.building_district_jakarta_layer, _ = load_layer(
            building_district_jakarta_impact_path)
        self.district_jakarta_layer, _ = load_layer(
            district_jakarta_boundary_path)

        layer_list = [self.population_entire_jakarta_layer,
                      self.population_district_jakarta_layer,
                      self.building_entire_jakarta_layer,
                      self.building_district_jakarta_layer,
                      self.district_jakarta_layer]

        # noinspection PyArgumentList
        self.map_layer_registry.addMapLayers(layer_list)
예제 #27
0
    def test_get_map_title(self):
        """Getting the map title from the keywords"""
        impact_layer_path = test_data_path(
            'impact', 'population_affected_entire_area.shp')
        layer, _ = load_layer(impact_layer_path)

        template = resources_path('qgis-composer-templates',
                                  'inasafe-portrait-a4.qpt')
        report = ImpactReport(IFACE, template, layer)
        title = report.map_title
        expected_title = 'People affected by flood prone areas'
        message = 'Expected: %s\nGot:\n %s' % (expected_title, title)
        self.assertEqual(title, expected_title, message)
예제 #28
0
    def test_get_map_title(self):
        """Getting the map title from the keywords"""
        impact_layer_path = test_data_path(
            'impact', 'population_affected_entire_area.shp')
        layer, _ = load_layer(impact_layer_path)

        template = resources_path(
            'qgis-composer-templates', 'a4-portrait-blue.qpt')
        report = ImpactReport(IFACE, template, layer)
        title = report.map_title
        expected_title = 'People affected by flood prone areas'
        message = 'Expected: %s\nGot:\n %s' % (expected_title, title)
        self.assertEqual(title, expected_title, message)
예제 #29
0
    def setUp(self):
        self.keyword_io = KeywordIO()

        # SQLite Layer
        uri = QgsDataSourceURI()
        sqlite_building_path = test_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 = {
            'category': 'exposure',
            'datatype': 'OSM',
            'subcategory': 'building'}

        # Raster Layer keywords
        hazard_path = test_data_path('hazard', 'tsunami_wgs84.tif')
        self.raster_layer, _ = load_layer(hazard_path)
        self.expected_raster_keywords = {
            'category': 'hazard',
            'subcategory': 'tsunami',
            'data_type': 'continuous',
            'unit': 'metres_depth',
            'title': 'Tsunami'}

        # Vector Layer keywords
        vector_path = test_data_path('exposure', 'buildings_osm_4326.shp')
        self.vector_layer, _ = load_layer(vector_path)
        self.expected_vector_keywords = {
            'category': 'exposure',
            'datatype': 'osm',
            'subcategory': 'structure',
            'title': 'buildings_osm_4326',
            'purpose': 'dki'}

        # Keyword less layer
        keywordless_path = test_data_path('other', 'keywordless_layer.shp')
        self.keywordless_layer, _ = load_layer(keywordless_path)
예제 #30
0
 def test_zonal(self):
     """Test that zonal stats returns the expected output."""
     raster_layer, _ = load_layer(
         test_data_path('other', 'tenbytenraster.asc'))
     vector_layer, _ = load_layer(
         test_data_path('other', 'zonal_polygons.shp'))
     result = calculate_zonal_stats(raster_layer=raster_layer,
                                    polygon_layer=vector_layer)
     expected_result = {
         0L: {
             'count': 4,
             'sum': 34.0,
             'mean': 8.5
         },  # BR polygon
         1L: {
             'count': 9,
             'sum': 36.0,
             'mean': 4.0
         },  # center polygon
         2L: {
             'count': 4,
             'sum': 2.0,
             'mean': 0.5
         },  # TL polygon
         3L: {
             'count': 4,
             'sum': 2.0,
             'mean': 0.5
         },  # BL Polygon
         4L: {
             'count': 4,
             'sum': 34.0,
             'mean': 8.5
         }
     }  # TR polygon
     # noinspection PyPep8Naming
     self.maxDiff = None
     self.assertDictEqual(expected_result, result)
예제 #31
0
    def test_custom_logo(self):
        """Test that setting user-defined logo works."""
        LOGGER.info('Testing custom_logo')
        impact_layer_path = test_data_path(
            'impact', 'population_affected_entire_area.shp')
        layer, _ = load_layer(impact_layer_path)
        # noinspection PyUnresolvedReferences
        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',
                                  'inasafe-portrait-a4.qpt')
        report = ImpactReport(IFACE, template, layer)

        # Set custom logo
        custom_logo_path = resources_path('img', 'logos', 'logo-flower.png')
        report.organisation_logo = custom_logo_path

        out_path = unique_filename(prefix='map_custom_logo_test',
                                   suffix='.pdf',
                                   dir=temp_dir('test'))
        report.print_map_to_pdf(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)

        # Check the organisation logo in composition sets correctly to
        # logo-flower
        if qgis_version() < 20500:
            custom_img_path = report.composition.getComposerItemById(
                'organisation-logo').pictureFile()
        else:
            custom_img_path = report.composition.getComposerItemById(
                'organisation-logo').picturePath()

        message = 'The custom logo path is not set correctly'
        self.assertEqual(custom_logo_path, custom_img_path, message)
예제 #32
0
    def test_custom_logo(self):
        """Test that setting user-defined logo works."""
        LOGGER.info('Testing custom_logo')
        impact_layer_path = test_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)

        # Set custom logo
        custom_logo_path = resources_path('img', 'logos', 'supporters.png')
        report.organisation_logo = custom_logo_path

        out_path = unique_filename(
            prefix='map_custom_logo_test', suffix='.pdf', dir=temp_dir('test'))
        report.print_map_to_pdf(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)

        # Check the organisation logo in composition sets correctly to
        # logo-flower
        if qgis_version() < 20500:
            custom_img_path = report.composition.getComposerItemById(
                'organisation-logo').pictureFile()
        else:
            custom_img_path = report.composition.getComposerItemById(
                'organisation-logo').picturePath()

        message = 'The custom logo path is not set correctly'
        self.assertEqual(custom_logo_path, custom_img_path, message)
예제 #33
0
    def test_layer_changed(self):
        """Test the metadata is updated as the user highlights different
        QGIS layers. For inasafe outputs, the table of results should be shown
        See also
        https://github.com/AIFDR/inasafe/issues/58
        """
        layer_path = os.path.join(TESTDATA, 'issue58.tif')
        layer, layer_type = load_layer(layer_path)
        message = ('Unexpected category for issue58.tif.\nGot:'
                   ' %s\nExpected: undefined' % layer_type)

        self.assertTrue(layer_type == 'undefined', message)
        DOCK.layer_changed(layer)
        DOCK.save_state()
        html = DOCK.state['report']
        expected = '4229'
        message = "%s\nDoes not contain:\n%s" % (html, expected)
        self.assertTrue(expected in html, message)
예제 #34
0
    def test_layer_saved_as_with_keywords_and_xml(self):
        """Check that auxiliary files are well copied when they exist and the
        'saved as' is used.
        """

        layer_path = os.path.join(TESTDATA, 'tsunami_building_assessment.shp')
        # pylint: disable=unused-variable
        layer, layer_type = load_layer(layer_path)
        # pylint: enable=unused-variable

        new_name = unique_filename(
            prefix='tsunami_building_assessment_saved_as_')
        self.dock.save_auxiliary_files(layer,
                                       join(TESTDATA, '%s.shp' % new_name))

        new_xml_filepath = os.path.join(TESTDATA, '%s.xml' % new_name)

        message = 'New auxiliary file does not exist : '
        self.assertTrue(os.path.isfile(new_xml_filepath), '%s xml' % message)
예제 #35
0
    def test_layer_saved_as_without_keywords_and_xml(self):
        """Check that auxiliary files aren't created when they don't exist.

        ... and the 'saved as' is used.
        """

        layer_path = os.path.join(TESTDATA, 'kecamatan_jakarta_osm.shp')
        # pylint: disable=unused-variable
        layer, layer_type = load_layer(layer_path)
        # pylint: enable=unused-variable

        new_name = unique_filename(prefix='kecamatan_jakarta_osm_saved_as')
        self.dock.save_auxiliary_files(layer,
                                       join(TESTDATA, '%s.shp' % new_name))
        new_xml_file_path = os.path.join(TESTDATA, '%s.xml' % new_name)

        message = 'New auxiliary file exist : '
        # Will automatically add xml file for the metadata.
        self.assertTrue(os.path.isfile(new_xml_file_path), '%s xml' % message)
예제 #36
0
    def test_missing_elements(self):
        """Test missing elements set correctly."""
        impact_layer_path = test_data_path(
            'impact', 'population_affected_entire_area.shp')
        layer, _ = load_layer(impact_layer_path)

        template = resources_path(
            'qgis-composer-templates', 'a4-portrait-blue.qpt')
        report = ImpactReport(IFACE, template, layer)
        # There are missing elements in the template
        component_ids = ['safe-logo', 'north-arrow', 'organisation-logo',
                         'impact-map', 'impact-legend',
                         'i-added-element-id-here-nooo']
        report.component_ids = component_ids
        expected_missing_elements = ['i-added-element-id-here-nooo']
        message = 'The missing_elements should be %s, but it returns %s' % (
            report.missing_elements, expected_missing_elements)
        self.assertEqual(
            expected_missing_elements, report.missing_elements, message)
예제 #37
0
    def test_layer_saved_as_with_keywords_and_xml(self):
        """Check that auxiliary files are well copied when they exist and the
        'saved as' is used.
        """

        layer_path = os.path.join(TESTDATA, 'tsunami_building_assessment.shp')
        # pylint: disable=unused-variable
        layer, layer_type = load_layer(layer_path)
        # pylint: enable=unused-variable

        new_name = unique_filename(
            prefix='tsunami_building_assessment_saved_as_')
        self.dock.save_auxiliary_files(
            layer, join(TESTDATA, '%s.shp' % new_name))

        new_xml_filepath = os.path.join(TESTDATA, '%s.xml' % new_name)

        message = 'New auxiliary file does not exist : '
        self.assertTrue(os.path.isfile(new_xml_filepath), '%s xml' % message)
예제 #38
0
    def test_layer_saved_as_without_keywords_and_xml(self):
        """Check that auxiliary files aren't created when they don't exist.

        ... and the 'saved as' is used.
        """

        layer_path = os.path.join(TESTDATA, 'kecamatan_jakarta_osm.shp')
        # pylint: disable=unused-variable
        layer, layer_type = load_layer(layer_path)
        # pylint: enable=unused-variable

        new_name = unique_filename(prefix='kecamatan_jakarta_osm_saved_as')
        self.dock.save_auxiliary_files(
            layer, join(TESTDATA, '%s.shp' % new_name))
        new_xml_file_path = os.path.join(TESTDATA, '%s.xml' % new_name)

        message = 'New auxiliary file exist : '
        # Will automatically add xml file for the metadata.
        self.assertTrue(os.path.isfile(new_xml_file_path), '%s xml' % message)
예제 #39
0
    def test_missing_elements(self):
        """Test missing elements set correctly."""
        impact_layer_path = standard_data_path(
            'impact', 'population_affected_entire_area.shp')
        layer, _ = load_layer(impact_layer_path)

        template = resources_path(
            'qgis-composer-templates', 'a4-portrait-blue.qpt')
        report = ImpactReport(IFACE, template, layer)
        # There are missing elements in the template
        component_ids = ['safe-logo', 'north-arrow', 'organisation-logo',
                         'impact-map', 'impact-legend',
                         'i-added-element-id-here-nooo']
        report.component_ids = component_ids
        expected_missing_elements = ['i-added-element-id-here-nooo']
        message = 'The missing_elements should be %s, but it returns %s' % (
            report.missing_elements, expected_missing_elements)
        self.assertEqual(
            expected_missing_elements, report.missing_elements, message)
예제 #40
0
    def test_layer_saved_as_without_keywords_and_xml(self):
        """Check that auxiliary files aren't created when they don't exist and
        the 'saved as' is used.
        """

        layer_path = os.path.join(TESTDATA, 'kecamatan_jakarta_osm.shp')
        # pylint: disable=unused-variable
        layer, layer_type = load_layer(layer_path)
        # pylint: enable=unused-variable

        new_name = unique_filename(prefix='kecamatan_jakarta_osm_saved_as')
        DOCK.save_auxiliary_files(layer, join(TESTDATA, '%s.shp' % new_name))
        new_keywords_filepath = os.path.join(TESTDATA,
                                             '%s.keywords' % new_name)
        new_xml_filepath = os.path.join(TESTDATA, '%s.xml' % new_name)

        message = 'New auxiliary file exist : '
        self.assertFalse(os.path.isfile(new_keywords_filepath),
                         '%s keywords' % message)
        self.assertFalse(os.path.isfile(new_xml_filepath), '%s xml' % message)
예제 #41
0
    def test_layer_saved_as_without_keywords_and_xml(self):
        """Check that auxiliary files aren't created when they don't exist and
        the 'saved as' is used.
        """

        layer_path = os.path.join(TESTDATA, 'kecamatan_jakarta_osm.shp')
        # pylint: disable=unused-variable
        layer, layer_type = load_layer(layer_path)
        # pylint: enable=unused-variable

        new_name = unique_filename(prefix='kecamatan_jakarta_osm_saved_as')
        DOCK.save_auxiliary_files(
            layer, join(TESTDATA, '%s.shp' % new_name))
        new_keywords_filepath = os.path.join(
            TESTDATA, '%s.keywords' % new_name)
        new_xml_filepath = os.path.join(TESTDATA, '%s.xml' % new_name)

        message = 'New auxiliary file exist : '
        self.assertFalse(
            os.path.isfile(new_keywords_filepath), '%s keywords' % message)
        self.assertFalse(os.path.isfile(new_xml_filepath), '%s xml' % message)
예제 #42
0
    def test_layer_changed(self):
        """Test the metadata is updated as the user highlights layers.

        For inasafe outputs, the table of results should be shown
        See also https://github.com/AIFDR/inasafe/issues/58
        """
        layer_path = os.path.join(TESTDATA, 'issue58.tif')
        layer, layer_type = load_layer(layer_path)
        message = (
            'Unexpected category for issue58.tif.\nGot:'
            ' %s\nExpected: undefined' % layer_type)

        self.assertTrue(layer_type == 'undefined', message)
        self.dock.layer_changed(layer)
        self.dock.save_state()
        html = self.dock.state['report']
        expected = '4229'
        message = "%s\nDoes not contain:\n%s" % (
            html,
            expected)
        self.assertTrue(expected in html, message)
예제 #43
0
    def test_issue126(self):
        """Test that non integer transparency ranges fail gracefully.
        .. seealso:: https://github.com/AIFDR/inasafe/issues/126
        """
        # This dataset has all cells with value 1.3
        data_path = test_data_path('other', 'issue126.tif')
        layer, _ = load_layer(data_path)

        # Note the float quantity values below
        style_info = {
            'style_classes': [
                dict(colour='#38A800', quantity=1.1, transparency=100),
                dict(colour='#38A800', quantity=1.4, transparency=0),
                dict(colour='#79C900', quantity=10.1, transparency=0)]}

        try:
            setRasterStyle(layer, style_info)
        except Exception, e:
            message = (
                'Setting style info with float based ranges should fail '
                'gracefully.')
            e.args = (e.args[0] + message,)
            raise
예제 #44
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
예제 #45
0
    def test_issue230(self):
        """Verify that we give informative errors when style is not correct
           .. seealso:: https://github.com/AIFDR/inasafe/issues/230
        """
        path = test_data_path('impact', 'polygons_for_styling.shp')
        vector_layer, _ = load_layer(path)

        style = {
            'legend_title':
            u'Population Count',
            'target_field':
            'population',
            'style_classes': [
                {
                    'transparency': 0,
                    'min': [0],  # <--intentionally broken list not number!
                    'max': 139904.08186340332,
                    'colour': '#FFFFFF',
                    'size': 1,
                    'label': u'Nil'
                },
                {
                    'transparency': 0,
                    'min': 139904.08186340332,
                    'max': 279808.16372680664,
                    'colour': '#38A800',
                    'size': 1,
                    'label': u'Low'
                },
                {
                    'transparency': 0,
                    'min': 279808.16372680664,
                    'max': 419712.24559020996,
                    'colour': '#79C900',
                    'size': 1,
                    'label': u'Low'
                },
                {
                    'transparency': 0,
                    'min': 419712.24559020996,
                    'max': 559616.32745361328,
                    'colour': '#CEED00',
                    'size': 1,
                    'label': u'Low'
                },
                {
                    'transparency': 0,
                    'min': 559616.32745361328,
                    'max': 699520.4093170166,
                    'colour': '#FFCC00',
                    'size': 1,
                    'label': u'Medium'
                },
                {
                    'transparency': 0,
                    'min': 699520.4093170166,
                    'max': 839424.49118041992,
                    'colour': '#FF6600',
                    'size': 1,
                    'label': u'Medium'
                },
                {
                    'transparency': 0,
                    'min': 839424.49118041992,
                    'max': 979328.57304382324,
                    'colour': '#FF0000',
                    'size': 1,
                    'label': u'Medium'
                },
                {
                    'transparency': 0,
                    'min': 979328.57304382324,
                    'max': 1119232.6549072266,
                    'colour': '#7A0000',
                    'size': 1,
                    'label': u'High'
                }
            ]
        }
        try:
            set_vector_graduated_style(vector_layer, style)
        except StyleError:
            # Exactly what should have happened
            return
        except Exception, e:  # pylint: disable=broad-except
            print str(e)
예제 #46
0
    def test_issue230(self):
        """Verify that we give informative errors when style is not correct
           .. seealso:: https://github.com/AIFDR/inasafe/issues/230
        """
        path = test_data_path('impact', 'polygons_for_styling.shp')
        vector_layer, _ = load_layer(path)

        style = {
            'legend_title': u'Population Count',
            'target_field': 'population',
            'style_classes':
                [
                    {
                        'transparency': 0,
                        'min': [0],  # <--intentionally broken list not number!
                        'max': 139904.08186340332,
                        'colour': '#FFFFFF',
                        'size': 1,
                        'label': u'Nil'},
                    {
                        'transparency': 0,
                        'min': 139904.08186340332,
                        'max': 279808.16372680664,
                        'colour': '#38A800',
                        'size': 1,
                        'label': u'Low'},
                    {
                        'transparency': 0,
                        'min': 279808.16372680664,
                        'max': 419712.24559020996,
                        'colour': '#79C900',
                        'size': 1,
                        'label': u'Low'},
                    {
                        'transparency': 0,
                        'min': 419712.24559020996,
                        'max': 559616.32745361328,
                        'colour': '#CEED00',
                        'size': 1,
                        'label': u'Low'},
                    {
                        'transparency': 0,
                        'min': 559616.32745361328,
                        'max': 699520.4093170166,
                        'colour': '#FFCC00',
                        'size': 1,
                        'label': u'Medium'},
                    {
                        'transparency': 0,
                        'min': 699520.4093170166,
                        'max': 839424.49118041992,
                        'colour': '#FF6600',
                        'size': 1,
                        'label': u'Medium'},
                    {
                        'transparency': 0,
                        'min': 839424.49118041992,
                        'max': 979328.57304382324,
                        'colour': '#FF0000',
                        'size': 1,
                        'label': u'Medium'},
                    {
                        'transparency': 0,
                        'min': 979328.57304382324,
                        'max': 1119232.6549072266,
                        'colour': '#7A0000',
                        'size': 1,
                        'label': u'High'}]}
        try:
            set_vector_graduated_style(vector_layer, style)
        except StyleError:
            # Exactly what should have happened
            return
        except Exception, e:  # pylint: disable=broad-except
            print str(e)
예제 #47
0
    def test_regression_2553_no_resample(self):
        """Test for regression 2553 (no resampling).

        see :

        https://github.com/inasafe/inasafe/issues/2553

        We want to verify that population with resampling should produce
        a result within a reasonable range of the same analysis but doing
        population with no resampling.

        """
        hazard_path = test_data_path(
            'hazard', 'continuous_flood_unaligned_big_size.tif')
        exposure_path = test_data_path(
            'exposure', 'people_allow_resampling_false.tif')

        hazard_layer, hazard_layer_purpose = load_layer(hazard_path)
        # Check if there is a regression about keywords being updated from
        # another layer - see #2605
        keywords = KeywordIO(hazard_layer)
        self.assertIn('flood unaligned', keywords.to_message().to_text())

        exposure_layer, exposure_layer_purpose = load_layer(
            exposure_path)
        keywords = KeywordIO(exposure_layer)
        self.assertIn(
            '*Allow resampling*, false------',
            keywords.to_message().to_text())

        QgsMapLayerRegistry.instance().addMapLayers(
            [hazard_layer, exposure_layer])

        # Count the total value of all exposure pixels
        # this is arse about face but width is actually giving height
        height = exposure_layer.width()
        # this is arse about face but height is actually giving width
        width = exposure_layer.height()
        provider = exposure_layer.dataProvider()
        # Bands count from 1!
        block = provider.block(1, provider.extent(), height, width)
        # Enable on-the-fly reprojection
        set_canvas_crs(GEOCRS, True)

        # This is the nicer way but wierdly it gets nan for every cell
        total_population = 0.0
        cell_count = 0
        row = 0
        # Iterate down each column to match the layout produced by r.stats
        while row < width:
            column = 0
            while column < height:
                cell_count += 1
                value = block.value(row, column)
                if value > 0:
                    total_population += value
                column += 1
            row += 1
        print "Total value of all cells is: %d" % total_population
        print "Number of cells counted: %d" % cell_count

        # 131 computed using r.sum
        self.assertAlmostEqual(total_population, 131.0177006121)

        result, message = setup_scenario(
            self.dock,
            hazard='flood unaligned',
            exposure='People never resample',
            function='Need evacuation',
            function_id='FloodEvacuationRasterHazardFunction')
        self.assertTrue(result, message)
        # Press RUN
        self.dock.accept()

        safe_layer = self.dock.analysis.impact_layer
        keywords = safe_layer.get_keywords()
        evacuated = float(keywords['evacuated'])
        self.assertLess(evacuated, total_population)
        expected_evacuated = 131.0
        self.assertEqual(evacuated, expected_evacuated)
    def test_regression_2553_no_resample(self):
        """Test for regression 2553 (no resampling).

        see :

        https://github.com/inasafe/inasafe/issues/2553

        We want to verify that population with resampling should produce
        a result within a reasonable range of the same analysis but doing
        population with no resampling.

        """
        hazard_path = test_data_path(
            'hazard', 'continuous_flood_unaligned_big_size.tif')
        exposure_path = test_data_path(
            'exposure', 'people_allow_resampling_false.tif')

        hazard_layer, hazard_layer_purpose = load_layer(hazard_path)
        # Check if there is a regression about keywords being updated from
        # another layer - see #2605
        keywords = KeywordIO(hazard_layer)
        self.assertIn('flood unaligned', keywords.to_message().to_text())

        exposure_layer, exposure_layer_purpose = load_layer(
            exposure_path)
        keywords = KeywordIO(exposure_layer)
        self.assertIn(
            '*Allow resampling*, false------',
            keywords.to_message().to_text())

        QgsMapLayerRegistry.instance().addMapLayers(
            [hazard_layer, exposure_layer])

        # Count the total value of all exposure pixels
        # this is arse about face but width is actually giving height
        height = exposure_layer.width()
        # this is arse about face but height is actually giving width
        width = exposure_layer.height()
        provider = exposure_layer.dataProvider()
        # Bands count from 1!
        block = provider.block(1, provider.extent(), height, width)
        # Enable on-the-fly reprojection
        set_canvas_crs(GEOCRS, True)

        # This is the nicer way but wierdly it gets nan for every cell
        total_population = 0.0
        cell_count = 0
        row = 0
        # Iterate down each column to match the layout produced by r.stats
        while row < width:
            column = 0
            while column < height:
                cell_count += 1
                value = block.value(row, column)
                if value > 0:
                    total_population += value
                column += 1
            row += 1
        print "Total value of all cells is: %d" % total_population
        print "Number of cells counted: %d" % cell_count

        # 131 computed using r.sum
        self.assertAlmostEqual(total_population, 131.0177006121)

        result, message = setup_scenario(
            self.dock,
            hazard='flood unaligned',
            exposure='People never resample',
            function='Need evacuation',
            function_id='FloodEvacuationRasterHazardFunction')
        self.assertTrue(result, message)
        # Press RUN
        self.dock.accept()

        safe_layer = self.dock.impact_function.impact
        keywords = safe_layer.get_keywords()
        evacuated = float(keywords['evacuated'])
        self.assertLess(evacuated, total_population)
        expected_evacuated = 131.0
        self.assertEqual(evacuated, expected_evacuated)