I have a shapefile with a little over one million features (but it could also be 10+ million).
I need to categorize them by an attribute they have. It is a float value, and the categories are:
1: attrib < 0.1
2: attrib >= 0.1 and < 0.3
3: attrib >= 0.3 and < 0.5
4: attrib >= 0.5
I want to add this category value in a new field.
Easy enough using the field calculator, I thought:
CASE WHEN attrib < 0.1 THEN 1 WHEN attrib >= 0.1 AND attrib < 0.3 THEN 2 WHEN attrib >= 0.3 AND attrib < 0.5 THEN 3 WHEN attrib < 0.5 THEN 4 END
into a new field ('category', integer, length 1).
This process is very slow (about a minute per 1000 features).
If I just style the features according to the same rules (graduated, 4 classes), that happens in a manner of seconds and I get a fancy visualization, which I take to mean there has to be a faster way to do this.
So the question: Why is the field calculator so much slower than the graduated style, and what can I do to speed this up? I am not opposed or unfamiliar with pyqgis, so a python/processing script solution is just as fine. I attempted a small script that basically does the same as the field calculator above, but it has the same low performance:
##input_layer=vector ##General Tools=group ##Test=name from qgis.core import * layer = processing.getObject(input_layer) layer.startEditing() for i, f in enumerate(layer.getFeatures()): level = f['attrib'] if level < 0.1: f['cat'] = 1 elif level >= 0.1 and level < 0.3: f['cat'] = 2 elif level >= 0.3 and level < 0.5: f['cat'] = 3 elif level >= 0.5: f['cat'] = 4 layer.updateFeature(f) if i % 1000 == 0: # Only added this to track how slow the script is print i progress.setText('Feature ' + str(i)) layer.commitChanges()
Running QGIS 2.18.15.