Ejemplo n.º 1
0
    def process(self):
        '''Function that does all the real work'''
        title = QtCore.QCoreApplication.translate("digitizingtools",
                                                  "Splitter")
        splitterLayer = dtutils.dtChooseVectorLayer(
            self.iface,
            1,
            msg=QtCore.QCoreApplication.translate("digitizingtools",
                                                  "splitter layer"))

        if splitterLayer == None:
            self.iface.messageBar().pushMessage(
                title,
                QtCore.QCoreApplication.translate(
                    "digitizingtools",
                    "Please provide a line layer to split with."))
        else:
            passiveLayer = self.iface.activeLayer()
            msgLst = dtutils.dtGetNoSelMessage()
            noSelMsg1 = msgLst[0]

            if splitterLayer.selectedFeatureCount() == 0:
                self.iface.messageBar().pushMessage(
                    title, noSelMsg1 + " " + splitterLayer.name())
                return None
            elif splitterLayer.selectedFeatureCount() != 1:
                numSelSplitMsg = dtutils.dtGetManySelMessage(splitterLayer)
                self.iface.messageBar().pushMessage(title, numSelSplitMsg  + \
                    QtCore.QCoreApplication.translate("digitizingtools", " Please select only one feature to split with."))
            else:
                if passiveLayer.selectedFeatureCount() == 0:
                    self.iface.messageBar().pushMessage(title,  noSelMsg1  + " " + passiveLayer.name() + ".\n" + \
                        QtCore.QCoreApplication.translate("digitizingtools", " Please select the features to be splitted."))
                    return None

            # determine srs, we work in the project's srs
                splitterCRSSrsid = splitterLayer.crs().srsid()
                passiveCRSSrsid = passiveLayer.crs().srsid()
                mc = self.iface.mapCanvas()
                renderer = mc.mapRenderer()
                projectCRSSrsid = renderer.destinationCrs().srsid()
                passiveLayer.beginEditCommand(
                    QtCore.QCoreApplication.translate("editcommand",
                                                      "Split features"))
                featuresBeingSplit = 0
                featuresToAdd = []

                for feat in splitterLayer.selectedFeatures():
                    splitterGeom = feat.geometry()

                    if splitterCRSSrsid != projectCRSSrsid:
                        splitterGeom.transform(
                            QgsCoordinateTransform(splitterCRSSrsid,
                                                   projectCRSSrsid))

                    for selFeat in passiveLayer.selectedFeatures():
                        selGeom = selFeat.geometry()

                        if passiveCRSSrsid != projectCRSSrsid:
                            selGeom.transform(
                                QgsCoordinateTransform(passiveCRSSrsid,
                                                       projectCRSSrsid))

                        if splitterGeom.intersects(
                                selGeom):  # we have a candidate
                            splitterPList = dtutils.dtExtractPoints(
                                splitterGeom)

                            try:
                                result, newGeometries, topoTestPoints = selGeom.splitGeometry(
                                    splitterPList, False
                                )  #QgsProject.instance().topologicalEditing())
                            except:
                                self.iface.messageBar().pushMessage(
                                    title,
                                    dtutils.dtGetErrorMessage() +
                                    QtCore.QCoreApplication.translate(
                                        "digitizingtools",
                                        "splitting of feature") + " " +
                                    str(selFeat.id()),
                                    level=QgsMessageBar.CRITICAL)
                                return None

                            if result == 0:
                                selFeat.setGeometry(selGeom)
                                passiveLayer.updateFeature(selFeat)

                                if len(newGeometries) > 0:
                                    featuresBeingSplit += 1
                                    newFeatures = dtutils.dtMakeFeaturesFromGeometries(
                                        passiveLayer, selFeat, newGeometries)

                                    for newFeat in newFeatures:
                                        newGeom = newFeat.geometry()

                                        if passiveCRSSrsid != projectCRSSrsid:
                                            newGeom.transform(
                                                QgsCoordinateTransform(
                                                    projectCRSSrsid,
                                                    passiveCRSSrsid))
                                            newFeat.setGeometry(newGeom)

                                        featuresToAdd.append(newFeat)

                if featuresBeingSplit > 0:
                    passiveLayer.addFeatures(featuresToAdd, False)
                    passiveLayer.endEditCommand()
                    passiveLayer.removeSelection()
                else:
                    passiveLayer.destroyEditCommand()
