コード例 #1
0
ファイル: test_shake_grid.py プロジェクト: inasafe/inasafe
 def test_convert_grid_to_raster(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, output_path=output_raster,
         algorithm=NEAREST_NEIGHBOUR)
     expected_result = output_raster.replace(
         '.tif', '-%s.tif' % NEAREST_NEIGHBOUR)
     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')
コード例 #2
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'])
コード例 #3
0
ファイル: test_shake_grid.py プロジェクト: inasafe/inasafe
 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'])
コード例 #4
0
ファイル: test_shake_grid.py プロジェクト: inasafe/inasafe
 def test_convert_grid_to_raster(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, output_path=output_raster,
         algorithm=NEAREST_NEIGHBOUR)
     expected_result = output_raster.replace(
         '.tif', '-%s.tif' % NEAREST_NEIGHBOUR)
     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')
コード例 #5
0
    def accept(self):
        """Handler for when OK is clicked.
        """
        input_path = self.input_path.text()
        input_title = self.line_edit_title.text()
        input_source = self.line_edit_source.text()
        output_path = self.output_path.text()
        if not output_path.endswith('.tif'):
            # noinspection PyArgumentList,PyCallByClass,PyTypeChecker
            QMessageBox.warning(
                self,
                self.tr('InaSAFE'),
                (self.tr('Output file name must be tif file')))
        if not os.path.exists(input_path):
            # noinspection PyArgumentList,PyCallByClass,PyTypeChecker
            QMessageBox.warning(
                self,
                self.tr('InaSAFE'),
                (self.tr('Input file does not exist')))
            return

        if self.nearest_mode.isChecked():
            algorithm = 'nearest'
        else:
            algorithm = 'invdist'

        QtGui.qApp.setOverrideCursor(QtGui.QCursor(QtCore.Qt.WaitCursor))

        file_name = convert_mmi_data(
            input_path,
            input_title,
            input_source,
            output_path,
            algorithm=algorithm,
            algorithm_filename_flag=True)

        QtGui.qApp.restoreOverrideCursor()

        if self.load_result.isChecked():
            file_info = QFileInfo(file_name)
            base_name = file_info.baseName()
            layer = QgsRasterLayer(file_name, base_name)
            # noinspection PyTypeChecker
            mmi_ramp(layer)
            layer.saveDefaultStyle()
            if not layer.isValid():
                LOGGER.debug("Failed to load")
            else:
                # noinspection PyArgumentList
                QgsMapLayerRegistry.instance().addMapLayers([layer])
                iface.zoomToActiveLayer()
        self.done(self.Accepted)
コード例 #6
0
    def accept(self):
        """Handler for when OK is clicked.
        """
        input_path = self.input_path.text()
        input_title = self.line_edit_title.text()
        input_source = self.line_edit_source.text()
        output_path = self.output_path.text()
        if not output_path.endswith('.tif'):
            # noinspection PyArgumentList,PyCallByClass,PyTypeChecker
            QMessageBox.warning(
                self,
                self.tr('InaSAFE'),
                (self.tr('Output file name must be tif file')))
        if not os.path.exists(input_path):
            # noinspection PyArgumentList,PyCallByClass,PyTypeChecker
            QMessageBox.warning(
                self,
                self.tr('InaSAFE'),
                (self.tr('Input file does not exist')))
            return

        if self.nearest_mode.isChecked():
            algorithm = 'nearest'
        else:
            algorithm = 'invdist'

        QtGui.qApp.setOverrideCursor(QtGui.QCursor(QtCore.Qt.WaitCursor))

        file_name = convert_mmi_data(
            input_path,
            input_title,
            input_source,
            output_path,
            algorithm=algorithm,
            algorithm_filename_flag=True)

        QtGui.qApp.restoreOverrideCursor()

        if self.load_result.isChecked():
            file_info = QFileInfo(file_name)
            base_name = file_info.baseName()
            layer = QgsRasterLayer(file_name, base_name)
            # noinspection PyTypeChecker
            mmi_ramp(layer)
            layer.saveDefaultStyle()
            if not layer.isValid():
                LOGGER.debug("Failed to load")
            else:
                # noinspection PyArgumentList
                QgsMapLayerRegistry.instance().addMapLayers([layer])
                iface.zoomToActiveLayer()
        self.done(self.Accepted)
