Example #1
0
    def vertexSnapped(self,  snapResult):
        snappedVertex = snapResult[0][0]
        snappedFid = snapResult[2][0]
        layer = self.iface.activeLayer()
        feat = dtutils.dtGetFeatureForId(layer,  snappedFid)

        if feat != None:
            geom = feat.geometry()
            rings = dtutils.dtExtractRings(geom)
            thisRing = None

            for aRing in rings:
                for aPoint in dtutils.dtExtractPoints(aRing):
                    if aPoint.x() == snappedVertex.x() and aPoint.y() == snappedVertex.y():
                        thisRing = aRing
                        break

            if thisRing != None:
                defaultAttributeMap = dtutils.dtGetDefaultAttributeMap(layer)
                layer.beginEditCommand(QtCore.QCoreApplication.translate("editcommand", "Fill ring"))

                if self.iface.vectorLayerTools().addFeature(layer, defaultValues = defaultAttributeMap, defaultGeometry = thisRing):
                    layer.endEditCommand()
                    self.canvas.refresh()
                else:
                    layer.destroyEditCommand()

        self.tool.reset()
Example #2
0
    def process(self):
        layer = self.iface.activeLayer()
        layer.featureAdded.connect(self.featureAdded)
        numRingsFilled = 0
        aborted = False

        for featureToFill in layer.selectedFeatures():
            geom = featureToFill.geometry()
            rings = dtutils.dtExtractRings(geom)

            for aRing in rings:

                if numRingsFilled == 0:
                    defaultAttributeMap = dtutils.dtGetDefaultAttributeMap(layer)
                    layer.beginEditCommand(QtCore.QCoreApplication.translate("editcommand", "Fill rings"))

                    if self.iface.vectorLayerTools().addFeature(layer, defaultValues = defaultAttributeMap, defaultGeometry = aRing):
                        layer.featureAdded.disconnect(self.featureAdded)
                    else:
                        layer.featureAdded.disconnect(self.featureAdded)
                        layer.destroyEditCommand()
                        aborted = True
                        break
                else:
                    aFeat = dtutils.dtCopyFeature(layer,  srcFid = self.newFid)
                    aFeat.setGeometry(aRing)
                    layer.addFeature(aFeat)

                numRingsFilled += 1

                if aborted:
                    break

        layer.endEditCommand()
        self.canvas.refresh()
Example #3
0
    def vertexSnapped(self, snapResult):
        snappedVertex = snapResult[0][0]
        snappedFid = snapResult[2][0]
        layer = self.iface.activeLayer()
        feat = dtutils.dtGetFeatureForId(layer, snappedFid)

        if feat != None:
            geom = feat.geometry()
            rings = dtutils.dtExtractRings(geom)
            thisRing = None

            for aRing in rings:
                for aPoint in dtutils.dtExtractPoints(aRing):
                    if aPoint.x() == snappedVertex.x() and aPoint.y(
                    ) == snappedVertex.y():
                        thisRing = aRing
                        break

            if thisRing != None:
                newFeat = dtutils.dtCreateFeature(layer)

                if self.iface.openFeatureForm(layer, newFeat, True):
                    # let user edit attributes
                    layer.beginEditCommand(
                        QtCore.QCoreApplication.translate(
                            "editcommand", "Fill ring"))
                    newFeat.setGeometry(thisRing)
                    layer.addFeature(newFeat)
                    layer.endEditCommand()
                    self.canvas.refresh()

        self.tool.clear()
Example #4
0
    def process(self):
        layer = self.iface.activeLayer()
        newFeat = dtutils.dtCreateFeature(layer)
        numRingsFilled = 0

        if self.iface.openFeatureForm(layer, newFeat):
            for featureToFill in layer.selectedFeatures():
                geom = featureToFill.geometry()
                rings = dtutils.dtExtractRings(geom)

                for aRing in rings:

                    if numRingsFilled == 0:
                        layer.beginEditCommand(
                            QtCore.QCoreApplication.translate(
                                "editcommand", "Fill rings"))

                    aFeat = dtutils.dtCopyFeature(layer, newFeat)
                    aFeat.setGeometry(aRing)
                    #for i in range(layer.pendingFields().count()):
                    layer.addFeature(aFeat)
                    numRingsFilled += 1

            layer.endEditCommand()
            self.canvas.refresh()
        else:
            layer.destroyEditCommand()
