def _validate(self, db, db_qr, layer_dict, tolerance, **kwargs):
        self.progress_changed.emit(5)
        # First, run an ili2db --validate on the data
        model = LADMColModelRegistry().model(LADMNames.SURVEY_MODEL_KEY)
        res, msg = Ili2DB().validate(db, [model.full_name()], self._xtf_log)

        if not res:
            return QualityRuleExecutionResult(
                EnumQualityRuleResult.CRITICAL,
                QCoreApplication.translate(
                    "QualityRules",
                    "There was an error running the quality rule '{}'! Details: '{}'."
                ).format(self._id, msg))

        self.progress_changed.emit(85)

        error_layer = self.app.core.get_layer(db_qr,
                                              db_qr.names.ERR_QUALITY_ERROR_T,
                                              load=True)
        count_before = error_layer.featureCount()

        # Write errors to QR DB
        res, msg = IliVErrorsToErroresCalidad01Converter().convert(
            self._xtf_log, db_qr, params=dict())

        self.progress_changed.emit(100)

        if not res:
            return QualityRuleExecutionResult(
                EnumQualityRuleResult.CRITICAL,
                QCoreApplication.translate(
                    "QualityRules",
                    "There was an error running the quality rule '{}'! Details: '{}'."
                )).format(self._id, msg)
        else:
            count = error_layer.featureCount() - count_before
            if count:
                return QualityRuleExecutionResult(
                    EnumQualityRuleResult.ERRORS,
                    QCoreApplication.translate(
                        "QualityRules",
                        "There were {} errors validating the data against their model!"
                    ).format(count), count)
            else:
                return QualityRuleExecutionResult(
                    EnumQualityRuleResult.SUCCESS,
                    QCoreApplication.translate(
                        "QualityRules", "The data comply with their model."))
コード例 #2
0
    def update_configuration(self, ili2db: Ili2DB):
        """
        Get the configuration that is updated with the user configuration changes on the dialog.
        :return: Configuration
        """
        disable_validation = \
            not QSettings().value('Asistente-LADM-COL/models/validate_data_importing_exporting', True, bool)

        configuration = ili2db.get_import_data_configuration(self.db, self.xtf_file_line_edit.text().strip(),
                                                             disable_validation=disable_validation)

        configuration.delete_data = False

        # TODO this is different to ili2db.py
        if self.get_ili_models():
            configuration.ilimodels = ';'.join(self.get_ili_models())

        return configuration
