Exemplo n.º 1
0
 def affine(self):
     warn = QgsMessageViewer()
     layer_name = self.ui.comboBoxLayer.currentText()
     vlayer = self.project.mapLayers().get(layer_name)
     if vlayer is None:
         warn.setMessageAsPlainText("Select a layer to transform")
         warn.showMessage()
         return
     if not vlayer.isEditable():
         warn.setMessageAsPlainText("Layer not in edit mode")
         warn.showMessage()
         return
     if self.ui.radioButtonWholeLayer.isChecked():
         vlayer.selectAll()
     if vlayer.geometryType() == QgsWkbTypes.PolygonGeometry:
         start = 1
     else:
         start = 0
     v = self.affine_namespace
     for fid in vlayer.selectedFeatureIds():
         feature = vlayer.getFeature(fid)
         geometry = feature.geometry()
         for i in itertools.count(start=start):
             vertex = geometry.vertexAt(i)
             if vertex == QgsPoint(0, 0):
                 break
             # matrix form: x' = A x + b
             # x' = a x + b y + tx
             # y' = c x + d y + ty
             newx = v.a * vertex.x() + v.b * vertex.y() + v.tx
             newy = v.c * vertex.x() + v.d * vertex.y() + v.ty
             vlayer.moveVertex(newx, newy, fid, i)
     if self.ui.checkBoxZoomToLayer.isChecked():
         self.iface.mapCanvas().zoomToSelected()
Exemplo n.º 2
0
 def invert(self):
     # matrix form: x' = A x + b
     # --> x = A^-1 x' - A^-1 b
     # A^-1 = [d -b; -c a] / det A
     # only valid if det A = a d - b c != 0
     v = self.affine_namespace
     det = v.a * v.d - v.b * v.c
     if det == 0:
         warn = QgsMessageViewer()
         warn.setMessageAsPlainText("Transformation is not invertable")
         warn.showMessage()
         return
     v.a, v.b, v.c, v.d = v.d / det, -v.b / det, -v.c / det, v.a / det
     v.tx, v.ty = -v.a * v.tx - v.b * v.ty, -v.c * v.tx - v.d * v.ty