Example #5
0
    def vertexSnapped(self,  snapResult):
        if not self.doIgnoreTool:
            snappedVertex = snapResult[0][0]
            snappedFid = snapResult[2][0]
            layer = self.iface.activeLayer()
            thisRing = None
            feat = dtutils.dtGetFeatureForId(layer,  snappedFid)

            if feat != None:
                geom = feat.geometry()
                rings = dtutils.dtExtractRings(geom)

                for aRing in rings:
                    for aPoint in dtutils.dtExtractPoints(aRing):
                        if aPoint.x() == snappedVertex.x() and aPoint.y() == snappedVertex.y():
                            thisRing = aRing
                            break

                if thisRing != None:
                    newFeat = dtutils.dtCreateFeature(layer)
                    layer.beginEditCommand(QtCore.QCoreApplication.translate("editcommand", "Fill ring"))

                    if self.iface.openFeatureForm(layer,  newFeat,  True):
                        # let user edit attributes
                        newFeat.setGeometry(thisRing)
                        layer.addFeature(newFeat)
                        layer.endEditCommand()
                        self.canvas.refresh()
                    else:
                        layer.destroyEditCommand()

        self.tool.clear()
Example #6
0
    def process(self):
        layer = self.iface.activeLayer()
        newFeat = dtutils.dtCreateFeature(layer)
        numRingsFilled = 0

        if self.iface.openFeatureForm(layer,  newFeat):
            for featureToFill in layer.selectedFeatures():
                geom = featureToFill.geometry()
                rings = dtutils.dtExtractRings(geom)

                for aRing in rings:

                    if numRingsFilled == 0:
                        layer.beginEditCommand(QtCore.QCoreApplication.translate("editcommand", "Fill rings"))

                    aFeat = dtutils.dtCopyFeature(layer,  newFeat)
                    aFeat.setGeometry(aRing)
                    #for i in range(layer.pendingFields().count()):
                    layer.addFeature(aFeat)
                    numRingsFilled += 1

            layer.endEditCommand()
            self.canvas.refresh()
        else:
            layer.destroyEditCommand()
Example #7
0
    def vertexSnapped(self, snapResult):
        snappedVertex = snapResult[0][0]
        snappedFid = snapResult[2][0]
        layer = self.iface.activeLayer()
        feat = dtutils.dtGetFeatureForId(layer, snappedFid)

        if feat != None:
            geom = feat.geometry()
            rings = dtutils.dtExtractRings(geom)
            thisRing = None

            for aRing in rings:
                for aPoint in dtutils.dtExtractPoints(aRing):
                    if aPoint.x() == snappedVertex.x() and aPoint.y(
                    ) == snappedVertex.y():
                        thisRing = aRing
                        break

            if thisRing != None:
                defaultAttributeMap = dtutils.dtGetDefaultAttributeMap(layer)
                layer.beginEditCommand(
                    QtCore.QCoreApplication.translate("editcommand",
                                                      "Fill ring"))

                if self.iface.vectorLayerTools().addFeature(
                        layer,
                        defaultValues=defaultAttributeMap,
                        defaultGeometry=thisRing):
                    layer.endEditCommand()
                    self.canvas.refresh()
                else:
                    layer.destroyEditCommand()

        self.tool.reset()
Example #8
0
    def deleteRings(self,  poly):
        outGeom = QgsGeometry.fromPolygon(poly)

        if len(poly) > 1:
            # we have rings
            rings = dtutils.dtExtractRings(outGeom)
            for aRing in rings:
                outGeom = outGeom.combine(aRing)

        return outGeom
Example #9
0
    def canvasReleaseEvent(self,event):
        #Get the click
        x = event.pos().x()
        y = event.pos().y()

        layer = self.canvas.currentLayer()
        visibleLayers = []

        if self.allLayers:
            legendIface = self.iface.legendInterface()

            for aLayer in legendIface.layers():
                if 0 == aLayer.type():
                    if legendIface.isLayerVisible(aLayer) and \
                            self.isPolygonLayer(aLayer):
                        visibleLayers.append(aLayer)
        else:
            if layer <> None:
                visibleLayers.append(layer)

        if len(visibleLayers) > 0:
            #the clicked point is our starting point
            startingPoint = QtCore.QPoint(x,y)
            mapToPixel = self.canvas.getCoordinateTransform()
            thisQgsPoint = mapToPixel.toMapCoordinates(startingPoint)
            multiGeom = None

            for aLayer in visibleLayers:
                if not self.allLayers and aLayer.selectedFeatureCount() > 0:
                    #we assume, that the gap is between the selected polyons
                    hadSelection = True
                else:
                    hadSelection = False
                    spatialIndex = dtutils.dtSpatialindex(aLayer)
                    # get the 100 closest Features
                    featureIds = spatialIndex.nearestNeighbor(thisQgsPoint, 100)
                    aLayer.setSelectedFeatures(featureIds)

                multiGeom = dtutils.dtCombineSelectedPolygons(aLayer, self.iface, multiGeom)

                if self.allLayers or not hadSelection:
                    aLayer.removeSelection()

                if multiGeom == None:
                    return None

            if multiGeom != None:
                rings = dtutils.dtExtractRings(multiGeom)

                if len(rings) > 0:
                    for aRing in rings:
                        if aRing.contains(thisQgsPoint):
                            self.gapSelected.emit([aRing])
                            break
Example #10
0
    def process(self):
        # DtDualTool makes sure a selection exists
        layer = self.iface.activeLayer()
        multiGeom = dtutils.dtCombineSelectedPolygons(layer, self.iface)

        if multiGeom != None:
            rings = dtutils.dtExtractRings(multiGeom)

            if len(rings) == 0:
                self.iface.messageBar().pushWarning(
                    self.title,
                    QtCore.QCoreApplication.translate(
                        "digitizingtools",
                        "There are no gaps between the polygons."))
            else:
                defaultAttributeMap = dtutils.dtGetDefaultAttributeMap(layer)
                layer.featureAdded.connect(self.featureAdded)
                numRingsFilled = 0
                aborted = False

                for aRing in rings:
                    if numRingsFilled == 0:
                        layer.beginEditCommand(
                            QtCore.QCoreApplication.translate(
                                "editcommand", "Fill gaps"))

                        if self.iface.vectorLayerTools().addFeature(
                                layer,
                                defaultValues=defaultAttributeMap,
                                defaultGeometry=aRing):
                            layer.featureAdded.disconnect(self.featureAdded)
                        else:
                            layer.featureAdded.disconnect(self.featureAdded)
                            aborted = True
                            break
                    else:
                        aFeat = dtutils.dtCopyFeature(layer,
                                                      srcFid=self.newFid)
                        aFeat.setGeometry(aRing)
                        layer.addFeature(aFeat)

                    numRingsFilled += 1

                if aborted:
                    layer.destroyEditCommand()
                else:
                    layer.endEditCommand()

            self.canvas.refresh()
Example #11
0
    def process(self):
        layer = self.iface.activeLayer()
        layer.featureAdded.connect(self.featureAdded)
        numRingsFilled = 0
        aborted = False

        for featureToFill in layer.selectedFeatures():
            geom = QgsGeometry(featureToFill.geometry())

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

            rings = dtutils.dtExtractRings(geom)

            for aRing in rings:

                if numRingsFilled == 0:
                    defaultAttributeMap = dtutils.dtGetDefaultAttributeMap(
                        layer)
                    layer.beginEditCommand(
                        QtCore.QCoreApplication.translate(
                            "editcommand", "Fill rings"))

                    if self.iface.vectorLayerTools().addFeature(
                            layer,
                            defaultValues=defaultAttributeMap,
                            defaultGeometry=aRing):
                        layer.featureAdded.disconnect(self.featureAdded)
                    else:
                        layer.featureAdded.disconnect(self.featureAdded)
                        layer.destroyEditCommand()
                        aborted = True
                        break
                else:
                    aFeat = dtutils.dtCopyFeature(layer, srcFid=self.newFid)
                    aFeat.setGeometry(aRing)
                    layer.addFeature(aFeat)

                numRingsFilled += 1

                if aborted:
                    break

        layer.endEditCommand()
        self.canvas.refresh()
