Get started with PyQGIS 01 - Some basic commands with QGIS python console

bordoray

Raymond Lay

Posted on September 13, 2023

Get started with PyQGIS 01 - Some basic commands with QGIS python console

QGIS is a useful GIS opensource software which offers many ways of views and analyze geospatial data.
However analysis tools and basic functionalities may be not enough for specific needs, or when tasks need to be automated or easier to use.

PyQGIS is an API which allows us to code in python in QGIS and manipulate many functionalities.
This library is however so wide that it is difficult to know how to get started.

This post is a first step to get started with PyQGIS, from very basic commands.

1. Open Python console in QGIS

Open QGIS, menu -> Plugins -> Python Console
Python console
Or use shortcut Ctrl+Alt+P (Windows) command+option+P (mac)

Python console open usually at the bottom of QGIS frame

Python console on QGIS

2. Run first commands

2.1. Hello world command

Use print("some_result") to display a result



print("hello_world")

"hello_world"


Enter fullscreen mode Exit fullscreen mode

Command

hello world command

result

Hello world

2.2. Get map canvas scale

By launching the following command on python console, Current scale of map canvas can be retrieved.



iface.mapCanvas().scale()


Enter fullscreen mode Exit fullscreen mode

Get map Canvas scale

2.3. Set map canvas scale

With the following command launched on python console, map canvas can be set to 1:25,000 scale



iface.mapCanvas().zoomScale(25000)


Enter fullscreen mode Exit fullscreen mode

Mapcanvas set in 25000

3. Understand commands and PyQGIS library



iface.mapCanvas().scale()
iface.mapCanvas().zoomScale(25000)


Enter fullscreen mode Exit fullscreen mode

Above commands looks not intuitive at a first view, but Python console of QGIS allows us to investigate commands components easily.

For example if we inquiry only



iface.mapCanvas()


Enter fullscreen mode Exit fullscreen mode

Console will return



<qgis._gui.QgsMapCanvas object at 0x1ac03ab80>


Enter fullscreen mode Exit fullscreen mode

which means that iface.mapCanvas() is a QgsMapCanvas object of PyQGIS.

By searching QgsMapCanvas pyqgis on your favorite search browser, you'll get documentation of this class,
https://qgis.org/pyqgis/3.2/gui/Map/QgsMapCanvas.html
and have access of all methods that iface.mapCanvas() is able to do.

Thus, you can try many of methods of QgsMapCanvas class.
Here are two examples:

3.1. center to retrieve center point of map canvas

Qgsmapcanvas center documentation



iface.mapCanvas().center()
<QgsPointXY: POINT(454338.46291687525808811 5845991.44166376255452633)>


Enter fullscreen mode Exit fullscreen mode

center result

3.2. extent where canvas extent can be retrieved



iface.mapCanvas().extent()
<QgsRectangle: 452382.75110932614188641 5845268.24721858091652393, 456294.17472442437428981 5846714.63610894419252872>


Enter fullscreen mode Exit fullscreen mode

extent

etc.

4. First conclusion of PyQGIS approach

Python console of QGIS is very useful to understand PyQGIS.
By launching first commands, and inquiry commands prefixes such as iface.mapCanvas(), you can discover a PyQGIS class (here QgsMapCanvas) and the wealth of methods it offers.

PyQGIS is a very wide library, and understand its architecture becomes easier by inquiring a class like above on the Python Console of QGIS, and discover methods on documentation.

Next post will focus on dealing with layers with pyQGIS

Remark:

in iface.mapCanvas(), you surely wondered what iface means. By the same approach we used on this post, you can run iface command, it will return the QgisInterface class and many many related methods.



# iface() with ()does not work
iface
<qgis._gui.QgisInterface object at 0x15589fe50>


Enter fullscreen mode Exit fullscreen mode

iface command

qgisinterface documentation

๐Ÿ’– ๐Ÿ’ช ๐Ÿ™… ๐Ÿšฉ
bordoray
Raymond Lay

Posted on September 13, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

ยฉ TheLazy.dev

About