sourcepole / qgis-instantprint-plugin

QGIS Instant Print Plugin
GNU General Public License v2.0
10 stars 10 forks source link

map canvas rotation #24

Open XavierCalatrava opened 5 years ago

XavierCalatrava commented 5 years ago

Is it possible to respect the map rotation in canvas view when you print?

tveinot commented 4 years ago

I have been working on this, as well as asked about this very feature. So far I discovered that you can make a custom function for the Expression String Builder that will capture the Map Rotation and apply it to the Map Canvas of your Layout. In the Function Tab of the Expression String Builder enter the below Python code

from qgis.core import from qgis.gui import from qgis.utils import iface

@qgsfunction(args='auto', group='Custom') def CanvasRotation(feature, parent): return (iface.mapCanvas().rotation())

As you can see I named my function "CanvasRotation" it should probably be "MapRotation" you can change the name where it says "def CanvasRotation(feature, parent):" with that saved you can then go to the Expression tab and use CanvasRotation() to return the value from your map rotation.

Back to your Layout if you look to the left of the Map Rotation value box in the Map Properties you will see a button for Data Defined Override. Click that, got to edit and enter CanvasRotation() and save it. Now, your Layout will be rotated by the same rotation as your Canvas.

NOTES: There are some issues... Since the original tool was built to use QRectF the rectangle will not rotate with map properly; this is because of how QRectF works. I tried to use QgsGeometry, QgsTransform, and PyMath to alter the rubberband to appear much like the view port of the rotated layout but so far have been unsuccessful.

The other issue is once rotated when you try to move the box will move in relation to the rotation; meaning if you rotate the map 90 degrees and grab the map view box and drag the mouse left the box will move up, at 180 degrees all the movements are reversed i.e. the box moves "Map Rotation" off from your mouse movements. It can make things interesting when aligning your map view but can be done with some patience.

I think QgsTransform needs to be applied to the tools def __createRubberBand(self):, and the def canvasMoveEvent(self, e): to fix this. As I mentioned I have made some attempts but this is as far as I have gotten to get it all to work. FYI I am not the original developer of this tool, I was just looking for a way to do the same thing you are.