Beispiel #1
0
    def __init__(self, dock_widget: GeologicalDataProcessingDockWidget) -> None:
        """
        Initialize the view
        :param dock_widget: current GeologicalDataProcessingDockWidget instance
        """

        self.logger = QGISLogHandler(self.__class__.__name__)
        self.__combos = dict()
        self._dwg = dock_widget

        # initialize user interface
        self._import_service = ImportService.get_instance()
        self._import_service.reset_import.connect(self.reset_import)
        self._import_service.import_columns_changed.connect(self._on_import_columns_changed)

        self._table_view: QTableView or None = None
        self._only_number_in_table_view: bool = False
        self._table_model: PropertyImportModel = PropertyImportModel()
        self._dwg.start_import_button.clicked.connect(self._on_start_import)
        self._controller_thread: ImportControllersInterface or None = None

        super().__init__()
Beispiel #2
0
    def run(self):
        """
        Save well logs to the database
        :return: Nothing, emits Qt Signals
        """

        self._mutex.lock()
        service = DatabaseService.get_instance()
        service.close_session()
        service.connect()
        session = service.get_session()

        failed_imports = 0
        try:
            onekey = self._data[next(iter(self._data.keys()))]["values"]
            # import json
            # self._logger.info(json.dumps(onekey, indent=2))
            count = len(onekey)

            well_name_col = self._selection["well_name"]
            depth_col = self._selection["depth"]

            reference = ImportService.get_instance().get_crs()
            if reference is None:
                reference = ""
            else:
                reference = reference.toWkt()

            self._logger.debug("Saving with reference system\n{}".format(reference))

            for i in range(count):
                self.update_progress.emit(100 * i / count)

                try:
                    well_name = self._data[well_name_col]["values"][i]
                except ValueError:
                    self._logger.warn("No well_name specified for current data set at line {}".format(i))
                    failed_imports += 1
                    continue

                if (well_name is None) or (well_name == ""):
                    self._logger.warn("Unknown well name found: [{}]".format(well_name))
                    failed_imports += 1
                    continue

                try:
                    well: Well = Well.load_by_wellname_from_db(well_name, session)
                    if well is None:
                        self._logger.warn("No well with name [{}] found...".format(well_name))
                        failed_imports += 1
                        continue

                    well.reference_system = reference

                    # add / update properties
                    for item in self._properties:
                        self._logger.debug("Property: {}".format(item))
                        if well.has_log(item.name):
                            log = well.get_log(item.name)
                        else:
                            log = WellLog(property_name = item.name, property_unit = item.unit, session=session)
                            well.add_log(log)

                        depth = self._data[depth_col]["values"][i]
                        value = self._data[item.name]["values"][i]
                        try:
                            log_value: WellLogValue = log.get_value_by_depth(depth)
                        except ValueError:
                            log_value = WellLogValue(depth, value, session=session)

                        log_value.value = value
                        log.insert_log_value(log_value)

                    self._logger.debug("well: {}".format(well))
                    well.save_to_db()

                except DatabaseRequestException:
                    self._logger.warn("Cannot find well with name [{}]. Skipping log import".format(well_name))
                    failed_imports += 1

                if self._cancel:
                    self._logger.debug("Import canceled")
                    self.import_failed.emit(self._message)
                    break

            if not self._cancel:
                if failed_imports > 0:
                    self.import_finished_with_warnings.emit("Could not import {} properties.".format(failed_imports))
                self.update_progress.emit(100)
                self.import_finished.emit()

        except Exception as e:
            msg = str(ExceptionHandler(e))
            self.import_failed.emit(msg)
            self._logger.error("Error", msg, to_messagebar=True)

        finally:
            service.close_session()

        self._mutex.unlock()
        self.quit()