Ejemplo n.º 2
0
    def process(self):
        '''Function that does all the real work'''
        title = QtCore.QCoreApplication.translate("digitizingtools", "Splitter")
        splitterLayer = dtutils.dtChooseVectorLayer(self.iface,  1,  msg = QtCore.QCoreApplication.translate("digitizingtools", "splitter layer"))

        if splitterLayer == None:
            self.iface.messageBar().pushMessage(title,  QtCore.QCoreApplication.translate("digitizingtools", "Please provide a line layer to split with."))
        else:
            passiveLayer = self.iface.activeLayer()
            msgLst = dtutils.dtGetNoSelMessage()
            noSelMsg1 = msgLst[0]

            if splitterLayer.selectedFeatureCount() == 0:
                self.iface.messageBar().pushMessage(title, noSelMsg1 + " " + splitterLayer.name())
                return None
            elif splitterLayer.selectedFeatureCount() != 1:
                numSelSplitMsg = dtutils.dtGetManySelMessage(splitterLayer)
                self.iface.messageBar().pushMessage(title, numSelSplitMsg  + \
                    QtCore.QCoreApplication.translate("digitizingtools", " Please select only one feature to split with."))
            else:
                if passiveLayer.selectedFeatureCount() == 0:
                    self.iface.messageBar().pushMessage(title,  noSelMsg1  + " " + passiveLayer.name() + ".\n" + \
                        QtCore.QCoreApplication.translate("digitizingtools", " Please select the features to be splitted."))
                    return None

               # determine srs, we work in the project's srs
                splitterCRSSrsid = splitterLayer.crs().srsid()
                passiveCRSSrsid = passiveLayer.crs().srsid()
                mc = self.iface.mapCanvas()
                renderer = mc.mapRenderer()
                projectCRSSrsid = renderer.destinationCrs().srsid()
                passiveLayer.beginEditCommand(QtCore.QCoreApplication.translate("editcommand", "Split features"))
                featuresBeingSplit = 0
                featuresToAdd = []

                for feat in splitterLayer.selectedFeatures():
                    splitterGeom = QgsGeometry(feat.geometry())

                    if not splitterGeom.isGeosValid():
                        thisWarning = dtutils.dtGetInvalidGeomWarning(splitterLayer)
                        dtutils.dtShowWarning(self.iface, thisWarning)
                        continue

                    if splitterCRSSrsid != projectCRSSrsid:
                        splitterGeom.transform(QgsCoordinateTransform(splitterCRSSrsid,  projectCRSSrsid))

                    for selFeat in passiveLayer.selectedFeatures():
                        selGeom = QgsGeometry(selFeat.geometry())

                        if not selGeom.isGeosValid():
                            thisWarning = dtutils.dtGetInvalidGeomWarning(passiveLayer)
                            dtutils.dtShowWarning(self.iface, thisWarning)
                            continue

                        if passiveCRSSrsid != projectCRSSrsid:
                            selGeom.transform(QgsCoordinateTransform(passiveCRSSrsid,  projectCRSSrsid))

                        if splitterGeom.intersects(selGeom): # we have a candidate
                            splitterPList = dtutils.dtExtractPoints(splitterGeom)

                            try:
                                result,  newGeometries,  topoTestPoints = selGeom.splitGeometry(splitterPList,  False) #QgsProject.instance().topologicalEditing())
                            except:
                                self.iface.messageBar().pushMessage(title,
                                    dtutils.dtGetErrorMessage() + QtCore.QCoreApplication.translate("digitizingtools", "splitting of feature") + " " + str(selFeat.id()),
                                    level=QgsMessageBar.CRITICAL)
                                return None

                            if result == 0:
                                selFeat.setGeometry(selGeom)
                                passiveLayer.updateFeature(selFeat)

                                if len(newGeometries) > 0:
                                    featuresBeingSplit += 1
                                    newFeatures = dtutils.dtMakeFeaturesFromGeometries(passiveLayer,  selFeat,  newGeometries)

                                    for newFeat in newFeatures:
                                        newGeom = QgsGeometry(newFeat.geometry())
                                        if passiveCRSSrsid != projectCRSSrsid:
                                            newGeom.transform(QgsCoordinateTransform( projectCRSSrsid,  passiveCRSSrsid))
                                            newFeat.setGeometry(newGeom)

                                        featuresToAdd.append(newFeat)

                if featuresBeingSplit > 0:
                    passiveLayer.addFeatures(featuresToAdd,  False)
                    passiveLayer.endEditCommand()
                    passiveLayer.removeSelection()
                else:
                    passiveLayer.destroyEditCommand()
