Skip to content
Permalink
Browse files

[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
  • Loading branch information
nyalldawson committed Nov 18, 2016
1 parent 263ba81 commit 5e1a69fc88e6ac97675ed178bcb8f0d8e32da3c1
@@ -93,6 +93,9 @@ qgis:countuniquepointsinpolygon: >

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.

qgis:createattributeindex: >
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.

qgis:createconstantrasterlayer: >
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.

@@ -0,0 +1,73 @@
# -*- coding: utf-8 -*-

"""
***************************************************************************
CreateAttributeIndex.py
-----------------------
Date : November 2016
Copyright : (C) 2016 by Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""

__author__ = 'Nyall Dawson'
__date__ = 'November 2016'
__copyright__ = '(C) 2016, Nyall Dawson'

# This will get replaced with a git SHA1 when you do a git archive

__revision__ = '$Format:%H$'

from qgis.core import QgsVectorDataProvider, QgsFields

from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterTableField
from processing.core.outputs import OutputVector

from processing.tools import dataobjects


class CreateAttributeIndex(GeoAlgorithm):

INPUT = 'INPUT'
FIELD = 'FIELD'
OUTPUT = 'OUTPUT'

def defineCharacteristics(self):
self.name, self.i18n_name = self.trAlgorithm('Create attribute index')
self.group, self.i18n_group = self.trAlgorithm('Vector general tools')

self.addParameter(ParameterVector(self.INPUT,
self.tr('Input Layer')))
self.addParameter(ParameterTableField(self.FIELD,
self.tr('Attribute to index'), self.INPUT))
self.addOutput(OutputVector(self.OUTPUT,
self.tr('Indexed layer'), True))

def processAlgorithm(self, progress):
file_name = self.getParameterValue(self.INPUT)
layer = dataobjects.getObjectFromUri(file_name)
field = self.getParameterValue(self.FIELD)
provider = layer.dataProvider()

field_index = layer.fields().lookupField(field)
if field_index < 0 or layer.fields().fieldOrigin(field_index) != QgsFields.OriginProvider:
progress.setInfo(self.tr('Can not create attribute index on "{}"').format(field))
else:
provider_index = layer.fields().fieldOriginIndex(field_index)
if provider.capabilities() & QgsVectorDataProvider.CreateAttributeIndex:
if not provider.createAttributeIndex(provider_index):
progress.setInfo(self.tr('Could not create attribute index'))
else:
progress.setInfo(self.tr("Layer's data provider does not support "
"creating attribute indexes"))

self.setOutputValue(self.OUTPUT, file_name)
@@ -177,6 +177,7 @@
from .GeometryByExpression import GeometryByExpression
from .SnapGeometries import SnapGeometriesToLayer
from .PoleOfInaccessibility import PoleOfInaccessibility
from .CreateAttributeIndex import CreateAttributeIndex

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

if hasMatplotlib:
@@ -1544,4 +1544,11 @@ tests:
name: expected/extract_by_attribute_greater.gml
type: vector


- algorithm: qgis:createattributeindex
name: Create Attribute Index (only tests for python errors, does not check result)
params:
FIELD: fid
INPUT:
name: lines.gml
type: vector
results: {}

1 comment on commit 5e1a69f

@m-kuhn

This comment has been minimized.

Copy link
Member

@m-kuhn m-kuhn commented on 5e1a69f Nov 18, 2016

Thank you Nyall!!!

Please sign in to comment.
You can’t perform that action at this time.