1

I am using QGIS (ver2.18.22) to join 2 layers (polygons) at intersections to derive some summary statistics into the join layer ("Grid" layer). The "Grid" layer is assembled by using Create Grid, and the "Merged" layer is assembled by Merge vector layers tool using 8 different layers that all have the same polygons and attributes, just different values for the attributes.

In PyQGIS I do the joining part by the following:

processing.runalg("qgis:joinattributesbylocation", "Grid", "Merged", u'intersects', 0, 1, "mean,min,max,median,q1,q3", 1, OUTPUT_PATH)

(I have also done this using the QGIS GUI) I am getting incorrect values for the Q1 and Q3 (Quartiles) statistics. For example, Q1 = Q3, furthermore, Q1 > median. It seems QGIS 3 does this correctly though. My question is 2 parts:

  1. Is there a plugin for QGIS 2.18 that corrects this behavior in the core Join Attributes By Location tool for getting quartiles?

  2. What would be the equivalent PyQGIS code for QGIS 3? The api for 2 and 3 are totally different. For example, processing.runalg, processing.alglist are not found in QGIS3.

1 Answer 1

0

Not sure about the first part of your question (are you using the latest 2.18 version?) but for the second part, you can always check the PyQGIS equivalent of running native tools from the menubar:

Processing > History

You could see which tools you executed along with the parameters.


The following code is a simple example of using the aforementioned tool:

import processing
# Use following to check the parameters
#processing.algorithmHelp('qgis:joinattributesbylocation')

layer = QgsProject.instance().mapLayersByName('firstLayer')[0]
joined_layer = QgsProject.instance().mapLayersByName('secondLayer')[0]

parameters = {'INPUT':layer, 
                'JOIN':joined_layer, 
                'PREDICATE':[0], 
                'JOIN_FIELDS':['ID'], 
                'METHOD':0, 
                'DISCARD_NONMATCHING':True,
                'PREFIX':'new_',
                'OUTPUT':'memory:'}

processing.runAndLoadResults('qgis:joinattributesbylocation', parameters)
2
  • 1
    I am using version 2.18.22 and it seems there is a minor version update available. I'll try that in case, but the QGIS 3 processing history helps! Thank you.
    – shotes
    Commented Aug 23, 2018 at 21:06
  • @shotes - Most welcome! Glad it helped :)
    – Joseph
    Commented Aug 24, 2018 at 9:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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