Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Feature: Rotation by Selecting North #47

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions freehandrastergeoreferencer.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
MoveRasterMapTool,
RotateRasterMapTool,
ScaleRasterMapTool,
SelectNorthMapTool,
)
from .freehandrastergeoreferencerdialog import FreehandRasterGeoreferencerDialog

Expand Down Expand Up @@ -78,6 +79,15 @@ def initGui(self):
self.actionRotateRaster.triggered.connect(self.rotateRaster)
self.actionRotateRaster.setCheckable(True)

self.actionSelectNorth = QAction(
QIcon(":/plugins/freehandrastergeoreferencer/iconSelectNorth.png"),
"Select North",
self.iface.mainWindow())
self.actionSelectNorth.setObjectName(
"FreehandRasterGeoreferencingLayerPlugin_SelectNorth")
self.actionSelectNorth.triggered.connect(self.selectNorth)
self.actionSelectNorth.setCheckable(True)

self.actionScaleRaster = QAction(
QIcon(":/plugins/freehandrastergeoreferencer/iconScale.png"),
"Scale raster",
Expand Down Expand Up @@ -170,6 +180,7 @@ def initGui(self):
self.toolbar.addAction(self.actionAddLayer)
self.toolbar.addAction(self.actionMoveRaster)
self.toolbar.addAction(self.actionRotateRaster)
self.toolbar.addAction(self.actionSelectNorth)
self.toolbar.addWidget(self.spinBoxRotate)
self.toolbar.addAction(self.actionScaleRaster)
self.toolbar.addAction(self.actionAdjustRaster)
Expand All @@ -190,6 +201,8 @@ def initGui(self):
self.moveTool.setAction(self.actionMoveRaster)
self.rotateTool = RotateRasterMapTool(self.iface)
self.rotateTool.setAction(self.actionRotateRaster)
self.selectNorthTool = SelectNorthMapTool(self.iface)
self.selectNorthTool.setAction(self.actionSelectNorth)
self.scaleTool = ScaleRasterMapTool(self.iface)
self.scaleTool.setAction(self.actionScaleRaster)
self.adjustTool = AdjustRasterMapTool(self.iface)
Expand Down Expand Up @@ -236,6 +249,7 @@ def checkCurrentLayerIsPluginLayer(self):
):
self.actionMoveRaster.setEnabled(True)
self.actionRotateRaster.setEnabled(True)
self.actionSelectNorth.setEnabled(True)
self.actionScaleRaster.setEnabled(True)
self.actionAdjustRaster.setEnabled(True)
self.actionGeoref2PRaster.setEnabled(True)
Expand All @@ -261,6 +275,7 @@ def checkCurrentLayerIsPluginLayer(self):
else:
self.actionMoveRaster.setEnabled(False)
self.actionRotateRaster.setEnabled(False)
self.actionSelectNorth.setEnabled(False)
self.actionScaleRaster.setEnabled(False)
self.actionAdjustRaster.setEnabled(False)
self.actionGeoref2PRaster.setEnabled(False)
Expand Down Expand Up @@ -339,6 +354,9 @@ def moveRaster(self):
def rotateRaster(self):
self._toggleTool(self.rotateTool)

def selectNorth(self):
self._toggleTool(self.selectNorthTool)

def scaleRaster(self):
self._toggleTool(self.scaleTool)

Expand Down
87 changes: 87 additions & 0 deletions freehandrastergeoreferencer_maptools.py
Original file line number Diff line number Diff line change
Expand Up @@ -815,3 +815,90 @@ def _addDisplacementToPoint(self, rubberBand, point, doUpdate):
x = point.x() + self.endPoint.x() - self.startPoint.x()
y = point.y() + self.endPoint.y() - self.startPoint.y()
self.rubberBandExtent.addPoint(QgsPointXY(x, y), doUpdate)

class SelectNorthMapTool(QgsMapToolEmitPoint):
def __init__(self, iface):
self.iface = iface
self.canvas = iface.mapCanvas()
QgsMapToolEmitPoint.__init__(self, self.canvas)

self.rasterShadow = RasterShadowMapCanvasItem(self.canvas)

self.firstPoint = None
self.secondPoint = None