コード例 #3
0
    def accepted(self):
        self._running_tool = True
        self.txtStdout.clear()
        self.progress_bar.setValue(0)
        self.bar.clearWidgets()

        if not os.path.isfile(self.xtf_file_line_edit.text().strip()):
            self._running_tool = False
            error_msg = QCoreApplication.translate("DialogImportData", "Please set a valid XTF file before importing data. XTF file does not exist.")
            self.txtStdout.setText(error_msg)
            self.show_message(error_msg, Qgis.Warning)
            self.xtf_file_line_edit.setFocus()
            return

        java_home_set = self.java_dependency.set_java_home()
        if not java_home_set:
            message_java = QCoreApplication.translate("DialogImportData", """Configuring Java {}...""").format(JAVA_REQUIRED_VERSION)
            self.txtStdout.setTextColor(QColor('#000000'))
            self.txtStdout.clear()
            self.txtStdout.setText(message_java)
            self.java_dependency.get_java_on_demand()
            self.disable()
            return

        ili2db = Ili2DB()

        configuration = self.update_configuration(ili2db)
        configuration = self.apply_role_model_configuration(configuration)

        if configuration.disable_validation:  # If data validation at import is disabled, we ask for confirmation
            self.msg = QMessageBox()
            self.msg.setIcon(QMessageBox.Question)
            self.msg.setText(QCoreApplication.translate("DialogImportData",
                                                        "Are you sure you want to import your data without validation?"))
            self.msg.setWindowTitle(QCoreApplication.translate("DialogImportData", "Import XTF without validation?"))
            self.msg.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
            res = self.msg.exec_()
            if res == QMessageBox.No:
                self._running_tool = False
                return

        if not self.xtf_file_line_edit.validator().validate(configuration.xtffile, 0)[0] == QValidator.Acceptable:
            self._running_tool = False
            error_msg = QCoreApplication.translate("DialogImportData", "Please set a valid XTF before importing data.")
            self.txtStdout.setText(error_msg)
            self.show_message(error_msg, Qgis.Warning)
            self.xtf_file_line_edit.setFocus()
            return

        if not self.get_ili_models():
            self._running_tool = False
            error_msg = QCoreApplication.translate("DialogImportData", "The selected XTF file does not have information according to the LADM-COL model to import.")
            self.txtStdout.setText(error_msg)
            self.show_message(error_msg, Qgis.Warning)
            self.import_models_list_view.setFocus()
            return

        # Get list of models present in the XTF file, in the DB and in the list of required models (by the plugin)
        ili_models = set([ili_model for ili_model in self.get_ili_models()])

        supported_models_in_ili = set([m.full_name() for m in self.__ladmcol_models.supported_models()]).intersection(ili_models)

        if not supported_models_in_ili:
            self._running_tool = False
            error_msg = QCoreApplication.translate("DialogImportData",
                                                   "The selected XTF file does not have data from any LADM-COL model supported by the LADM-COL Assistant. " \
                                                   "Therefore, you cannot import it! These are the models supported:\n\n * {}").format(" \n * ".join([m.full_alias() for m in self.__ladmcol_models.supported_models()]))
            self.txtStdout.setText(error_msg)
            self.show_message(error_msg, Qgis.Warning)
            self.import_models_list_view.setFocus()
            return

        db_models = set(self.db.get_models())
        suggested_models = sorted(ili_models.difference(db_models))

        if not ili_models.issubset(db_models):
            self._running_tool = False
            error_msg = QCoreApplication.translate("DialogImportData",
                                                   "IMPORT ERROR: The XTF file to import does not have the same models as the target database schema. " \
                                                   "Please create a schema that also includes the following missing modules:\n\n * {}").format(
                " \n * ".join(suggested_models))
            self.txtStdout.clear()
            self.txtStdout.setTextColor(QColor('#000000'))
            self.txtStdout.setText(error_msg)
            self.show_message(error_msg, Qgis.Warning)
            self.xtf_file_line_edit.setFocus()

            # button is removed to define order in GUI
            for button in self.buttonBox.buttons():
                if button.text() == self.BUTTON_NAME_IMPORT_DATA:
                    self.buttonBox.removeButton(button)

            # Check if button was previously added
            self.remove_create_structure_button()

            if self.link_to_import_schema:
                self.buttonBox.addButton(self.BUTTON_NAME_GO_TO_CREATE_STRUCTURE,
                                         QDialogButtonBox.AcceptRole).setStyleSheet("color: #aa2222;")
            self.buttonBox.addButton(self.BUTTON_NAME_IMPORT_DATA,
                                     QDialogButtonBox.AcceptRole)

            return

        self.progress_bar.show()

        self.disable()
        self.txtStdout.setTextColor(QColor('#000000'))
        self.txtStdout.clear()

        self._connect_ili2db_signals(ili2db)

        self.save_configuration(configuration)

        res, msg = ili2db.import_data(self.db, configuration)

        self._disconnect_ili2db_signals(ili2db)

        self._running_tool = False

        self.progress_bar.setValue(25)

        if res:
            self.buttonBox.clear()
            self.buttonBox.setEnabled(True)
            self.buttonBox.addButton(QDialogButtonBox.Close)
            self.progress_bar.setValue(100)

        message_type = Qgis.Success if res else Qgis.Warning
        self.show_message(msg, message_type)
