Get started with PyQGIS 01 - Some basic commands with QGIS python console
Tags: python, qgis, pyqgis
Published: 2023-09-13 13:43:03

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i8bbeipl8but0ghdhg5v.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/em395ftw347mv0beul3n.png)

## 2. Run first commands 
### 2.1. Hello world command 
Use `print("some_result")` to display a result
```python
print("hello_world")

"hello_world"
```
Command


![hello world command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h2a7hvqf4o8zkb6zdk0w.png)



result

![Hello world](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zgc066uibajc7idooinu.png)


### 2.2. Get map canvas scale
By launching the following command on python console, Current scale of map canvas can be retrieved.
```python
iface.mapCanvas().scale()
```

![Get map Canvas scale](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5xmx9z8dzhdd2i30e916.png)



### 2.3. Set map canvas scale
With the following command launched on python console, map canvas can be set to `1:25,000` scale
```python
iface.mapCanvas().zoomScale(25000)
```


![Mapcanvas set in 25000](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oxtgozj8zza2b3qirjb3.png)

## 3. Understand commands and PyQGIS library

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

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 
```python
iface.mapCanvas()
```
Console will return 
```python
<qgis._gui.QgsMapCanvas object at 0x1ac03ab80>
```
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](https://qgis.org/pyqgis/3.2/gui/Map/QgsMapCanvas.html#qgis.gui.QgsMapCanvas.center) to retrieve center point of map canvas

![Qgsmapcanvas center documentation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ewi21g3m74ofjcen8njo.png)

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

![center result](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vaeddt8eogpyc7tp17cu.png)

## 3.2. [extent](https://qgis.org/pyqgis/3.2/gui/Map/QgsMapCanvas.html#qgis.gui.QgsMapCanvas.extent) where canvas extent can be retrieved
```python
iface.mapCanvas().extent()
<QgsRectangle: 452382.75110932614188641 5845268.24721858091652393, 456294.17472442437428981 5846714.63610894419252872>
```

![extent](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w629dyjz09y82i168ng0.png)

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](https://qgis.org/pyqgis/master/gui/QgisInterface.html).
```python
# iface() with ()does not work
iface
<qgis._gui.QgisInterface object at 0x15589fe50>
```

![iface command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4cxk27tshogdq4ecgat1.png)


![qgisinterface documentation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fukc4vulu911zwf3dvhg.png)