コード例 #7
0
    def accept(self):
        """Handler for when OK is clicked."""
        input_path = self.input_path.text()
        input_title = self.line_edit_title.text()
        input_source = self.line_edit_source.text()
        output_path = self.output_path.text()
        if not output_path.endswith('.tif'):
            # noinspection PyArgumentList,PyCallByClass,PyTypeChecker
            QMessageBox.warning(
                self,
                self.tr('InaSAFE'),
                (self.tr('Output file name must be tif file')))
        if not os.path.exists(input_path):
            # noinspection PyArgumentList,PyCallByClass,PyTypeChecker
            QMessageBox.warning(
                self,
                self.tr('InaSAFE'),
                (self.tr('Input file does not exist')))
            return

        if self.nearest_mode.isChecked():
            algorithm = 'nearest'
        else:
            algorithm = 'invdist'

        QtGui.qApp.setOverrideCursor(QtGui.QCursor(QtCore.Qt.WaitCursor))

        file_name = convert_mmi_data(
            input_path,
            input_title,
            input_source,
            output_path,
            algorithm=algorithm,
            algorithm_filename_flag=True)

        # reclassify raster
        file_info = QFileInfo(file_name)
        base_name = file_info.baseName()
        self.output_layer = QgsRasterLayer(file_name, base_name)
        self.output_layer.keywords = KeywordIO.read_keywords(self.output_layer)
        self.output_layer.keywords['classification'] = (
            earthquake_mmi_scale['key'])
        keywords = self.output_layer.keywords
        if self.output_layer.isValid():
            self.output_layer = reclassify(
                self.output_layer, overwrite_input=True)
            KeywordIO.write_keywords(self.output_layer, keywords)
        else:
            LOGGER.debug("Failed to load")

        QtGui.qApp.restoreOverrideCursor()

        if self.load_result.isChecked():
            # noinspection PyTypeChecker
            mmi_ramp_roman(self.output_layer)
            self.output_layer.saveDefaultStyle()
            if not self.output_layer.isValid():
                LOGGER.debug("Failed to load")
            else:
                # noinspection PyArgumentList
                QgsMapLayerRegistry.instance().addMapLayer(self.output_layer)
                iface.zoomToActiveLayer()

        if (self.keyword_wizard_checkbox.isChecked() and
                self.keyword_wizard_checkbox.isEnabled()):
            self.launch_keyword_wizard()

        self.done(self.Accepted)
コード例 #8
0
    def accept(self):
        """Handler for when OK is clicked."""
        input_path = self.input_path.text()
        input_title = self.line_edit_title.text()
        input_source = self.line_edit_source.text()
        output_path = self.output_path.text()
        if not output_path.endswith('.tif'):
            # noinspection PyArgumentList,PyCallByClass,PyTypeChecker
            QMessageBox.warning(self, tr('InaSAFE'),
                                tr('Output file name must be tif file'))
        if not os.path.exists(input_path):
            # noinspection PyArgumentList,PyCallByClass,PyTypeChecker
            QMessageBox.warning(self, tr('InaSAFE'),
                                tr('Input file does not exist'))
            return

        algorithm = 'nearest'
        if self.nearest_mode.isChecked():
            algorithm = 'nearest'
        elif self.inverse_distance_mode.isChecked():
            algorithm = 'invdist'
        elif self.use_ascii_mode.isChecked():
            algorithm = 'use_ascii'

        # Smoothing
        smoothing_method = NONE_SMOOTHING
        if self.numpy_smoothing.isChecked():
            smoothing_method = NUMPY_SMOOTHING
        if self.scipy_smoothing.isChecked():
            smoothing_method = SCIPY_SMOOTHING

        # noinspection PyUnresolvedReferences
        QgsApplication.instance().setOverrideCursor(
            QtGui.QCursor(QtCore.Qt.WaitCursor))

        extra_keywords = {}
        if self.check_box_custom_shakemap_id.isChecked():
            event_id = self.line_edit_shakemap_id.text()
            extra_keywords[extra_keyword_earthquake_event_id['key']] = event_id

        current_index = self.combo_box_source_type.currentIndex()
        source_type = self.combo_box_source_type.itemData(current_index)
        if source_type:
            extra_keywords[
                extra_keyword_earthquake_source['key']] = source_type

        file_name = convert_mmi_data(input_path,
                                     input_title,
                                     input_source,
                                     output_path,
                                     algorithm=algorithm,
                                     algorithm_filename_flag=True,
                                     smoothing_method=smoothing_method,
                                     extra_keywords=extra_keywords)

        file_info = QFileInfo(file_name)
        base_name = file_info.baseName()
        self.output_layer = QgsRasterLayer(file_name, base_name)

        # noinspection PyUnresolvedReferences
        QgsApplication.instance().restoreOverrideCursor()

        if self.load_result.isChecked():
            # noinspection PyTypeChecker
            mmi_ramp_roman(self.output_layer)
            self.output_layer.saveDefaultStyle()
            if not self.output_layer.isValid():
                LOGGER.debug("Failed to load")
            else:
                # noinspection PyArgumentList
                QgsProject.instance().addMapLayer(self.output_layer)
                iface.zoomToActiveLayer()

        if (self.keyword_wizard_checkbox.isChecked()
                and self.keyword_wizard_checkbox.isEnabled()):
            self.launch_keyword_wizard()

        self.done(self.Accepted)
