Skip to content

Commit 5e1a69f

Browse files
committedNov 18, 2016
[FEATURE][processing] Create attribute index algorithm
Allows creation of an index on an attribute in a layer for faster attribute based filtering Support depends on the underlying data provider for the layer
1 parent 263ba81 commit 5e1a69f

File tree

4 files changed

+86
-2
lines changed

4 files changed

+86
-2
lines changed
 

‎python/plugins/processing/algs/help/qgis.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ qgis:countuniquepointsinpolygon: >
9393

9494
A new polygons layer is generated, with the exact same content as the input polygons layer, but containing an additional field with the points count corresponding to each polygon.
9595

96+
qgis:createattributeindex: >
97+
Creates an index to speed up queries made against a field in a table. Support for index creation is dependant on the layer's data provider and the field type.
98+
9699
qgis:createconstantrasterlayer: >
97100
Given an input raster layer an a value, this algorithm generates a new layer with the same extent and cellsize as the input one, and all cells with the specified value.
98101

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
CreateAttributeIndex.py
6+
-----------------------
7+
Date : November 2016
8+
Copyright : (C) 2016 by Nyall Dawson
9+
Email : nyall dot dawson at gmail dot com
10+
***************************************************************************
11+
* *
12+
* This program is free software; you can redistribute it and/or modify *
13+
* it under the terms of the GNU General Public License as published by *
14+
* the Free Software Foundation; either version 2 of the License, or *
15+
* (at your option) any later version. *
16+
* *
17+
***************************************************************************
18+
"""
19+
20+
__author__ = 'Nyall Dawson'
21+
__date__ = 'November 2016'
22+
__copyright__ = '(C) 2016, Nyall Dawson'
23+
24+
# This will get replaced with a git SHA1 when you do a git archive
25+
26+
__revision__ = '$Format:%H$'
27+
28+
from qgis.core import QgsVectorDataProvider, QgsFields
29+
30+
from processing.core.GeoAlgorithm import GeoAlgorithm
31+
from processing.core.parameters import ParameterVector
32+
from processing.core.parameters import ParameterTableField
33+
from processing.core.outputs import OutputVector
34+
35+
from processing.tools import dataobjects
36+
37+
38+
class CreateAttributeIndex(GeoAlgorithm):
39+
40+
INPUT = 'INPUT'
41+
FIELD = 'FIELD'
42+
OUTPUT = 'OUTPUT'
43+
44+
def defineCharacteristics(self):
45+
self.name, self.i18n_name = self.trAlgorithm('Create attribute index')
46+
self.group, self.i18n_group = self.trAlgorithm('Vector general tools')
47+
48+
self.addParameter(ParameterVector(self.INPUT,
49+
self.tr('Input Layer')))
50+
self.addParameter(ParameterTableField(self.FIELD,
51+
self.tr('Attribute to index'), self.INPUT))
52+
self.addOutput(OutputVector(self.OUTPUT,
53+
self.tr('Indexed layer'), True))
54+
55+
def processAlgorithm(self, progress):
56+
file_name = self.getParameterValue(self.INPUT)
57+
layer = dataobjects.getObjectFromUri(file_name)
58+
field = self.getParameterValue(self.FIELD)
59+
provider = layer.dataProvider()
60+
61+
field_index = layer.fields().lookupField(field)
62+
if field_index < 0 or layer.fields().fieldOrigin(field_index) != QgsFields.OriginProvider:
63+
progress.setInfo(self.tr('Can not create attribute index on "{}"').format(field))
64+
else:
65+
provider_index = layer.fields().fieldOriginIndex(field_index)
66+
if provider.capabilities() & QgsVectorDataProvider.CreateAttributeIndex:
67+
if not provider.createAttributeIndex(provider_index):
68+
progress.setInfo(self.tr('Could not create attribute index'))
69+
else:
70+
progress.setInfo(self.tr("Layer's data provider does not support "
71+
"creating attribute indexes"))
72+
73+
self.setOutputValue(self.OUTPUT, file_name)

‎python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@
177177
from .GeometryByExpression import GeometryByExpression
178178
from .SnapGeometries import SnapGeometriesToLayer
179179
from .PoleOfInaccessibility import PoleOfInaccessibility
180+
from .CreateAttributeIndex import CreateAttributeIndex
180181

181182
pluginPath = os.path.normpath(os.path.join(
182183
os.path.split(os.path.dirname(__file__))[0], os.pardir))
@@ -240,7 +241,7 @@ def __init__(self):
240241
TinInterpolationZValue(), TinInterpolationAttribute(),
241242
RemoveNullGeometry(), ExtractByExpression(), ExtendLines(),
242243
ExtractSpecificNodes(), GeometryByExpression(), SnapGeometriesToLayer(),
243-
PoleOfInaccessibility()
244+
PoleOfInaccessibility(), CreateAttributeIndex()
244245
]
245246

246247
if hasMatplotlib:

‎python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1544,4 +1544,11 @@ tests:
15441544
name: expected/extract_by_attribute_greater.gml
15451545
type: vector
15461546

1547-
1547+
- algorithm: qgis:createattributeindex
1548+
name: Create Attribute Index (only tests for python errors, does not check result)
1549+
params:
1550+
FIELD: fid
1551+
INPUT:
1552+
name: lines.gml
1553+
type: vector
1554+
results: {}

1 commit comments

Comments
 (1)

m-kuhn commented on Nov 18, 2016

@m-kuhn
Member

Thank you Nyall!!!

Please sign in to comment.