コード例 #4
0
    def accepted(self):
        self._running_tool = True
        self.txtStdout.clear()
        self.progress_bar.setValue(0)
        self.bar.clearWidgets()

        java_home_set = self.java_dependency.set_java_home()
        if not java_home_set:
            message_java = QCoreApplication.translate(
                "DialogExportData",
                """Configuring Java {}...""").format(JAVA_REQUIRED_VERSION)
            self.txtStdout.setTextColor(QColor('#000000'))
            self.txtStdout.clear()
            self.txtStdout.setText(message_java)
            self.java_dependency.get_java_on_demand()
            self.disable()
            return

        ili2db = Ili2DB()
        configuration = self.update_configuration(ili2db)

        if configuration.disable_validation:  # If data validation at export is disabled, we ask for confirmation
            self.msg = QMessageBox()
            self.msg.setIcon(QMessageBox.Question)
            self.msg.setText(
                QCoreApplication.translate(
                    "DialogExportData",
                    "Are you sure you want to export your data without validation?"
                ))
            self.msg.setWindowTitle(
                QCoreApplication.translate("DialogExportData",
                                           "Export XTF without validation?"))
            self.msg.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
            res = self.msg.exec_()
            if res == QMessageBox.No:
                self._running_tool = False
                return

        if not self.xtf_file_line_edit.validator().validate(
                configuration.xtffile, 0)[0] == QValidator.Acceptable:
            self._running_tool = False
            message_error = QCoreApplication.translate(
                "DialogExportData",
                "Please set a valid XTF file before exporting data.")
            self.txtStdout.setText(message_error)
            self.show_message(message_error, Qgis.Warning)
            self.xtf_file_line_edit.setFocus()
            return

        if not self.get_ili_models():
            self._running_tool = False
            message_error = QCoreApplication.translate(
                "DialogExportData",
                "Please set a valid schema to export. This schema does not have information to export."
            )
            self.txtStdout.setText(message_error)
            self.show_message(message_error, Qgis.Warning)
            self.export_models_list_view.setFocus()
            return

        # If xtf browser was opened and the file exists, the user already chose
        # to overwrite the file
        if os.path.isfile(self.xtf_file_line_edit.text().strip()
                          ) and not self.xtf_browser_was_opened:
            self.msg = QMessageBox()
            self.msg.setIcon(QMessageBox.Warning)
            self.msg.setText(
                QCoreApplication.translate(
                    "DialogExportData",
                    "{filename} already exists.\nDo you want to replace it?").
                format(filename=os.path.basename(
                    self.xtf_file_line_edit.text().strip())))
            self.msg.setWindowTitle(
                QCoreApplication.translate("DialogExportData",
                                           "Save in XTF Transfer File"))
            self.msg.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
            msg_box = self.msg.exec_()
            if msg_box == QMessageBox.No:
                self._running_tool = False
                return

        self.progress_bar.show()

        self.disable()
        self.txtStdout.setTextColor(QColor('#000000'))
        self.txtStdout.clear()

        self._connect_ili2db_signals(ili2db)
        self.save_configuration(configuration)
        res, msg = ili2db.export(self.db, configuration)
        self._disconnect_ili2db_signals(ili2db)
        self._running_tool = False

        self.progress_bar.setValue(25)

        if res:
            self.buttonBox.clear()
            self.buttonBox.setEnabled(True)
            self.buttonBox.addButton(QDialogButtonBox.Close)
            self.progress_bar.setValue(100)
        else:
            # Since the export was not successful, we'll try to remove any temp XTF generated
            if os.path.exists(configuration.xtffile):
                try:
                    os.remove(configuration.xtffile)
                except:
                    pass

        message_type = Qgis.Success if res else Qgis.Warning
        self.show_message(msg, message_type)

        # Inform other classes whether the execution was successful or not
        self.on_result.emit(res)
    def get_quality_error_connector(self,
                                    output_path,
                                    timestamp,
                                    load_layers=False):
        # TODO: should we support both new and existent gpkgs in this method? I guess so!
        self.progress_changed.emit(0)

        res, msg, output_path = QualityErrorDBUtils.get_quality_validation_output_path(
            output_path, timestamp)
        if not res:
            return False, msg, None

        db_file = os.path.join(output_path,
                               "Reglas_de_Calidad_{}.gpkg".format(timestamp))
        db = GPKGConnector(db_file)
        ili2db = Ili2DB()
        error_model = LADMColModelRegistry().model(
            LADMNames.QUALITY_ERROR_MODEL_KEY)

        configuration = ili2db.get_import_schema_configuration(
            db, [error_model.full_name()])
        res, msg = ili2db.import_schema(db, configuration)

        self.progress_changed.emit(50)

        if res:
            for catalog_key, catalog_xtf_path in error_model.get_catalogs(
            ).items():
                logger.info(
                    __name__,
                    "Importing catalog '{}' to quality error database...".
                    format(catalog_key))
                configuration = ili2db.get_import_data_configuration(
                    db, catalog_xtf_path)
                res_xtf, msg_xtf = ili2db.import_data(db, configuration)
                if not res_xtf:
                    logger.warning(
                        __name__,
                        "There was a problem importing catalog '{}'! Skipping..."
                        .format(catalog_key))
        else:
            return False, "There were errors creating the 'Errores Calidad' structure!", None

        self.progress_changed.emit(80)

        if getattr(db.names, "T_ID_F",
                   None) is None or db.names.T_ID_F is None:
            db.test_connection()  # Just to build the names object

        if load_layers:
            names = db.names
            layers = {
                names.ERR_QUALITY_ERROR_T: None,
                names.ERR_RULE_TYPE_T: None,
                names.ERR_ERROR_TYPE_T: None,
                names.ERR_POINT_T: None,
                names.ERR_LINE_T: None,
                names.ERR_POLYGON_T: None,
                names.ERR_METADATA_T: None,
                names.ERR_ERROR_STATE_D: None
            }
            app.core.get_layers(
                db,
                layers,
                load=True,
                group=QualityErrorDBUtils.get_quality_error_group(timestamp))

        self.progress_changed.emit(100)

        return res, msg, None if not res else db