Beispiel #3
0
    def run(self):
        """
        Save the Properties to the database
        :return: Nothing, emits Qt Signals
        """

        self._mutex.lock()
        service = DatabaseService.get_instance()
        service.close_session()
        service.connect()
        session = service.get_session()

        failed_imports = 0
        try:
            onekey = self._data[next(iter(self._data.keys()))]["values"]
            # import json
            # self._logger.info(json.dumps(onekey, indent=2))
            count = len(onekey)

            id_col = self._selection["id"]

            reference = ImportService.get_instance().get_crs()
            if reference is None:
                reference = ""
            else:
                reference = reference.toWkt()

            self._logger.debug("Saving with reference system\n{}".format(reference))

            for i in range(count):
                self.update_progress.emit(100 * i / count)

                try:
                    _id = int(self._data[id_col]["values"][i])
                except ValueError:
                    self._logger.warn("No id specified for current data set at line {}".format(i), only_logfile=True)
                    failed_imports += 1
                    continue

                if (_id is None) or (_id < 0):
                    self._logger.warn("Unknown Geopoint ID found: [{}]".format(_id))
                    failed_imports += 1
                    continue

                try:
                    point = GeoPoint.load_by_id_from_db(_id, session)
                    if point is None:
                        self._logger.warn("No Geopoint with ID [{}] found".format(_id))
                        failed_imports += 1
                        continue

                    point.reference_system = reference

                    # add / update properties
                    for item in self._properties:
                        self._logger.debug("Property: {}".format(item))
                        if point.has_property(item.name):
                            p = point.get_property(item.name)
                            p.property_unit = item.unit
                            p.value = self._data[item.name]["values"][i]
                            p.property_type = item.property_type
                        else:
                            p = Property(value=self._data[item.name]["values"][i], property_name=item.name,
                                         _type=item.property_type, property_unit=item.unit,
                                         session=session)
                            point.add_property(p)

                    self._logger.debug("point: {}".format(point))
                    point.save_to_db()

                except DatabaseRequestException:
                    self._logger.warn("Cannot find Geopoint with ID [{}]. Skipping property import".format(_id))
                    failed_imports += 1

                if self._cancel:
                    self._logger.debug("Import canceled")
                    self.import_failed.emit(self._message)
                    break

            if not self._cancel:
                if failed_imports > 0:
                    self.import_finished_with_warnings.emit("Could not import {} properties.".format(failed_imports))
                self.update_progress.emit(100)
                self.import_finished.emit()

        except Exception as e:
            msg = str(ExceptionHandler(e))
            self.import_failed.emit(msg)
            self._logger.error("Error", msg, to_messagebar=True)

        finally:
            service.close_session()

        self._mutex.unlock()
        self.quit()
Beispiel #4
0
    def run(self) -> bool:
        """
        Save the Well Object(s) to the database
        :return: True if function executed successfully, else False
        """

        self._mutex.lock()
        service = DatabaseService.get_instance()
        service.close_session()
        service.connect()
        session = DatabaseService.get_instance().get_session()

        try:
            onekey = self._data[next(iter(self._data.keys()))]["values"]
            count = len(onekey)

            name = self._selection["name"]
            short_name = self._selection["short_name"]
            east = self._selection["easting"]
            north = self._selection["northing"]
            alt = self._selection["altitude"]
            total_depth = self._selection["total_depth"]
            strat = self._selection["strat"]
            depth_to = self._selection["depth_to"]
            comment = self._selection["comment"]

            reference = ImportService.get_instance().get_crs()
            if reference is None:
                reference = ""
            else:
                reference = reference.toWkt()

            self._logger.debug("Saving with reference system\n{}".format(reference))

            wells = dict()

            maximum = count
            for i in range(count):
                if (self._data[east]["values"][i] == "") and (self._data[north]["values"][i] == ""):
                    continue

                na = self._data[name]["values"][i]
                sn = "" if short_name == "" else self._data[short_name]["values"][i]
                e = float(self._data[east]["values"][i])
                n = float(self._data[north]["values"][i])
                kb = None if alt == "" else float(self._data[alt]["values"][i])
                td = -1 if total_depth == "" else float(self._data[total_depth]["values"][i])
                s = "" if strat == "" else self._data[strat]["values"][i]
                dt = -1 if depth_to == "" else float(self._data[depth_to]["values"][i])
                c = "" if comment == "" else self._data[comment]["values"][i]

                strat_obj = StratigraphicObject.init_stratigraphy(session, s, -1)
                strat_obj.save_to_db()

                marker = WellMarker(dt, strat_obj, session=session, comment=c)
                # marker.save_to_db()
                # point = GeoPoint(None, False if (h is None) else True, reference,
                #                e, n, 0 if (h is None) else h, session, sn, c)

                if na in wells:
                    wells[na]["marker"].append(marker)
                else:
                    wells[na] = {
                        "short_name": sn,
                        "easting": e,
                        "northing": n,
                        "altitude": kb,
                        "total_depth": td,
                        "marker": [marker]
                    }

                    maximum += 1

                if self._cancel:
                    self._logger.debug("Import canceled")
                    self.import_failed.emit(self._message)
                    break

                self.update_progress.emit(100 * i / maximum)

            i = count
            if not self._cancel:
                for well_name in wells:
                    i += 1
                    well = wells[well_name]

                    new_well = Well.load_by_wellname_from_db(well_name, session)
                    if new_well is not None:
                        try:
                            self._logger.debug("Updating existing well: {}".format(new_well))
                            for marker in new_well.marker:
                                WellMarker.delete_from_db(marker, session)

                            new_well.save_to_db()
                            new_well.marker = well["marker"]
                            new_well.short_name = well["short_name"]
                            new_well.easting = well["easting"]
                            new_well.northing = well["northing"]
                            new_well.depth = well["total_depth"]
                        except WellMarkerDepthException as e:
                            self._logger.error("WellMarkerDepthException", "{}\n{}".format(new_well, str(e)))
                            return False
                    else:
                        self._logger.debug("Creating new well with name [{}]".format(well_name))
                        new_well = Well(well_name, well["short_name"], well["total_depth"], reference_system=reference,
                                        easting=well["easting"], northing=well["northing"], altitude=well["altitude"],
                                        session=session)

                        new_well.marker = well["marker"]

                    new_well.save_to_db()

                    self._logger.debug("Saved well:\n{}".format(new_well))

                    self.update_progress.emit(100 * i / maximum)

                    if self._cancel:
                        self._logger.debug("Import canceled")
                        self.import_failed.emit(self._message)
                        break

            if not self._cancel:
                self.update_progress.emit(100)
                self._logger.debug("Lines successfully imported")
                self.import_finished.emit()

        except Exception as e:
            msg = str(ExceptionHandler(e))
            self.import_failed.emit(msg)
            self._logger.error("Error", msg)

        finally:
            service.close_session()

        self._mutex.unlock()
        self.quit()
