2

I'm conducting aggregate function in QGIS. But since the feature number is high, the process is very slow. Is there a way to speed up the calculation process?

By using this function: sum("lengths", "ID-1" ) I aimed to get the sum of the lengths field from all features in the layer with the same ID-1 value as the current feature.

.

4
  • 1
    how is your data stored? is there an index on ID1?
    – Ian Turton
    Jun 22, 2022 at 10:07
  • 1
    I created by using $id from field calculator. They are integer. There is not an index I suppose.
    – Eren E
    Jun 22, 2022 at 10:10
  • 1
    then export your data into a databse and add an index to it
    – Ian Turton
    Jun 22, 2022 at 10:40
  • 1
    I'm very new in this field. So I'm not sure how to do it. Could you suggest a tutorial or any other thing to follow?
    – Eren E
    Jun 22, 2022 at 10:42

1 Answer 1

2

What if you try a Virtual Field with PyQGIS from the QGIS Python Console (Ctrl+Alt+T).

from qgis.utils import iface
from qgis.core import QgsVectorLayer, QgsField
try:
    from PyQt5.QtCore import QVariant
except ImportError:
    from qgis.PyQt.QtCore import QVariant

layer = iface.activeLayer()

layer.startEditing()
field = QgsField('calc', QVariant.String)
layer.addExpressionField('sum("lengths","ID_1")', field)
layer.commitChanges()
4
  • I got null results from this function.
    – Eren E
    Jun 22, 2022 at 16:43
  • Which data types are your input fields ?
    – Taras
    Jun 22, 2022 at 16:43
  • lengths field is Decimal, ID field is whole number Int 64
    – Eren E
    Jun 22, 2022 at 16:51
  • 1
    Can you please share a piece of your data ?
    – Taras
    Jun 22, 2022 at 17:03

Your Answer

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

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