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
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.
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.
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