-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'maptool_filter_geometry' into 'main'
maptool_filter_geometry Closes #4 See merge request qgis/hvbg-filterplugin!9
- Loading branch information
Showing
4 changed files
with
94 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from qgis.gui import QgsMapTool, QgsRubberBand | ||
from qgis.core import QgsGeometry, QgsWkbTypes | ||
from qgis.utils import iface | ||
|
||
from PyQt5.QtCore import Qt, QPoint, pyqtSignal | ||
|
||
|
||
class PolygonTool(QgsMapTool): | ||
sketchFinished = pyqtSignal(QgsGeometry) | ||
|
||
def __init__(self): | ||
self.rubberBand = None | ||
self.canvas = iface.mapCanvas() | ||
super().__init__(self.canvas) | ||
|
||
def reset(self): | ||
if self.rubberBand: | ||
self.rubberBand.reset() | ||
self.canvas.scene().removeItem(self.rubberBand) | ||
self.rubberBand = None | ||
|
||
def canvasPressEvent(self, event): | ||
pass | ||
|
||
def canvasReleaseEvent(self, event): | ||
x = event.pos().x() | ||
y = event.pos().y() | ||
thisPoint = QPoint(x, y) | ||
|
||
mapToPixel = self.canvas.getCoordinateTransform() # QgsMapToPixel instance | ||
|
||
if event.button() == Qt.LeftButton: | ||
if not self.rubberBand: | ||
self.rubberBand = QgsRubberBand(self.canvas, geometryType=QgsWkbTypes.PolygonGeometry) | ||
self.rubberBand.setLineStyle(Qt.DashLine) | ||
self.rubberBand.setWidth(2) | ||
self.rubberBand.addPoint(mapToPixel.toMapCoordinates(thisPoint)) | ||
|
||
elif event.button() == Qt.RightButton: | ||
if self.rubberBand and self.rubberBand.numberOfVertices() > 3: | ||
# Finish rubberband sketch | ||
self.rubberBand.removeLastPoint() | ||
geometry = self.rubberBand.asGeometry() | ||
self.sketchFinished.emit(geometry) | ||
self.reset() | ||
self.canvas.refresh() | ||
|
||
def canvasMoveEvent(self, event): | ||
if not self.rubberBand: | ||
return | ||
x = event.pos().x() | ||
y = event.pos().y() | ||
thisPoint = QPoint(x, y) | ||
mapToPixel = self.canvas.getCoordinateTransform() | ||
self.rubberBand.movePoint(self.rubberBand.numberOfVertices() - 1, mapToPixel.toMapCoordinates(thisPoint)) | ||
|
||
def deactivate(self): | ||
self.reset() | ||
QgsMapTool.deactivate(self) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters