3

I currently want to intersect every line from a Linestring layer with another Linestring layer that I get from a WFS. When I use the GUI to do that (Vector -> Analysis tools -> Line Intersections...) it works perfectly. Yet I am trying to do that using the Python Console inside QGIS3 with the following function:

def loadIntersections():
    player = QgsVectorLayer('Point?crs=EPSG:4326', 'linhas selecionadas', 'memory')
    provider = player.dataProvider()
    provider.addAttributes([QgsField('name', QVariant.String)])
    player.updateFields()
    QgsProject.instance().addMapLayer(player)
    intersect_layer = QgsProject.instance().mapLayersByName('linhas_stpc')[0]
    processing.run('qgis:lineintersections', 'pontos_selecionados', 'linhas_stpc','memory:pontos')

Yet I get the following error:

TypeError: QgsProcessingAlgorithm.checkParameterValues(): argument 1 has unexpected type 'str'

I must note that I have tried to load the layers into alias using the QgsProject.instance().mapLayersByName($LAYER_NAME)[0] but then the unexpected parameter changes from 'str' to 'QgsVectorLayer'. It is worth noticing that in the processing algorithm documentation both types are acceptable.

I only need to replicate the function used by the GUI to a script.

  • I'll try this on my system but it looks like you are passing a string 'linhas_stpc' when the processing function wants a layer object - intersect_layer. – Cary H Jan 28 '20 at 20:12
3

The main problem is that when you call processing algorithms via the Python console, you need to pass the parameter values as a dictionary.

However, there are a couple of things I don't understand about your code:

a) Why you are creating an empty point layer before calling the algorithm.

b) Where your layer 'pontos_selecionados' which you are passing to the algorithm is coming from.

All you need to run the algorithm is two line layer objects. If you call processing.runAndLoadResults(), the output will be added to the table of contents panel.

The snippet below works in the console in QGIS 3.4:

Just change the layer names to access your line and intersect layer objects.

line_layer = QgsProject().instance().mapLayersByName('Line_layer_1')[0]
intersect_layer = QgsProject().instance().mapLayersByName('Line_layer_2')[0]
processing.runAndLoadResults('native:lineintersections', {'INPUT': line_layer, 'INTERSECT': intersect_layer, 'OUTPUT': 'memory'})

By the way, another method to access Python code for algorithms which you have run via the GUI, is to open the History window (clock icon at the top of the processing toolbox) and copy the code from there.

enter image description here

  • Ben - That Algorithm History tip is awesome! Thanks for both of these. – Cary H Jan 29 '20 at 17:38
  • a) I was creating that layer because there would be a single layer for input from the user, which would be that one. b) It would be the layer which upon the user would select the features. Thank you a lot! It finally worked. – Paulo Veras Jan 29 '20 at 18:06
  • @Paulo Vegas, no problem- glad it works. If my answer helped, it would be great if you could accept it. – Ben W Jan 29 '20 at 23:08
  • @Cary H, no problem! Yes it's very handy! You can replace the file paths with layer objects, and change processing.run with processing.runAndLoadResults, but it's certainly helpful for getting the syntax of parameter values. – Ben W Jan 29 '20 at 23:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.