Exemplo n.º 3
0
    def accept(self):
        if self.mode == self.ASK_FOR_INPUT_MODE:
            # create the input layer (if not already done) and
            # update available options
            self.reloadInputLayer()

        # sanity checks
        if self.inLayer is None:
            QMessageBox.information(
                self, self.tr("Import to database"),
                self.tr("Input layer missing or not valid"))
            return

        if self.cboTable.currentText() == "":
            QMessageBox.information(self, self.tr("Import to database"),
                                    self.tr("Output table name is required"))
            return

        if self.chkSourceSrid.isEnabled() and self.chkSourceSrid.isChecked():
            try:
                sourceSrid = self.editSourceSrid.text()
            except ValueError:
                QMessageBox.information(
                    self, self.tr("Import to database"),
                    self.tr("Invalid source srid: must be an integer"))
                return

        if self.chkTargetSrid.isEnabled() and self.chkTargetSrid.isChecked():
            try:
                targetSrid = self.editTargetSrid.text()
            except ValueError:
                QMessageBox.information(
                    self, self.tr("Import to database"),
                    self.tr("Invalid target srid: must be an integer"))
                return

        # override cursor
        QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
        # store current input layer crs and encoding, so I can restore it
        prevInCrs = self.inLayer.crs()
        prevInEncoding = self.inLayer.dataProvider().encoding()

        try:
            schema = self.outUri.schema() if not self.cboSchema.isEnabled(
            ) else self.cboSchema.currentText()
            table = self.cboTable.currentText()

            # get pk and geom field names from the source layer or use the
            # ones defined by the user
            srcUri = QgsDataSourceURI(self.inLayer.source())

            pk = srcUri.keyColumn() if not self.chkPrimaryKey.isChecked(
            ) else self.editPrimaryKey.text()
            if not pk:
                pk = self.default_pk

            if self.inLayer.hasGeometryType() and self.chkGeomColumn.isEnabled(
            ):
                geom = srcUri.geometryColumn(
                ) if not self.chkGeomColumn.isChecked(
                ) else self.editGeomColumn.text()
                if not geom:
                    geom = self.default_geom
            else:
                geom = None

            options = {}
            if self.chkLowercaseFieldNames.isEnabled(
            ) and self.chkLowercaseFieldNames.isChecked():
                pk = pk.lower()
                if geom:
                    geom = geom.lower()
                options['lowercaseFieldNames'] = True

            # get output params, update output URI
            self.outUri.setDataSource(schema, table, geom, "", pk)
            typeName = self.db.dbplugin().typeName()
            providerName = self.db.dbplugin().providerName()
            if typeName == 'gpkg':
                uri = self.outUri.database()
                options['update'] = True
                options['driverName'] = 'GPKG'
                options['layerName'] = table
            else:
                uri = self.outUri.uri(False)

            if self.chkDropTable.isChecked():
                options['overwrite'] = True

            if self.chkSinglePart.isEnabled() and self.chkSinglePart.isChecked(
            ):
                options['forceSinglePartGeometryType'] = True

            outCrs = None
            if self.chkTargetSrid.isEnabled() and self.chkTargetSrid.isChecked(
            ):
                targetSrid = int(self.editTargetSrid.text())
                outCrs = QgsCoordinateReferenceSystem(targetSrid)

            # update input layer crs and encoding
            if self.chkSourceSrid.isEnabled() and self.chkSourceSrid.isChecked(
            ):
                sourceSrid = int(self.editSourceSrid.text())
                inCrs = QgsCoordinateReferenceSystem(sourceSrid)
                self.inLayer.setCrs(inCrs)

            if self.chkEncoding.isEnabled() and self.chkEncoding.isChecked():
                enc = self.cboEncoding.currentText()
                self.inLayer.setProviderEncoding(enc)

            onlySelected = self.chkSelectedFeatures.isChecked()

            # do the import!
            ret, errMsg = QgsVectorLayerImport.importLayer(
                self.inLayer, uri, providerName, outCrs, onlySelected, False,
                options)
        except Exception as e:
            ret = -1
            errMsg = unicode(e)

        finally:
            # restore input layer crs and encoding
            self.inLayer.setCrs(prevInCrs)
            self.inLayer.setProviderEncoding(prevInEncoding)
            # restore cursor
            QApplication.restoreOverrideCursor()

        if ret != 0:
            output = QgsMessageViewer()
            output.setTitle(self.tr("Import to database"))
            output.setMessageAsPlainText(
                self.tr("Error %d\n%s") % (ret, errMsg))
            output.showMessage()
            return

        # create spatial index
        if self.chkSpatialIndex.isEnabled() and self.chkSpatialIndex.isChecked(
        ):
            self.db.connector.createSpatialIndex((schema, table), geom)

        self.db.connection().reconnect()
        self.db.refresh()
        QMessageBox.information(self, self.tr("Import to database"),
                                self.tr("Import was successful."))
        return QDialog.accept(self)
