예제 #1
0
    def syntheticFeatureSelection(self, startPoint: QgsPointXY,
                                  endPoint: QgsPointXY,
                                  modifiers: Qt.KeyboardModifiers) -> None:
        if startPoint is None or endPoint is None:
            raise Exception(
                'Something went very bad, unable to create selection without start or end point'
            )

        assert self.simplifiedSegmentsLayer

        if self.selectionMode != SelectionModes.LINES:
            self.simplifiedSegmentsLayer.removeSelection()

        # check the Shift and Control modifiers to reproduce the navive selection
        if modifiers & Qt.ShiftModifier:
            selectBehaviour = QgsVectorLayer.AddToSelection
        elif modifiers & Qt.ControlModifier:
            selectBehaviour = QgsVectorLayer.RemoveFromSelection
        else:
            selectBehaviour = QgsVectorLayer.SetSelection

        if startPoint.x() == endPoint.x() and startPoint.y() == endPoint.y():
            tolerance = QgsTolerance.defaultTolerance(iface.activeLayer(),
                                                      QgsMapSettings())
            tolerance = tolerance * self.canvas.mapUnitsPerPixel()
            startPoint = QgsPointXY(startPoint.x() - tolerance,
                                    startPoint.y() - tolerance)
            endPoint = QgsPointXY(endPoint.x() + tolerance,
                                  endPoint.y() + tolerance)

        lines = None
        rect = QgsRectangle(startPoint, endPoint)

        if self.selectionMode == SelectionModes.ENCLOSING:
            lines = self.getLinesSelectionModeEnclosing(selectBehaviour, rect)
        elif self.selectionMode == SelectionModes.LINES:
            lines = self.getLinesSelectionModeLines(selectBehaviour, rect)
        elif self.selectionMode == SelectionModes.NODES:
            lines = self.getLinesSelectionModeVertices(selectBehaviour, rect)

            if lines is None:
                return
        else:
            raise Exception(
                'Wrong selection mode selected, should never be the case')

        if not len(lines):
            show_info(__('No results found!'))
            self.deleteAllCandidates()
            return

        if not self.addCandidates(lines):
            show_info(__('Unable to add candidates'))
            return
예제 #2
0
    def layer_tolerance(map_canvas, layer):
        """Functions finds out default tolerance from qgis settings.

        Required value (of qgis settings) is returned by QgsTolerance.defaultTolerance(...) calling.
        If returned value equals 0.0, we ignore it and calculate our own tolerance from current extent width.

        @return default tolerance
        """

        qgis_tolerance = QgsTolerance.defaultTolerance(
            layer, map_canvas.mapRenderer())

        if qgis_tolerance == 0.0:
            extent = map_canvas.extent()
            w = extent.xMaximum() - extent.xMinimum()
            return w / 220

        return qgis_tolerance