Example #12
0
    def process(self):
        # DtDualTool makes sure a selection exists
        layer = self.iface.activeLayer()
        multiGeom = dtutils.dtCombineSelectedPolygons(layer, self.iface)

        if multiGeom != None:
            rings = dtutils.dtExtractRings(multiGeom)

            if len(rings) == 0:
                self.iface.messageBar().pushMessage(self.title,
                    QtCore.QCoreApplication.translate("digitizingtools",
                    "There are no gaps between the polygons."),
                    level=QgsMessageBar.WARNING, duration = 10)
            else:
                defaultAttributeMap = dtutils.dtGetDefaultAttributeMap(layer)
                layer.featureAdded.connect(self.featureAdded)
                numRingsFilled = 0
                aborted = False

                for aRing in rings:
                    if numRingsFilled == 0:
                        layer.beginEditCommand(QtCore.QCoreApplication.translate(
                            "editcommand", "Fill gaps"))

                        if self.iface.vectorLayerTools().addFeature(
                                layer, defaultValues = defaultAttributeMap, defaultGeometry = aRing):
                            layer.featureAdded.disconnect(self.featureAdded)
                        else:
                            layer.featureAdded.disconnect(self.featureAdded)
                            aborted = True
                            break
                    else:
                        aFeat = dtutils.dtCopyFeature(layer, srcFid = self.newFid)
                        aFeat.setGeometry(aRing)
                        layer.addFeature(aFeat)

                    numRingsFilled += 1

                if aborted:
                    layer.destroyEditCommand()
                else:
                    layer.endEditCommand()

            self.canvas.refresh()
Example #13
0
    def fillRings(self,  forFids):
        layer = self.iface.activeLayer()
        newFeat = dtutils.dtCreateFeature(layer)
        layer.beginEditCommand(QtCore.QCoreApplication.translate("editcommand", "Fill rings"))

        if self.iface.openFeatureForm(layer,  newFeat):
            for fid in forFids:
                featureToFill = dtutils.dtGetFeatureForId(layer,  fid)

                if featureToFill != None:
                    geom = featureToFill.geometry()
                    rings = dtutils.dtExtractRings(geom)

                    for aRing in rings:
                        aFeat = dtutils.dtCopyFeature(layer,  newFeat)
                        aFeat.setGeometry(aRing)
                        #for i in range(layer.pendingFields().count()):

                        layer.addFeature(aFeat)

            layer.endEditCommand()
            self.canvas.refresh()
        else:
            layer.destroyEditCommand()
Example #14
0
    def fillGaps(self, snappedVertex = None):
        layer = self.iface.activeLayer()
        if layer.selectedFeatureCount() == 0:
            layer.invertSelection()

        multiGeom = None

        for aFeat in layer.selectedFeatures():
            aGeom = aFeat.geometry()

            if not aGeom.isGeosValid():
                QtGui.QMessageBox.warning(None,  self.title,  dtutils.dtGetInvalidGeomWarning())
                return None

            # fill rings contained in the polygon
            if aGeom.isMultipart():
                tempGeom = None

                for poly in aGeom.asMultiPolygon():
                    noRingGeom = self.deleteRings(poly)

                    if tempGeom == None:
                        tempGeom = noRingGeom
                    else:
                        tempGeom = tempGeom.combine(noRingGeom)
            else:
                tempGeom = self.deleteRings(aGeom.asPolygon())

            # make a large polygon from all selected
            if multiGeom == None:
                multiGeom = tempGeom
            else:
                multiGeom = multiGeom.combine(tempGeom)

        rings = dtutils.dtExtractRings(multiGeom)

        if len(rings) == 0:
            QtGui.QMessageBox.warning(None,  self.title,  QtCore.QCoreApplication.translate("digitizingtools",
                "There are no gaps between the polygons.") )
        else:
            if snappedVertex != None:
                thisRing = None

                for aRing in rings:
                    for aPoint in dtutils.dtExtractPoints(aRing):
                        if aPoint.x() == snappedVertex.x() and aPoint.y() == snappedVertex.y():
                            thisRing = aRing
                            break

                if thisRing != None:
                    newFeat = dtutils.dtCreateFeature(layer)
                    layer.beginEditCommand(QtCore.QCoreApplication.translate("editcommand", "Fill gap"))

                    if self.iface.openFeatureForm(layer,  newFeat,  True):
                        newFeat.setGeometry(thisRing)
                        layer.addFeature(newFeat)
                        layer.endEditCommand()

                    else:
                        layer.destroyEditCommand()
                else:
                    QtGui.QMessageBox.warning(None,  self.title,  QtCore.QCoreApplication.translate("digitizingtools",
                        "The selected gap is not closed.") )
            else:
                newFeat = dtutils.dtCreateFeature(layer)
                layer.beginEditCommand(QtCore.QCoreApplication.translate("editcommand", "Fill gaps"))

                if self.iface.openFeatureForm(layer,  newFeat):
                    for aRing in rings:
                        aFeat = dtutils.dtCopyFeature(layer,  newFeat)
                        aFeat.setGeometry(aRing)
                        layer.addFeature(aFeat)

                    layer.endEditCommand()
            self.canvas.refresh()