コード例 #6
0
    def accepted(self):
        self._running_tool = True
        self.txtStdout.clear()
        self.progress_bar.setValue(0)
        self.bar.clearWidgets()

        java_home_set = self.java_dependency.set_java_home()
        if not java_home_set:
            message_java = QCoreApplication.translate(
                "DialogImportSchema",
                """Configuring Java {}...""").format(JAVA_REQUIRED_VERSION)
            self.txtStdout.setTextColor(QColor('#000000'))
            self.txtStdout.clear()
            self.txtStdout.setText(message_java)
            self.java_dependency.get_java_on_demand()
            self.disable()
            return

        if not self.get_checked_models():
            self._running_tool = False
            message_error = QCoreApplication.translate(
                "DialogImportSchema",
                "You should select a valid model(s) before creating the LADM-COL structure."
            )
            self.txtStdout.setText(message_error)
            self.show_message(message_error, Qgis.Warning)
            self.import_models_list_widget.setFocus()
            return

        self.save_configuration()

        self.progress_bar.show()
        self.disable()
        self.txtStdout.setTextColor(QColor('#000000'))
        self.txtStdout.clear()

        ili2db = Ili2DB()

        self._connect_ili2db_signals(ili2db)

        models = self.get_checked_models()

        configuration = ili2db.get_import_schema_configuration(self.db, models)
        configuration = self.apply_role_model_configuration(configuration)

        res, msg = ili2db.import_schema(self.db, configuration)

        self._disconnect_ili2db_signals(ili2db)

        self._running_tool = False

        if res:
            self.buttonBox.clear()
            if self.link_to_import_data:
                self.buttonBox.addButton(
                    self.BUTTON_NAME_GO_TO_IMPORT_DATA,
                    QDialogButtonBox.AcceptRole).setStyleSheet(
                        "color: #007208;")
            self.buttonBox.setEnabled(True)
            self.buttonBox.addButton(QDialogButtonBox.Close)
            self.progress_bar.setValue(100)
            self.print_info(
                QCoreApplication.translate("DialogImportSchema", "\nDone!"),
                '#004905')
            self._db_was_changed = True  # Schema could become LADM compliant after a schema import

            if self.db_source == COLLECTED_DB_SOURCE:
                self.logger.info(
                    __name__,
                    "Schedule a call to refresh db relations cache since a Schema Import was run on the current 'collected' DB."
                )
                self._schedule_layers_and_relations_refresh = True

        message_type = Qgis.Success if res else Qgis.Warning
        self.show_message(msg, message_type)
        # Inform other classes whether the execution was successful or not
        self.on_result.emit(res)