Skip to content
Browse files

Merge pull request #3783 from nyalldawson/create_attribute_index

[FEATURE][processing] Create attribute index algorithm
  • Loading branch information
alexbruy committed Nov 18, 2016
2 parents 263ba81 + 5e1a69f commit 72b4e72f05de753b6d21e3792119c8fe75238e20
@@ -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 -*-

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 import dataobjects

class CreateAttributeIndex(GeoAlgorithm):


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

self.addParameter(ParameterVector(self.INPUT,'Input Layer')))
self.addParameter(ParameterTableField(self.FIELD,'Attribute to index'), self.INPUT))
self.addOutput(OutputVector(self.OUTPUT,'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('Can not create attribute index on "{}"').format(field))
provider_index = layer.fields().fieldOriginIndex(field_index)
if provider.capabilities() & QgsVectorDataProvider.CreateAttributeIndex:
if not provider.createAttributeIndex(provider_index):
progress.setInfo('Could not create attribute index'))
progress.setInfo("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(), 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)
FIELD: fid
name: lines.gml
type: vector
results: {}

0 comments on commit 72b4e72

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