Exemplo n.º 4
0
    def accept(self):
        if self.mode == self.ASK_FOR_INPUT_MODE:
            # create the input layer (if not already done) and
            # update available options
            self.reloadInputLayer()

        # sanity checks
        if self.inLayer is None:
            QMessageBox.information(self, self.tr("Import to database"), self.tr("Input layer missing or not valid"))
            return

        if self.cboTable.currentText() == "":
            QMessageBox.information(self, self.tr("Import to database"), self.tr("Output table name is required"))
            return

        if self.chkSourceSrid.isEnabled() and self.chkSourceSrid.isChecked():
            try:
                sourceSrid = self.editSourceSrid.text()
            except ValueError:
                QMessageBox.information(self, self.tr("Import to database"),
                                        self.tr("Invalid source srid: must be an integer"))
                return

        if self.chkTargetSrid.isEnabled() and self.chkTargetSrid.isChecked():
            try:
                targetSrid = self.editTargetSrid.text()
            except ValueError:
                QMessageBox.information(self, self.tr("Import to database"),
                                        self.tr("Invalid target srid: must be an integer"))
                return

        # override cursor
        QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
        # store current input layer crs and encoding, so I can restore it
        prevInCrs = self.inLayer.crs()
        prevInEncoding = self.inLayer.dataProvider().encoding()

        try:
            schema = self.outUri.schema() if not self.cboSchema.isEnabled() else self.cboSchema.currentText()
            table = self.cboTable.currentText()

            # get pk and geom field names from the source layer or use the
            # ones defined by the user
            srcUri = QgsDataSourceUri(self.inLayer.source())

            pk = srcUri.keyColumn() if not self.chkPrimaryKey.isChecked() else self.editPrimaryKey.text()
            if not pk:
                pk = self.default_pk

            if self.inLayer.hasGeometryType() and self.chkGeomColumn.isEnabled():
                geom = srcUri.geometryColumn() if not self.chkGeomColumn.isChecked() else self.editGeomColumn.text()
                if not geom:
                    geom = self.default_geom
            else:
                geom = None

            options = {}
            if self.chkLowercaseFieldNames.isEnabled() and self.chkLowercaseFieldNames.isChecked():
                pk = pk.lower()
                if geom:
                    geom = geom.lower()
                options['lowercaseFieldNames'] = True

            # get output params, update output URI
            self.outUri.setDataSource(schema, table, geom, "", pk)
            uri = self.outUri.uri(False)

            providerName = self.db.dbplugin().providerName()
            if self.chkDropTable.isChecked():
                options['overwrite'] = True

            if self.chkSinglePart.isEnabled() and self.chkSinglePart.isChecked():
                options['forceSinglePartGeometryType'] = True

            outCrs = QgsCoordinateReferenceSystem()
            if self.chkTargetSrid.isEnabled() and self.chkTargetSrid.isChecked():
                targetSrid = int(self.editTargetSrid.text())
                outCrs = QgsCoordinateReferenceSystem(targetSrid)

            # update input layer crs and encoding
            if self.chkSourceSrid.isEnabled() and self.chkSourceSrid.isChecked():
                sourceSrid = int(self.editSourceSrid.text())
                inCrs = QgsCoordinateReferenceSystem(sourceSrid)
                self.inLayer.setCrs(inCrs)

            if self.chkEncoding.isEnabled() and self.chkEncoding.isChecked():
                enc = self.cboEncoding.currentText()
                self.inLayer.setProviderEncoding(enc)

            onlySelected = self.chkSelectedFeatures.isChecked()

            # do the import!
            ret, errMsg = QgsVectorLayerImport.importLayer(self.inLayer, uri, providerName, outCrs, onlySelected, False, options)
        except Exception as e:
            ret = -1
            errMsg = str(e)

        finally:
            # restore input layer crs and encoding
            self.inLayer.setCrs(prevInCrs)
            self.inLayer.setProviderEncoding(prevInEncoding)
            # restore cursor
            QApplication.restoreOverrideCursor()

        if ret != 0:
            output = QgsMessageViewer()
            output.setTitle(self.tr("Import to database"))
            output.setMessageAsPlainText(self.tr("Error %d\n%s") % (ret, errMsg))
            output.showMessage()
            return

        # create spatial index
        if self.chkSpatialIndex.isEnabled() and self.chkSpatialIndex.isChecked():
            self.db.connector.createSpatialIndex((schema, table), geom)

        QMessageBox.information(self, self.tr("Import to database"), self.tr("Import was successful."))
        return QDialog.accept(self)