Example #15
0
    def fillGaps(self, snappedVertex = None):
        title = QtCore.QCoreApplication.translate("digitizingtools", "Fill gap")
        layer = self.iface.activeLayer()
        hasNoSelection = (layer.selectedFeatureCount() == 0)

        if hasNoSelection:
            layer.invertSelection()

        multiGeom = None

        for aFeat in layer.selectedFeatures():
            aGeom = aFeat.geometry()

            if not aGeom.isGeosValid():
                self.iface.messageBar().pushMessage(title,  dtutils.dtGetInvalidGeomWarning(layer), level=QgsMessageBar.CRITICAL)
                return None

            # fill rings contained in the polygon
            if aGeom.isMultipart():
                tempGeom = None

                for poly in aGeom.asMultiPolygon():
                    noRingGeom = dtutils.dtDeleteRings(poly)

                    if tempGeom == None:
                        tempGeom = noRingGeom
                    else:
                        tempGeom = tempGeom.combine(noRingGeom)
            else:
                tempGeom = dtutils.dtDeleteRings(aGeom.asPolygon())

            # make a large polygon from all selected
            if multiGeom == None:
                multiGeom = tempGeom
            else:
                multiGeom = multiGeom.combine(tempGeom)

        rings = dtutils.dtExtractRings(multiGeom)

        if len(rings) == 0:
            self.iface.messageBar().pushMessage(title,  QtCore.QCoreApplication.translate("digitizingtools",
                "There are no gaps between the polygons."), level=QgsMessageBar.CRITICAL)
        else:
            defaultAttributeMap = dtutils.dtGetDefaultAttributeMap(layer)

            if snappedVertex != None:
                thisRing = None

                for aRing in rings:
                    for aPoint in dtutils.dtExtractPoints(aRing):
                        if aPoint.x() == snappedVertex.x() and aPoint.y() == snappedVertex.y():
                            thisRing = aRing
                            break

                if thisRing != None:
                    layer.beginEditCommand(QtCore.QCoreApplication.translate("editcommand", "Fill gap"))

                    if self.iface.vectorLayerTools().addFeature(layer, defaultValues = defaultAttributeMap, defaultGeometry = thisRing):
                        layer.endEditCommand()
                        self.canvas.refresh()
                    else:
                        layer.destroyEditCommand()
                else:
                    self.iface.messageBar().pushMessage(title,  QtCore.QCoreApplication.translate("digitizingtools",
                        "The selected gap is not closed."), level=QgsMessageBar.CRITICAL)
            else:
                layer.featureAdded.connect(self.featureAdded)
                numRingsFilled = 0
                aborted = False

                for aRing in rings:
                    if numRingsFilled == 0:
                        layer.beginEditCommand(QtCore.QCoreApplication.translate("editcommand", "Fill gaps"))

                        if self.iface.vectorLayerTools().addFeature(layer, defaultValues = defaultAttributeMap, defaultGeometry = aRing):
                            layer.featureAdded.disconnect(self.featureAdded)
                        else:
                            layer.featureAdded.disconnect(self.featureAdded)
                            aborted = True
                            break
                    else:
                        aFeat = dtutils.dtCopyFeature(layer,  srcFid = self.newFid)
                        aFeat.setGeometry(aRing)
                        layer.addFeature(aFeat)

                    numRingsFilled += 1

                if aborted:
                    layer.destroyEditCommand()
                else:
                    layer.endEditCommand()

            if hasNoSelection:
                layer.removeSelection()

            self.canvas.refresh()
Example #16
0
    def fillGaps(self, snappedVertex=None):
        title = QtCore.QCoreApplication.translate("digitizingtools",
                                                  "Fill gap")
        layer = self.iface.activeLayer()
        hasNoSelection = (layer.selectedFeatureCount() == 0)

        if hasNoSelection:
            layer.invertSelection()

        multiGeom = None

        for aFeat in layer.selectedFeatures():
            aGeom = aFeat.geometry()

            if not aGeom.isGeosValid():
                self.iface.messageBar().pushMessage(
                    title,
                    dtutils.dtGetInvalidGeomWarning(layer),
                    level=QgsMessageBar.CRITICAL)
                return None

            # fill rings contained in the polygon
            if aGeom.isMultipart():
                tempGeom = None

                for poly in aGeom.asMultiPolygon():
                    noRingGeom = dtutils.dtDeleteRings(poly)

                    if tempGeom == None:
                        tempGeom = noRingGeom
                    else:
                        tempGeom = tempGeom.combine(noRingGeom)
            else:
                tempGeom = dtutils.dtDeleteRings(aGeom.asPolygon())

            # make a large polygon from all selected
            if multiGeom == None:
                multiGeom = tempGeom
            else:
                multiGeom = multiGeom.combine(tempGeom)

        rings = dtutils.dtExtractRings(multiGeom)

        if len(rings) == 0:
            self.iface.messageBar().pushMessage(
                title,
                QtCore.QCoreApplication.translate(
                    "digitizingtools",
                    "There are no gaps between the polygons."),
                level=QgsMessageBar.CRITICAL)
        else:
            defaultAttributeMap = dtutils.dtGetDefaultAttributeMap(layer)

            if snappedVertex != None:
                thisRing = None

                for aRing in rings:
                    for aPoint in dtutils.dtExtractPoints(aRing):
                        if aPoint.x() == snappedVertex.x() and aPoint.y(
                        ) == snappedVertex.y():
                            thisRing = aRing
                            break

                if thisRing != None:
                    layer.beginEditCommand(
                        QtCore.QCoreApplication.translate(
                            "editcommand", "Fill gap"))

                    if self.iface.vectorLayerTools().addFeature(
                            layer,
                            defaultValues=defaultAttributeMap,
                            defaultGeometry=thisRing):
                        layer.endEditCommand()
                        self.canvas.refresh()
                    else:
                        layer.destroyEditCommand()
                else:
                    self.iface.messageBar().pushMessage(
                        title,
                        QtCore.QCoreApplication.translate(
                            "digitizingtools",
                            "The selected gap is not closed."),
                        level=QgsMessageBar.CRITICAL)
            else:
                layer.featureAdded.connect(self.featureAdded)
                numRingsFilled = 0
                aborted = False

                for aRing in rings:
                    if numRingsFilled == 0:
                        layer.beginEditCommand(
                            QtCore.QCoreApplication.translate(
                                "editcommand", "Fill gaps"))

                        if self.iface.vectorLayerTools().addFeature(
                                layer,
                                defaultValues=defaultAttributeMap,
                                defaultGeometry=aRing):
                            layer.featureAdded.disconnect(self.featureAdded)
                        else:
                            layer.featureAdded.disconnect(self.featureAdded)
                            aborted = True
                            break
                    else:
                        aFeat = dtutils.dtCopyFeature(layer,
                                                      srcFid=self.newFid)
                        aFeat.setGeometry(aRing)
                        layer.addFeature(aFeat)

                    numRingsFilled += 1

                if aborted:
                    layer.destroyEditCommand()
                else:
                    layer.endEditCommand()

            if hasNoSelection:
                layer.removeSelection()

            self.canvas.refresh()
