I am currently working on a Python script for QGIS in which I would like to obtain the (minimum) distance from each point (polygon centroid) in the points layer centroid_layer to the line layer nile_layer.
I unfortunately still don't understand how the parameters need to be specified in order to store the distance in a column of the 'FROM' layer, here: centroid_layer.
Error message:
Traceback (most recent call last): File "/Applications/QGIS3.10.app/Contents/Frameworks/Python.framework/Versions/Current/lib/python3.7/code.py", line 90, in runcode exec(code, self.locals) File "", line 1, in File "", line 255, in File "/Applications/QGIS3.10.app/Contents/MacOS/../Resources/python/plugins/processing/tools/general.py", line 106, in run return Processing.runAlgorithm(algOrName, parameters, onFinish, feedback, context) File "/Applications/QGIS3.10.app/Contents/MacOS/../Resources/python/plugins/processing/core/Processing.py", line 137, in runAlgorithm raise QgsProcessingException(msg) _core.QgsProcessingException: Unable to execute algorithm The number of columns and the number of upload parameters should be equal!
Code snippet:
# Import required packages
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import os
import getpass
import requests
from qgis.core import *
from qgis.utils import iface
import qgis.utils
from qgis import core
import processing
from processing.core.Processing import Processing
[...]
# Distance to Nile
params = {
'FROM' : centroid_layer , # 'From' layer, here: Murdock ethnicity centroid layer
'FROM_TYPE' : 0 , # Type of 'from' layer; 0: point, 1: line, 2: boundary, 3: area, 4: centroid
'TO': nile_layer,
'TO_TYPE' : 1 , # Type of 'to' layer; 0: point, 1: line, 2: boundary, 3: area, 4: centroid
'DMAX' : -1.0 , # Minimum distance or -1.0 for no limit
'DMIN' : -1.0 , # Minimum distance or -1.0 for no limit
'UPLOAD' : 1 , # 'dist' distance result type
'COLUMN' : 'distance_to_nile' ,
'OUTPUT' : 'Temporary datasets/Layers/distance_to_nile',
'--OVERWRITE' : True
}
distance_nile_calculation = processing.run('grass7:v.distance', params)
I am running the code from the Python console within QGIS 3.10.0 on MacOS.