Exemplo n.º 5
0
    def accept(self):
        if self.mode == self.ASK_FOR_INPUT_MODE:
            # create the input layer (if not already done) and
            # update available options
            self.reloadInputLayer()

        # sanity checks
        if self.inLayer is None:
            QMessageBox.critical(self, self.tr("Import to Database"), self.tr("Input layer missing or not valid."))
            return

        if self.cboTable.currentText() == "":
            QMessageBox.critical(self, self.tr("Import to Database"), self.tr("Output table name is required."))
            return

        if self.chkSourceSrid.isEnabled() and self.chkSourceSrid.isChecked():
            if not self.widgetSourceSrid.crs().isValid():
                QMessageBox.critical(self, self.tr("Import to Database"),
                                     self.tr("Invalid source srid: must be a valid crs."))
                return

        if self.chkTargetSrid.isEnabled() and self.chkTargetSrid.isChecked():
            if not self.widgetTargetSrid.crs().isValid():
                QMessageBox.critical(self, self.tr("Import to Database"),
                                     self.tr("Invalid target srid: must be a valid crs."))
                return

        with OverrideCursor(Qt.WaitCursor):
            # store current input layer crs and encoding, so I can restore it
            prevInCrs = self.inLayer.crs()
            prevInEncoding = self.inLayer.dataProvider().encoding()

            try:
                schema = self.outUri.schema() if not self.cboSchema.isEnabled() else self.cboSchema.currentText()
                table = self.cboTable.currentText()

                # get pk and geom field names from the source layer or use the
                # ones defined by the user
                srcUri = QgsDataSourceUri(self.inLayer.source())

                pk = srcUri.keyColumn() if not self.chkPrimaryKey.isChecked() else self.editPrimaryKey.text()
                if not pk:
                    pk = self.default_pk

                if self.inLayer.isSpatial() and self.chkGeomColumn.isEnabled():
                    geom = srcUri.geometryColumn() if not self.chkGeomColumn.isChecked() else self.editGeomColumn.text()
                    if not geom:
                        geom = self.default_geom
                else:
                    geom = None

                options = {}
                if self.chkLowercaseFieldNames.isEnabled() and self.chkLowercaseFieldNames.isChecked():
                    pk = pk.lower()
                    if geom:
                        geom = geom.lower()
                    options['lowercaseFieldNames'] = True

                # get output params, update output URI
                self.outUri.setDataSource(schema, table, geom, "", pk)
                typeName = self.db.dbplugin().typeName()
                providerName = self.db.dbplugin().providerName()
                if typeName == 'gpkg':
                    uri = self.outUri.database()
                    options['update'] = True
                    options['driverName'] = 'GPKG'
                    options['layerName'] = table
                else:
                    uri = self.outUri.uri(False)

                if self.chkDropTable.isChecked():
                    options['overwrite'] = True

                if self.chkSinglePart.isEnabled() and self.chkSinglePart.isChecked():
                    options['forceSinglePartGeometryType'] = True

                outCrs = QgsCoordinateReferenceSystem()
                if self.chkTargetSrid.isEnabled() and self.chkTargetSrid.isChecked():
                    outCrs = self.widgetTargetSrid.crs()

                # update input layer crs and encoding
                if self.chkSourceSrid.isEnabled() and self.chkSourceSrid.isChecked():
                    inCrs = self.widgetSourceSrid.crs()
                    self.inLayer.setCrs(inCrs)

                if self.chkEncoding.isEnabled() and self.chkEncoding.isChecked():
                    enc = self.cboEncoding.currentText()
                    self.inLayer.setProviderEncoding(enc)

                onlySelected = self.chkSelectedFeatures.isChecked()

                # do the import!
                ret, errMsg = QgsVectorLayerExporter.exportLayer(self.inLayer, uri, providerName, outCrs, onlySelected, options)
            except Exception as e:
                ret = -1
                errMsg = str(e)

            finally:
                # restore input layer crs and encoding
                self.inLayer.setCrs(prevInCrs)
                self.inLayer.setProviderEncoding(prevInEncoding)

        if ret != 0:
            output = QgsMessageViewer()
            output.setTitle(self.tr("Import to Database"))
            output.setMessageAsPlainText(self.tr("Error {0}\n{1}").format(ret, errMsg))
            output.showMessage()
            return

        # create spatial index
        if self.chkSpatialIndex.isEnabled() and self.chkSpatialIndex.isChecked():
            self.db.connector.createSpatialIndex((schema, table), geom)

        # add comment on table
        supportCom = self.db.supportsComment()
        if self.chkCom.isEnabled() and self.chkCom.isChecked() and supportCom:
            # using connector executing COMMENT ON TABLE query (with editCome.text() value)
            com = self.editCom.text()
            self.db.connector.commentTable(schema, table, com)

        self.db.connection().reconnect()
        self.db.refresh()
        QMessageBox.information(self, self.tr("Import to Database"), self.tr("Import was successful."))
        return QDialog.accept(self)