コード例 #9
0
    def accept(self):
        """Handler for when OK is clicked."""
        input_path = self.input_path.text()
        input_title = self.line_edit_title.text()
        input_source = self.line_edit_source.text()
        output_path = self.output_path.text()
        if not output_path.endswith('.tif'):
            # noinspection PyArgumentList,PyCallByClass,PyTypeChecker
            QMessageBox.warning(
                self,
                self.tr('InaSAFE'),
                (self.tr('Output file name must be tif file')))
        if not os.path.exists(input_path):
            # noinspection PyArgumentList,PyCallByClass,PyTypeChecker
            QMessageBox.warning(
                self,
                self.tr('InaSAFE'),
                (self.tr('Input file does not exist')))
            return

        if self.nearest_mode.isChecked():
            algorithm = 'nearest'
        else:
            algorithm = 'invdist'

        QtGui.qApp.setOverrideCursor(QtGui.QCursor(QtCore.Qt.WaitCursor))

        file_name = convert_mmi_data(
            input_path,
            input_title,
            input_source,
            output_path,
            algorithm=algorithm,
            algorithm_filename_flag=True)

        # reclassify raster
        file_info = QFileInfo(file_name)
        base_name = file_info.baseName()
        self.output_layer = QgsRasterLayer(file_name, base_name)
        self.output_layer.keywords = KeywordIO.read_keywords(self.output_layer)
        self.output_layer.keywords['classification'] = (
            earthquake_mmi_scale['key'])
        keywords = self.output_layer.keywords
        if self.output_layer.isValid():
            self.output_layer = reclassify(
                self.output_layer, overwrite_input=True)
            KeywordIO.write_keywords(self.output_layer, keywords)
        else:
            LOGGER.debug("Failed to load")

        QtGui.qApp.restoreOverrideCursor()

        if self.load_result.isChecked():
            # noinspection PyTypeChecker
            mmi_ramp_roman(self.output_layer)
            self.output_layer.saveDefaultStyle()
            if not self.output_layer.isValid():
                LOGGER.debug("Failed to load")
            else:
                # noinspection PyArgumentList
                QgsMapLayerRegistry.instance().addMapLayer(self.output_layer)
                iface.zoomToActiveLayer()

        if (self.keyword_wizard_checkbox.isChecked() and
                self.keyword_wizard_checkbox.isEnabled()):
            self.launch_keyword_wizard()

        self.done(self.Accepted)
コード例 #10
0
    def accept(self):
        """Handler for when OK is clicked."""
        input_path = self.input_path.text()
        input_title = self.line_edit_title.text()
        input_source = self.line_edit_source.text()
        output_path = self.output_path.text()
        if not output_path.endswith('.tif'):
            # noinspection PyArgumentList,PyCallByClass,PyTypeChecker
            QMessageBox.warning(
                self,
                tr('InaSAFE'),
                tr('Output file name must be tif file'))
        if not os.path.exists(input_path):
            # noinspection PyArgumentList,PyCallByClass,PyTypeChecker
            QMessageBox.warning(
                self,
                tr('InaSAFE'),
                tr('Input file does not exist'))
            return

        algorithm = 'nearest'
        if self.nearest_mode.isChecked():
            algorithm = 'nearest'
        elif self.inverse_distance_mode.isChecked():
            algorithm = 'invdist'
        elif self.use_ascii_mode.isChecked():
            algorithm = 'use_ascii'

        # Smoothing
        smoothing_method = NONE_SMOOTHING
        if self.numpy_smoothing.isChecked():
            smoothing_method = NUMPY_SMOOTHING
        if self.scipy_smoothing.isChecked():
            smoothing_method = SCIPY_SMOOTHING

        # noinspection PyUnresolvedReferences
        QgsApplication.instance().setOverrideCursor(
            QtGui.QCursor(QtCore.Qt.WaitCursor)
        )

        extra_keywords = {}
        if self.check_box_custom_shakemap_id.isChecked():
            event_id = self.line_edit_shakemap_id.text()
            extra_keywords[extra_keyword_earthquake_event_id['key']] = event_id

        current_index = self.combo_box_source_type.currentIndex()
        source_type = self.combo_box_source_type.itemData(current_index)
        if source_type:
            extra_keywords[
                extra_keyword_earthquake_source['key']] = source_type

        file_name = convert_mmi_data(
            input_path,
            input_title,
            input_source,
            output_path,
            algorithm=algorithm,
            algorithm_filename_flag=True,
            smoothing_method=smoothing_method,
            extra_keywords=extra_keywords
        )

        file_info = QFileInfo(file_name)
        base_name = file_info.baseName()
        self.output_layer = QgsRasterLayer(file_name, base_name)

        # noinspection PyUnresolvedReferences
        QgsApplication.instance().restoreOverrideCursor()

        if self.load_result.isChecked():
            # noinspection PyTypeChecker
            mmi_ramp_roman(self.output_layer)
            self.output_layer.saveDefaultStyle()
            if not self.output_layer.isValid():
                LOGGER.debug("Failed to load")
            else:
                # noinspection PyArgumentList
                QgsProject.instance().addMapLayer(self.output_layer)
                iface.zoomToActiveLayer()

        if (self.keyword_wizard_checkbox.isChecked()
                and self.keyword_wizard_checkbox.isEnabled()):
            self.launch_keyword_wizard()

        self.done(self.Accepted)