コード例 #1
0
    def setUp(self):
        # type: () -> None
        """
        Initialise a temporary database connection for all test cases and fill the database with test data

        :return: None
        """
        # initialise a in-memory sqlite database
        self.handler = DBHandler(connection="sqlite://", echo=False)
        self.session = self.handler.get_session()

        # add test data to the database
        self.well = {
            "well_name": "Well_1",
            "short_name": "W1",
            "reference": "none",
            "east": 1234.56,
            "north": 123.45,
            "altitude": 10.5,
            "depth": 555,
            "log_values": ((10, 4, "", ""),
                           (15, "45.4", "name 1", "Comment 1"),
                           (16, 34.3, "", ""),
                           (17, 234, "", "Comment 2"),
                           (5, "34.4", "", "Comment 3"))
        }

        well = Well(self.well["well_name"], self.well["short_name"], self.well["depth"], self.well["reference"],
                    self.well["east"], self.well["north"], self.well["altitude"], self.session, "", "")
        well.save_to_db()

        logging = WellLog("logging", "unit name", self.session, "", "")
        logging.save_to_db()

        well.add_log(logging)

        for value in self.well["log_values"]:
            logging.insert_log_value(WellLogValue(value[0], value[1], self.session, value[2], value[3]))
コード例 #2
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()
コード例 #3
0
    def setUp(self):
        # type: () -> None
        """
        Initialise a temporary database connection for all test cases and fill the database with test data

        :return: None
        """
        # initialise a in-memory sqlite database
        self.handler = DBHandler(connection="sqlite://", echo=False)
        self.session = self.handler.get_session()

        # add test data to the database
        self.wells = [
            {
                "name": "Well_1",
                "short_name": "W1",
                "comment": "A drilled well",
                "east": 1234.56,
                "north": 123.45,
                "altitude": 10.5,
                "depth": 555,
                "marker": ((10, "mu", 4, ""),
                           (15, "so", 3, "Comment 1"),
                           (16, "sm", 2, ""),
                           (17, "su", 1, "Comment 2"),
                           (5, "mm", 5, "Comment 3"))
            }, {
                "name": "Well_2",
                "short_name": "W2",
                "comment": "",
                "east": 1000.23,
                "north": 2300.34,
                "altitude": 342.23,
                "depth": 341,
                "marker": ((12, "mo", 6, ""),
                           (120, "mm", 5, "Comment 1"),
                           (300, "Fault", 0, "Comment 2"),
                           (320, "mo", 6, ""))
            }, {
                "name": "Well_3",
                "short_name": "W3",
                "comment": "A third well",
                "east": 3454.34,
                "north": 2340.22,
                "altitude": 342.20,
                "depth": 645.21,
                "marker": ((34, "mu", 4, ""),
                           (234, "so", 3, "Comment 1"),
                           (345, "Fault", 0, "Comment 2"),
                           (635, "mu", 4, "Comment 3"))
            }
        ]

        for well in self.wells:
            new_well = Well(well["name"], well["short_name"], well["depth"], "spatial_reference_unknown", well["east"],
                            well["north"], well["altitude"], self.session, "well_set", well["comment"])
            new_well.save_to_db()

            for mark in well["marker"]:
                new_well.marker.append(
                    WellMarker(mark[0], StratigraphicObject.init_stratigraphy(self.session, mark[1], mark[2], False),
                               self.session, well["name"], mark[3]))
                new_well.marker[-1].save_to_db()