[QGIS] Using the processing toolbox function in pyqgis in Pycharm

After searching on the Internet, I found a way to solve the problem. To use the toolbox function through the processing method, additional environment variables need to be configured . I set them according to the following two posts.

PyCharm QGIS environment configuration

Aiming at the pitfalls of environment construction during the secondary development of QGIS (solve the problems of no module named qgis and no module named processing)

1. Introduce the location of processing into the environment variable in the python code and directly reference processing.

Due to the needs of the project, QGIS was used to build an automatic processing program. It was too inconvenient to run the code in the Python console in QGIS, so the running environment was simply moved to Pycharm.

First, you need to configure the pyqgis environment on Pycharm. There are already many articles on the website, so I won’t go into details here. I’ll post a link below and you can configure it yourself.

PyCharm configures QGIS secondary development_Wu Daojian's blog-CSDN blog

Simply put, just set python-qgis-ltr.bat in QGIS as the interpreter in pycharm.

After configuring the environment, I found that I can call pyqgis packages, including qgis.core, qgis.gui, etc. But when I ran the previous program, I found that the compiler reported an error. When running to use the toolbox function, it will prompt that it cannot be used , but the same program runs without error on the console in QGIS.

from qgis.core import *
import processing

#加载道路
path_to_roads='D:\QGIS_Data\data\qgis_sample_data.v2\exercise_data\epsg4326\\road4.gpkg'
out_path_to_roads='D:\QGIS_Data\data\qgis_sample_data.v2\exercise_data\epsg4326\\road422.gpkg'
roads=QgsVectorLayer(path_to_roads, "roads", "ogr") 

# 配置道路缓冲区参数
parameter_dictionary_road = {
        'INPUT': roads,
        'DISTANCE': 50,
        'SEGMENTS': 5,
        'DISSOLVE': True,
        'OUTPUT': out_path_to_roads,
        'END_CAP_STYLE': 0,
        'JOIN_STYLE': 0,
        'MITER_LIMIT': 10
    }

roads_buffer = processing.run("native:buffer", parameter_dictionary_road)
print('缓冲区创建成功')

After searching on the Internet, I found a way to solve the problem. To use the toolbox function through the processing method, additional environment variables need to be configured . I set them according to the following two posts.

PyCharm QGIS environment configuration

Aiming at the pitfalls of environment construction during the secondary development of QGIS (solve the problems of no module named qgis and no module named processing)

1. Introduce the location of processing into the environment variable in the python code and directly reference processing.

import sys
sys.path.append(r'D:\\QGIS\\apps\\qgis\\python\\plugins')
import processing

2. Create the PYTHONPATH field in the environment variable and introduce the following address into the environment variable.

After re-running the program, I found that the error was still reported and the buffer function was not found.

After searching on the Internet, I found that there was a problem with the reference to Processing. Processing needs to be initialized before it can be referenced normally.

3. Initialize Processing

import sys
sys.path.append(r'D:\\QGIS\\apps\\qgis\\python\\plugins')
from processing.core.Processing import Processing
Processing.initialize()
import processing

Now there is no problem with the code and it runs successfully !

Attached new complete code:

from qgis.core import *
import sys
sys.path.append(r'D:\\QGIS\\apps\\qgis\\python\\plugins')
from processing.core.Processing import Processing
Processing.initialize()
import processing

#加载道路
path_to_roads='D:\QGIS_Data\data\qgis_sample_data.v2\exercise_data\epsg4326\\road4.gpkg'
out_path_to_roads='D:\QGIS_Data\data\qgis_sample_data.v2\exercise_data\epsg4326\\road422.gpkg'
roads=QgsVectorLayer(path_to_roads, "roads", "ogr") 

# 配置道路缓冲区参数
parameter_dictionary_road = {
        'INPUT': roads,
        'DISTANCE': 50,
        'SEGMENTS': 5,
        'DISSOLVE': True,
        'OUTPUT': out_path_to_roads,
        'END_CAP_STYLE': 0,
        'JOIN_STYLE': 0,
        'MITER_LIMIT': 10
    }

roads_buffer = processing.run("native:buffer", parameter_dictionary_road)
print('缓冲区创建成功')

Supongo que te gusta

Origin blog.csdn.net/ximi2231/article/details/123203768
Recomendado
Clasificación