Ejemplo n.º 3
0
    def process(self):
        '''Function that does all the real work'''
        title = QtCore.QCoreApplication.translate(
            "digitizingtools", "Clipper")

        clipperLayer = dtutils.dtChooseVectorLayer(
            self.iface, 2, msg = QtCore.QCoreApplication.translate(
                "digitizingtools", "clipper layer"
            ))

        if clipperLayer == None:
            self.iface.messageBar().pushMessage(
                title, QtCore.QCoreApplication.translate(
                    "digitizingtools", "Please provide a polygon layer to clip with."
                ))
        else:
            passiveLayer = self.iface.activeLayer()
            msgLst = dtutils.dtGetNoSelMessage()
            noSelMsg1 = msgLst[0]
            noSelMsg2 = msgLst[1]

            if clipperLayer.selectedFeatureCount() == 0:
                self.iface.messageBar().pushMessage(
                    title, noSelMsg1 + " " + clipperLayer.name())
                return None
            elif clipperLayer.selectedFeatureCount() != 1:
                numSelSplitMsg = dtutils.dtGetManySelMessage(clipperLayer)
                self.iface.messageBar().pushMessage(title, numSelSplitMsg  + \
                    QtCore.QCoreApplication.translate(
                        "digitizingtools",
                        " Please select only one feature to clip with."
                    ))
            else:
                if passiveLayer.selectedFeatureCount() == 0:
                    msgLst = dtutils.dtGetNoSelMessage()
                    noSelMsg1 = msgLst[0]
                    noSelMsg2 = msgLst[1]
                    reply = QtGui.QMessageBox.question(None,  title,
                        noSelMsg1 + " " + passiveLayer.name() + "\n" + noSelMsg2,
                        QtGui.QMessageBox.Yes | QtGui.QMessageBox.No )

                    if reply == QtGui.QMessageBox.Yes:
                        passiveLayer.invertSelection()
                    else:
                        return None

                idsToProcess = []

                for aFeat in passiveLayer.selectedFeatures():
                    idsToProcess.append(aFeat.id())

                if clipperLayer.selectedFeatureCount() > 0:
                   # determine srs, we work in the project's srs
                    clipperCRSSrsid = clipperLayer.crs().srsid()
                    passiveCRSSrsid = passiveLayer.crs().srsid()
                    mc = self.iface.mapCanvas()
                    renderer = mc.mapRenderer()
                    projectCRSSrsid = renderer.destinationCrs().srsid()
                    passiveLayer.beginEditCommand(
                        QtCore.QCoreApplication.translate(
                            "editcommand", "Clip Features"
                        ))
                    featuresBeingClipped = 0

                    for feat in clipperLayer.selectedFeatures():
                        clipperGeom = QgsGeometry(feat.geometry())

                        if not clipperGeom.isGeosValid():
                            thisWarning = dtutils.dtGetInvalidGeomWarning(clipperLayer)
                            dtutils.dtShowWarning(self.iface, thisWarning)
                            continue

                        if clipperCRSSrsid != projectCRSSrsid:
                            clipperGeom.transform(QgsCoordinateTransform(
                                clipperCRSSrsid, projectCRSSrsid
                            ))

                        bbox = clipperGeom.boundingBox()
                        passiveLayer.select(bbox, False) # make a new selection

                        for selFeat in passiveLayer.selectedFeatures():
                            if idsToProcess.count(selFeat.id()) == 0:
                                continue

                            selGeom = QgsGeometry(selFeat.geometry())

                            if not selGeom.isGeosValid():
                                thisWarning = dtutils.dtGetInvalidGeomWarning(passiveLayer)
                                dtutils.dtShowWarning(self.iface, thisWarning)
                                continue

                            if passiveCRSSrsid != projectCRSSrsid:
                                selGeom.transform(
                                    QgsCoordinateTransform(passiveCRSSrsid,
                                    projectCRSSrsid
                                ))

                            if clipperGeom.intersects(selGeom): # we have a candidate
                                newGeom = selGeom.intersection(clipperGeom)

                                if newGeom != None:
                                    if not newGeom.isGeosEmpty():
                                        if passiveCRSSrsid != projectCRSSrsid:
                                            newGeom.transform(QgsCoordinateTransform(
                                                projectCRSSrsid, passiveCRSSrsid))

                                        selFeat.setGeometry(newGeom)
                                        passiveLayer.updateFeature(selFeat)
                                        featuresBeingClipped += 1

                    if featuresBeingClipped == 0:
                        passiveLayer.destroyEditCommand()
                    else:
                        passiveLayer.endEditCommand()

                    passiveLayer.removeSelection()
                    self.iface.mapCanvas().refresh()