Type Python scripts to run in QGIS Processing

Introduction

This is the third time I have written an article about Processing [^ 1] [^ 2]. QGIS Processing is convenient, isn't it? As the version of QGIS goes up, there are more algorithms, better UI, and I feel like it's getting easier to use. I myself have been indebted to Processing so much, and when I think about doing something using vector raster data, or when I get a consultation and try to help, I first look for a solution in Processing. It has a habit. And even though it's usually found and sometimes unsolvable, it's often a clue to some sort of solution. Well, this time I'm going to write what I thought about Python when writing Processing scripts, rather than Processing.

Also, in writing this article, I am targeting QGIS3.10, which is LTS at the time of writing (2020/12/16).

[^ 1]: I made a pan-sharpened image using QGIS processing [^ 2]: What you can do with QGIS processing

Type in Python

Python is a dynamically typed language, so I don't think you'll be aware of types when writing code. However, with the implementation of PEP484 [^ 3] in Python 3.5, it is possible to write types in Python code written targeting Python 3.5 or later. [^ 3]: For Japanese translation, here will be helpful. It's like this.

Code that wrote the type


def sum(a: int, b: int) -> int:
    return a + b

class Point2D:
    def __init__(self, x: float, y: float) -> None:
        self.x: float = x
        self.y: float = y

p = Point2D(0.0, 0.0)

Untyped code


def sum(a, b):
    return a + b

class Point2D:
    def __init__(self, x, y):
        self.x = x
        self.y = y

p = Point2D(0.0, 0.0)

How about that. It's a personal impression, but the code that wrote the type has a return value that is an integer when calling the function sum than the code that does not write the type, and the Point2D class I think it's easy to understand that the x and y attributes are decimal numbers at a glance. The version of Python included in QGIS 3.10 is 3.7.3, so you can execute Python code with such types.

Type in Processing

Now that you can write types in Python code, what happens to Processing is that it's easier to understand whether the result of executing the Processing algorithm is a vector layer or a raster layer.

For example, if you try to execute a Processing algorithm in Python code, it will look like this:

There is a type description in the assigned variable


from typing import Dict

#Execution example of Intersect
params: Dict[str, Any] = { ... }
interect_layer: QgsVectorLayer = processing.run("native:intersection", params)['OUTPUT']

#Execution example of raster computer
params: Dict[str, Any] = { ... }
calc_layer: QgsRasterLayer = processing.run("qgis:rastercalculator", params)['OUTPUT']

No type description for assigned variable


#Execution example of Intersect
params = { ... }
intersect_layer = processing.run("native:intersection", params)['OUTPUT']

#Execution example of raster computer
params = { ... }
calc_layer = processing.run("qgis:rastercalculator", params)['OUTPUT']

How about that. When executing the Processing algorithm, you can pass the algorithm name you want to execute and the corresponding parameter as an argument to processing.run (), so you can write it universally, but at the same time, whether the processing result is a vector layer or a raster layer, I don't think it's easy to see. On the other hand, by writing types such as QgsVectorLayer and QgsRasterLayer in the variable to which the received result is assigned, it may be easier to understand the processing result.

in conclusion

It's true that writing a type in a script will result in more code to write than before, but considering that code takes longer to read than to write, I looked back. I think it should be easy to understand what you are doing at times. It doesn't matter if you don't write the type, but I think you'll be happier if you write the type later, so if you don't mind either, you should write the type.

Especially when trying to create a complicated Processing script, the amount of code in one Python script file tends to be large. Even if you try to avoid it, the algorithm itself is subdivided, and after all, you call it with processing.run (), assign the processing result to a variable, and move on to the next input ... I will end up.

that? Is the data for this variable a raster? Is it a vector? I want to write the type to reduce confusion.

Recommended Posts

Type Python scripts to run in QGIS Processing
Get Python scripts to run quickly in Cloud Run using responder
File processing in Python
A clever way to time processing in Python
Text processing in Python
Queue processing in Python
How to run Leap Motion in non-Apple Python
How to handle datetime type in python sqlite3
Type notes to Python scripts for running PyTorch model in C ++ with libtorch
How to run python in virtual space (for MacOS)
How to run tests in bulk with Python unittest
Create a plugin to run Python Doctest in Vim (2)
Create a plugin to run Python Doctest in Vim (1)
A memorandum to run a python script in a bat file
How to run setUp only once in python unittest
Convert Webpay Entity type to Dict type (recursively in Python)
Things to keep in mind when processing strings in Python2
Things to keep in mind when processing strings in Python3
UTF8 text processing in python
To flush stdout in Python
Run automatic jobs in python
Dynamically import scripts in Python
Run shell commands in python
Run Python unittests in parallel
Login to website in Python
Asynchronous processing (threading) in python
How to run Notepad ++ Python
Speech to speech in python [text to speech]
Image Processing Collection in Python
How to develop in Python
Using Python mode in Processing
Post to Slack in Python
Processing of python3 that seems to be usable in paiza
I want to be able to run Python in VS Code
How to execute external shell scripts and commands in python
Leave the troublesome processing to Python
[Python] How to do PCA in Python
Signal processing in Python (1): Fourier transform
Function argument type definition in python
Practice! !! Introduction to Python (Type Hints)
Convert markdown to PDF in Python
Let's run "python -m antigravity" in python
How to collect images in Python
100 Language Processing Knock Chapter 1 in Python
Run shell command / python in R
How to use SQLite in Python
Dynamically load json type in python
In the python command python points to python3.8
Try to calculate Trace in Python
Type specified in python. Throw exceptions
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
6 ways to string objects in Python
How to use PubChem in Python
Run Python scripts synchronously from C #
Run unittests in Python (for beginners)
How to run TensorFlow 1.0 code in 2.0
Run a simple algorithm in Python
How to handle Japanese in Python
An alternative to `pause` in Python