# Marker for the starting point
self.rubberBandOriginPoint = QgsRubberBand(self.canvas, QgsWkbTypes.PointGeometry)
self.rubberBandOriginPoint.setColor(Qt.red)
self.rubberBandOriginPoint.setIcon(QgsRubberBand.ICON_X)
self.rubberBandOriginPoint.setIconSize(7)
self.rubberBandOriginPoint.setWidth(2)

# Line for the North Line
self.rubberBandLine = QgsRubberBand(self.canvas, QgsWkbTypes.LineGeometry)
self.rubberBandLine.setColor(Qt.red)
self.rubberBandLine.setWidth(1)

self.isLayerVisible = True

self.reset()

def setLayer(self, layer):
self.layer = layer

def reset(self):
self.secondPoint = self.firstPoint = None
self.isEmittingPoint = False
self.rubberBandOriginPoint.reset(QgsWkbTypes.PointGeometry)
self.rubberBandLine.reset(QgsWkbTypes.LineGeometry)
self.layer = None

def deactivate(self):
QgsMapToolEmitPoint.deactivate(self)
self.reset()

def canvasPressEvent(self, e):
self.firstPoint = self.toMapCoordinates(e.pos())
self.isEmittingPoint = True

# Set the starting point marker to the first position and show
self.rubberBandOriginPoint.reset(QgsWkbTypes.PointGeometry)
self.rubberBandOriginPoint.addPoint(self.firstPoint, True)
self.rubberBandOriginPoint.show()

self.layer.history.append(
{
"action": "rotation",
"rotation": self.layer.rotation,
"center": self.layer.center,
})

def canvasReleaseEvent(self, e):
self.isEmittingPoint = False
self.secondPoint = self.toMapCoordinates(e.pos())

azimuth = -self.firstPoint.azimuth(self.secondPoint)
self.layer.setRotation(azimuth + self.layer.rotation)

self.layer.repaint()

self.layer.commitTransformParameters()

self.rubberBandLine.reset(QgsWkbTypes.LineGeometry)
self.rubberBandOriginPoint.reset(QgsWkbTypes.PointGeometry)
self.rasterShadow.reset()

self.firstPoint = self.secondPoint = None

def canvasMoveEvent(self, e):
if not self.isEmittingPoint:
return

self.secondPoint = self.toMapCoordinates(e.pos())
self.showDisplacement(self.firstPoint, self.secondPoint)

def showDisplacement(self, startPoint, endPoint):
self.rubberBandLine.reset(QgsWkbTypes.LineGeometry)
self.rubberBandLine.addPoint(startPoint, False)
self.rubberBandLine.addPoint(endPoint, True) # true to update canvas
self.rubberBandLine.show()
2 changes: 1 addition & 1 deletion gen_icons.bat
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ convert -size 24x24 xc:purple -gravity Center \( -density 288 -size 96x96 xc:non
convert -size 24x24 xc:yellow -gravity Center \( -density 288 -size 96x96 xc:none -fill 'green' -font "Consolas" -pointsize 14 -annotate 0 'SC' -resize 25% -fuzz 10% -trim \) -composite +repage iconScale.png
convert -size 24x24 xc:white -gravity Center \( -density 288 -size 96x96 xc:none -fill 'black' -font "Consolas" -pointsize 14 -annotate 0 'T-' -resize 25% -fuzz 10% -trim \) -composite +repage iconTransparencyDecrease.png
convert -size 24x24 xc:black -gravity Center \( -density 288 -size 96x96 xc:none -fill 'white' -font "Consolas" -pointsize 14 -annotate 0 'T+' -resize 25% -fuzz 10% -trim \) -composite +repage iconTransparencyIncrease.png

convert -size 24x24 xc:orange -gravity Center \( -density 288 -size 96x96 xc:none -fill 'purple' -font "Consolas" -pointsize 14 -annotate 0 'N' -resize 25% -fuzz 10% -trim \) -composite +repage iconSelectNorth.png

Binary file added iconSelectNorth.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions resources.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<file>iconAdd.png</file>
<file>iconMove.png</file>
<file>iconRotate.png</file>
<file>iconSelectNorth.png</file>
<file>iconScale.png</file>
<file>iconAdjust.png</file>
<file>icon2Points.png</file>
Expand Down