Beispiel #5
0
    def run(self):
        """
        Save the Point Object(s) to the database
        :return: True if function executed successfully, else False
        """
        self._mutex.lock()
        service = DatabaseService.get_instance()
        service.close_session()
        service.connect()
        session = DatabaseService.get_instance().get_session()

        try:
            onekey = self._data[next(iter(self._data.keys()))]["values"]
            # import json
            # self._logger.info(json.dumps(onekey, indent=2))
            count = len(onekey)

            east = self._selection["easting"]
            north = self._selection["northing"]
            alt = self._selection["altitude"]
            strat = self._selection["strat"]
            age = self._selection["strat_age"]
            set_name = self._selection["set_name"]
            comment = self._selection["comment"]

            reference = ImportService.get_instance().get_crs()
            if reference is None:
                reference = ""
            else:
                reference = reference.toWkt()

            self._logger.debug("Saving with reference system\n{}".format(reference))

            lines = dict()

            line = 0

            maximum = count
            for i in range(count):
                _id = None
                if "gln_id" in self._data:
                    try:
                        _id = int(self._data["gln_id"]["values"][i])
                    except ValueError:
                        pass

                if (self._data[east]["values"][i] == "") and (self._data[north]["values"][i] == ""):
                    line += 1
                    continue

                e = float(self._data[east]["values"][i])
                n = float(self._data[north]["values"][i])
                h = None if alt == "" else float(self._data[alt]["values"][i])
                s = "" if strat == "" else self._data[strat]["values"][i]
                a = -1 if age == "" else float(self._data[age]["values"][i])
                sn = "" if set_name == "" else self._data[set_name]["values"][i]
                c = "" if comment == "" else self._data[comment]["values"][i]

                strat_obj = StratigraphicObject.init_stratigraphy(session, s, a)
                point = GeoPoint(None, False if (h is None) else True, reference,
                                 e, n, 0 if (h is None) else h, session, sn, c)

                # add / update properties
                for item in self._properties:
                    self._logger.debug("Property: {}".format(item))
                    if point.has_property(item.name):
                        p = point.get_property(item.name)
                        p.property_unit = item.unit
                        p.value = self._data[item.name]["values"][i]
                        p.property_type = item.property_type
                    else:
                        p = Property(value=self._data[item.name]["values"][i], property_name=item.name,
                                     _type=item.property_type, property_unit=item.unit,
                                     session=session)
                        point.add_property(p)

                if _id is not None:
                    point.line_id = _id

                self._logger.debug("line point: {}".format(point))

                if line in lines:
                    lines[line]["points"].append(point)
                else:
                    lines[line] = {
                        "id": _id,
                        "strat": strat_obj,
                        "points": [point],
                        "name": sn,
                        "comment": c
                    }
                    maximum += 1

                i += 1
                self.update_progress.emit(100 * i / maximum)

                if self._cancel:
                    self._logger.debug("Import canceled")
                    self.import_failed.emit(self._message)
                    break

            i = count
            if not self._cancel:
                for l in lines:
                    i += 1

                    line = lines[l]
                    closed = False
                    if (len(line["points"]) > 1) and (line["points"][0] == line["points"][-1]):
                        closed = True
                        line["points"].pop()

                    new_line = None
                    if line["id"] is not None:
                        new_line = Line.load_by_id_from_db(line["id"], session)
                        if new_line is not None:
                            for point in new_line.points:
                                AbstractDBObject.delete_from_db(point, session)
                                del point

                            new_line.closed = closed
                            new_line.points = line["points"]
                            new_line.horizon = line["strat"]
                            new_line.name = line["name"]
                            new_line.comment = line["comment"]

                            self._logger.debug("Updated existing line")

                    if line["id"] is None or new_line is None:  # new_line is None ? not in database -> create a new one
                        new_line = Line(closed, line["strat"], line["points"], session, line["name"], line["comment"])
                        self._logger.debug("Created new line")

                    new_line.save_to_db()
                    self._logger.debug("Line: {}".format(str(new_line)))

                    self.update_progress.emit(100 * i / maximum)

                    if self._cancel:
                        self._logger.debug("Import canceled")
                        self.import_failed.emit(self._message)
                        break

            if not self._cancel:
                self.update_progress.emit(100)
                self._logger.debug("Lines successfully imported")
                self.import_finished.emit()

        except Exception as e:
            msg = str(ExceptionHandler(e))
            self.import_failed.emit(msg)
            self._logger.error("Error", msg)

        finally:
            service.close_session()

        self._mutex.unlock()
        self.quit()
Beispiel #6
0
    def run(self):
        """
        Save the Point Object(s) to the database
        :return: True if function executed successfully, else False
        """

        self._mutex.lock()
        service = DatabaseService.get_instance()
        service.close_session()
        service.connect()
        session = service.get_session()

        try:
            onekey = self._data[next(iter(self._data.keys()))]["values"]
            # import json
            # self._logger.info(json.dumps(onekey, indent=2))
            count = len(onekey)

            east = self._selection["easting"]
            north = self._selection["northing"]
            alt = self._selection["altitude"]
            strat = self._selection["strat"]
            age = self._selection["strat_age"]
            set_name = self._selection["set_name"]
            comment = self._selection["comment"]

            reference = ImportService.get_instance().get_crs()
            if reference is None:
                reference = ""
            else:
                reference = reference.toWkt()

            self._logger.debug("Saving with reference system\n{}".format(reference))

            for i in range(count):
                self.update_progress.emit(100 * i / count)

                if (self._data[east]["values"][i] == "") and (self._data[north]["values"][i] == ""):
                    continue

                _id = None
                if "gpt_id" in self._data:
                    try:
                        _id = int(self._data["gpt_id"]["values"][i])
                    except ValueError:
                        pass

                e = float(self._data[east]["values"][i])
                n = float(self._data[north]["values"][i])
                h = None if alt == "" else float(self._data[alt]["values"][i])
                s = "" if strat == "" else self._data[strat]["values"][i]
                a = -1 if age == "" else float(self._data[age]["values"][i])
                sn = "" if set_name == "" else self._data[set_name]["values"][i]
                c = "" if comment == "" else self._data[comment]["values"][i]

                strat_obj = StratigraphicObject.init_stratigraphy(session, s, a)

                if (_id is not None) and (_id > -1):
                    point = GeoPoint.load_by_id_from_db(_id, session)
                    point.easting = e
                    point.northing = n
                    point.altitude = h
                    point.del_z() if h is None else point.use_z()
                    point.reference_system = reference
                    point.horizon = strat_obj
                    point.name = sn
                    point.comment = c
                    self._logger.debug("point update")

                else:
                    point = GeoPoint(strat_obj, False if (h is None) else True, reference,
                                     e, n, 0 if (h is None) else h, session, sn, c)
                    self._logger.debug("new point")

                # add / update properties
                for item in self._properties:
                    self._logger.debug("Property: {}".format(item))
                    if point.has_property(item.name):
                        p = point.get_property(item.name)
                        p.property_unit = item.unit
                        p.value = self._data[item.name]["values"][i]
                        p.property_type = item.property_type
                    else:
                        p = Property(value=self._data[item.name]["values"][i], property_name=item.name,
                                     _type=item.property_type, property_unit=item.unit,
                                     session=session)
                        point.add_property(p)

                self._logger.debug("point: {}".format(point))
                point.save_to_db()

                if self._cancel:
                    self._logger.debug("Import canceled")
                    self.import_failed.emit(self._message)
                    break

            if not self._cancel:
                self.update_progress.emit(100)
                self._logger.debug("Points successfully imported")
                self.import_finished.emit()

        except Exception as e:
            msg = str(ExceptionHandler(e))
            self.import_failed.emit(msg)
            self._logger.error("Error", msg)

        finally:
            service.close_session()

        self._mutex.unlock()
        self.quit()
    def run(self) -> None:
        """Run method that loads and starts the plugin"""

        if not self.pluginIsActive:
            self.pluginIsActive = True

            try:
                # initialize logger
                logger = QGISLogHandler()
                logger.qgis_iface = self.iface
                logger.save_to_file = True

                if packages_found == "NO_PACKAGES" or not ModuleService.check_required_modules():
                    logger.info("installing or updating packages")
                    if not ModuleService.install_packages():
                        logger.error("package installation failed, please restart QGIS to try again.")
                    else:
                        logger.info("package installation successful, please restart QGIS")
                        msg = QMessageBox()
                        msg.setIcon(QMessageBox.Information)
                        msg.setText("packages installation successful")
                        msg.setInformativeText("Please restart QGIS to use the GeologicalDataProcessing extension")

                        msg.setWindowTitle("package update")
                        msg.exec_()
                        return

                    return
                else:
                    logger.debug("all required packages up2date")

                # dockwidget may not exist if:
                #    first run of plugin
                #    removed on close (see self.onClosePlugin method)
                if self.dockwidget is None:
                    # Create the dockwidget (after translation) and keep reference
                    self.dockwidget = GeologicalDataProcessingDockWidget()

                if self.settings_dialog is None:
                    self.settings_dialog = SettingsDialog(parent=self.dockwidget)
                    self.settings_dialog.setModal(True)
                    self.dockwidget.settings_button.clicked.connect(self.settings_dialog.exec)

                # connect to provide cleanup on closing of dockwidget
                self.dockwidget.closingPlugin.connect(self.onClosePlugin)

                # show the dockwidget
                # TODO: fix to allow choice of dock location
                self.iface.addDockWidget(Qt.RightDockWidgetArea, self.dockwidget)
                self.dockwidget.show()

                from GeologicalDataProcessing.controller.database_controller import DatabaseController
                from GeologicalDataProcessing.services.import_service import ImportService
                from GeologicalDataProcessing.views.import_views import LineImportView, PointImportView, \
                    WellImportView, PropertyImportView, WellLogImportView

                ImportService.get_instance(self.dockwidget)

                # initialize the gui and connect signals and slots
                self.dockwidget.import_type.currentChanged.connect(self.on_import_type_changed_event)

                # start tests button
                # -> only visible and active when the debug flag is True
                if config.debug:
                    self.dockwidget.start_tests_button.clicked.connect(self.on_start_tests)
                else:
                    self.dockwidget.start_tests_button.setVisible(False)
                    self.dockwidget.start_tests_separator.setVisible(False)

                self.dockwidget.progress_bar_layout.setVisible(False)

                self.__views["import_points"] = PointImportView(self.dockwidget)
                self.__views["import_lines"] = LineImportView(self.dockwidget)
                self.__views["import_wells"] = WellImportView(self.dockwidget)
                self.__views["import_properties"] = PropertyImportView(self.dockwidget)
                self.__views["import_well_logs"] = WellLogImportView(self.dockwidget)

                self.__db_controller = DatabaseController(self.settings_dialog)

                if config.debug:
                    self.dockwidget.import_file.setText(
                        "/Users/stephan/Library/Application Support/QGIS/QGIS3/profiles/" +
                        "default/python/plugins/GeologicalDataProcessing/tests/test_data/point_data.txt")

            except Exception as e:
                ExceptionHandler(e).log()