2

I have a PyQGIS application that uses the QgsMapCanvas as map display. I am trying to retrieve its four corners' coordinates, but couldn't find any public function to do that. All I have is its width() and height().

Is there any way to get the actual coordinates of the corners of the viewport?

Let me provide further context, I need the map canvas's viewport's corners' coordinates when it has not had any layers loaded into it yet or the first layer I am constructing is in memory that has yet to set its projection, I found that in such cases, the map canvas's extent is empty, so calling its extent won't help.

2 Answers 2

3

Would extent() work?

canvas = iface.mapCanvas()
extent = canvas.extent()
print(extent)

Taking it one step further you can break the QgsRectangle object into its components:

xmax = extent.xMaximum()
ymax = extent.yMaximum()
xmin = extent.xMinimum()
ymin = extent.yMinimum()
4
  • No, when there is no data layer added to the canvas, or dynamically adding features into a "in memory" layer on an empty canvas, the extent is empty, so calling extent would not work. Jan 16, 2020 at 2:00
  • 1
    @ForComment - The extent returned here is for the map canvas, not of any layer.
    – Joseph
    Jan 16, 2020 at 10:47
  • Yes, I want the map canvas's current viewport's corners' coordinates, but if you try it either in QGIS python console or in a standalone PyQgis app, you will find that there seems to be no way of getting them directly. I like to be told that I am wrong. Jan 16, 2020 at 16:03
  • I have a blank canvas and it was able to calculate the extents using the python console.canvas = iface.mapCanvas() extent = canvas.extent() print(extent) <QgsRectangle: -2.02627511591962906 -1, 2.02627511591962906 1> xmax = extent.xMaximum() ymax = extent.yMaximum() print(xmax) 2.026275115919629 print(ymax) 1.0
    – GreyHippo
    Jan 17, 2020 at 13:45
0

QgsMapCanvas is just a subclass of the Graphics View Framework's QGraphicsView class. The documentation for QgsMapCanvas doesn't list the base class members, so you need to consult the Graphics View Framework documentation.

For example: https://www.riverbankcomputing.com/static/Docs/PyQt4/qgraphicsview.html

The sceneRect() method is probably what you want. It gives you the rectangle of the QGraphicsView (or QgsMapCanvas) object, relative to the underlying QGraphicsScene. The rectangle is returned as a QRectF object.

So, if myMapCanvas holds your QgsMapCanvas object ...

 myCanvasRect = myMapCanvas.sceneRect()

QRectF has methods like x(), y(), width(), height(), left(), right(), top() and bottom() that return float values.

It also has convenience methods, like center(), topLeft(), bottomRight() etc. that return QPointF objects.

Don't confuse the sceneRect() and extent() methods. The former returns a rectangle in scene space. The latter returns a rectangle in map space.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.