Example #17
0
    def getFeatureForPoint(self, layer, startingPoint, inRing = False):
        '''
        return the feature this QPoint is in (polygon layer)
        or this QPoint snaps to (point or line layer)
        '''
        result = []

        if self.isPolygonLayer(layer):
            mapToPixel = self.canvas.getCoordinateTransform()
            #thisQgsPoint = mapToPixel.toMapCoordinates(startingPoint)
            thisQgsPoint = self.transformed(layer, mapToPixel.toMapCoordinates(startingPoint))
            spatialIndex = dtutils.dtSpatialindex(layer)
            featureIds = spatialIndex.nearestNeighbor(thisQgsPoint, 0)
            # if we use 0 as neighborCount then only features that contain the point
            # are included

            for fid in featureIds:
                feat = dtutils.dtGetFeatureForId(layer, fid)

                if feat != None:
                    geom = QgsGeometry(feat.geometry())

                    if geom.contains(thisQgsPoint):
                        result.append(feat)
                        result.append([])
                        return result
                        break
                    else:
                        if inRing:
                            rings = dtutils.dtExtractRings(geom)

                            if len(rings) > 0:
                                for aRing in rings:
                                    if aRing.contains(thisQgsPoint):
                                        result.append(feat)
                                        result.append([])
                                        result.append(aRing)
                                        return result
                                        break
        else:
            #we need a snapper, so we use the MapCanvas snapper
            snapper = self.canvas.snappingUtils()
            snapper.setCurrentLayer(layer)
            # snapType = 0: no snap, 1 = vertex, 2 vertex & segment, 3 = segment
            snapMatch = snapper.snapToCurrentLayer(startingPoint, QgsPointLocator.All)

            if not snapMatch.isValid():
                dtutils.showSnapSettingsWarning(self.iface)
            else:
                feat = dtutils.dtGetFeatureForId(layer, snapMatch.featureId())

                if feat != None:
                    result.append(feat)

                    if snapMatch.hasVertex():
                        result.append([snapMatch.point(), None])

                    if snapMatch.hasEdge():
                        result.append(snapMatch.edgePoints())

                    return result

        return result
Example #18
0
    def fillGaps(self, snappedVertex=None):
        title = QtCore.QCoreApplication.translate("digitizingtools",
                                                  "Fill gap")
        layer = self.iface.activeLayer()
        hasNoSelection = (layer.selectedFeatureCount() == 0)

        if hasNoSelection:
            layer.invertSelection()

        multiGeom = None

        for aFeat in layer.selectedFeatures():
            aGeom = aFeat.geometry()

            if not aGeom.isGeosValid():
                self.iface.messageBar().pushMessage(
                    title,
                    dtutils.dtGetInvalidGeomWarning(layer),
                    level=QgsMessageBar.CRITICAL)
                return None

            # fill rings contained in the polygon
            if aGeom.isMultipart():
                tempGeom = None

                for poly in aGeom.asMultiPolygon():
                    noRingGeom = dtutils.dtDeleteRings(poly)

                    if tempGeom == None:
                        tempGeom = noRingGeom
                    else:
                        tempGeom = tempGeom.combine(noRingGeom)
            else:
                tempGeom = dtutils.dtDeleteRings(aGeom.asPolygon())

            # make a large polygon from all selected
            if multiGeom == None:
                multiGeom = tempGeom
            else:
                multiGeom = multiGeom.combine(tempGeom)

        rings = dtutils.dtExtractRings(multiGeom)

        if len(rings) == 0:
            self.iface.messageBar().pushMessage(
                title,
                QtCore.QCoreApplication.translate(
                    "digitizingtools",
                    "There are no gaps between the polygons."),
                level=QgsMessageBar.CRITICAL)
        else:
            if snappedVertex != None:
                thisRing = None

                for aRing in rings:
                    for aPoint in dtutils.dtExtractPoints(aRing):
                        if aPoint.x() == snappedVertex.x() and aPoint.y(
                        ) == snappedVertex.y():
                            thisRing = aRing
                            break

                if thisRing != None:
                    newFeat = dtutils.dtCreateFeature(layer)

                    if self.iface.openFeatureForm(layer, newFeat, True):
                        layer.beginEditCommand(
                            QtCore.QCoreApplication.translate(
                                "editcommand", "Fill gap"))
                        newFeat.setGeometry(thisRing)
                        layer.addFeature(newFeat)
                        layer.endEditCommand()
                else:
                    self.iface.messageBar().pushMessage(
                        title,
                        QtCore.QCoreApplication.translate(
                            "digitizingtools",
                            "The selected gap is not closed."),
                        level=QgsMessageBar.CRITICAL)
            else:
                newFeat = dtutils.dtCreateFeature(layer)

                if self.iface.openFeatureForm(layer, newFeat):
                    layer.beginEditCommand(
                        QtCore.QCoreApplication.translate(
                            "editcommand", "Fill gaps"))

                    for aRing in rings:
                        aFeat = dtutils.dtCopyFeature(layer, newFeat)
                        aFeat.setGeometry(aRing)
                        layer.addFeature(aFeat)

                    layer.endEditCommand()

            if hasNoSelection:
                layer.removeSelection()

            self.canvas.refresh()