Skip to content

Commit cecfff0

Browse files
committedOct 7, 2013
[processing] added extract algorithm as alternative to selection algorithms that can be used in the modeler
1 parent 0150fe7 commit cecfff0

File tree

7 files changed

+488
-175
lines changed

7 files changed

+488
-175
lines changed
 

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
__revision__ = '$Format:%H$'
2727

2828
from PyQt4.QtGui import *
29+
from processing.algs.ftools.RandomExtract import RandomExtract
30+
from processing.algs.ftools.RandomExtractWithinSubsets import \
31+
RandomExtractWithinSubsets
32+
from processing.algs.ftools.ExtractByLocation import ExtractByLocation
33+
2934
from processing.core.AlgorithmProvider import AlgorithmProvider
3035
from processing.algs.ftools.PointsInPolygon import PointsInPolygon
3136
from processing.algs.ftools.PointsInPolygonUnique import PointsInPolygonUnique
@@ -80,7 +85,8 @@
8085
mmqgisx_geometry_convert_algorithm, mmqgisx_grid_algorithm, \
8186
mmqgisx_gridify_algorithm, mmqgisx_hub_distance_algorithm, \
8287
mmqgisx_hub_lines_algorithm, mmqgisx_merge_algorithm, \
83-
mmqgisx_select_algorithm, mmqgisx_text_to_float_algorithm
88+
mmqgisx_select_algorithm, mmqgisx_text_to_float_algorithm,\
89+
mmqgisx_extract_algorithm
8490

8591
from processing.algs.Polygonize import Polygonize
8692
from processing.algs.RasterLayerStatistics import RasterLayerStatistics
@@ -128,7 +134,8 @@ def __init__(self):
128134
VariableDistanceBuffer(), Dissolve(), Difference(),
129135
Intersection(), Union(), Clip(), ExtentFromLayer(),
130136
RandomSelection(), RandomSelectionWithinSubsets(),
131-
SelectByLocation(),
137+
SelectByLocation(), RandomExtract(), RandomExtractWithinSubsets(),
138+
ExtractByLocation(),
132139
# ------ mmqgisx ------
133140
mmqgisx_delete_columns_algorithm(),
134141
mmqgisx_delete_duplicate_geometries_algorithm(),
@@ -139,6 +146,7 @@ def __init__(self):
139146
mmqgisx_hub_lines_algorithm(),
140147
mmqgisx_merge_algorithm(),
141148
mmqgisx_select_algorithm(),
149+
mmqgisx_extract_algorithm(),
142150
mmqgisx_text_to_float_algorithm(),
143151
# ------ native algs ------
144152
AddTableField(), FieldsCalculator(),
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
ExtractByLocation.py
6+
---------------------
7+
Date : August 2012
8+
Copyright : (C) 2012 by Victor Olaya
9+
Email : volayaf 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__ = 'Victor Olaya'
21+
__date__ = 'August 2012'
22+
__copyright__ = '(C) 2012, Victor Olaya'
23+
24+
# This will get replaced with a git SHA1 when you do a git archive
25+
26+
__revision__ = '$Format:%H$'
27+
28+
from PyQt4.QtCore import *
29+
from qgis.core import *
30+
from processing.core.GeoAlgorithm import GeoAlgorithm
31+
from processing.parameters.ParameterVector import ParameterVector
32+
from processing.outputs.OutputVector import OutputVector
33+
from processing.tools import dataobjects, vector
34+
35+
36+
class ExtractByLocation(GeoAlgorithm):
37+
38+
INPUT = 'INPUT'
39+
INTERSECT = 'INTERSECT'
40+
OUTPUT = 'OUTPUT'
41+
42+
def defineCharacteristics(self):
43+
self.name = 'Extract by location'
44+
self.group = 'Vector selection tools'
45+
self.addParameter(ParameterVector(self.INPUT, 'Layer to select from',
46+
[ParameterVector.VECTOR_TYPE_ANY]))
47+
self.addParameter(ParameterVector(self.INTERSECT,
48+
'Additional layer (intersection layer)',
49+
[ParameterVector.VECTOR_TYPE_ANY]))
50+
self.addOutput(OutputVector(self.OUTPUT, 'Selection'))
51+
52+
def processAlgorithm(self, progress):
53+
filename = self.getParameterValue(self.INPUT)
54+
layer = dataobjects.getObjectFromUri(filename)
55+
filename = self.getParameterValue(self.INTERSECT)
56+
selectLayer = dataobjects.getObjectFromUri(filename)
57+
index = vector.spatialindex(layer)
58+
59+
geom = QgsGeometry()
60+
selectedSet = []
61+
current = 0
62+
features = vector.features(selectLayer)
63+
featureCount = len(features)
64+
total = 100.0 / float(len(features))
65+
for current,f in enumerate(features):
66+
geom = QgsGeometry(f.geometry())
67+
intersects = index.intersects(geom.boundingBox())
68+
for i in intersects:
69+
request = QgsFeatureRequest().setFilterFid(i)
70+
feat = layer.getFeatures(request).next()
71+
tmpGeom = QgsGeometry(feat.geometry())
72+
if geom.intersects(tmpGeom):
73+
selectedSet.append(feat.id())
74+
progress.setPercentage(int(current * total))
75+
76+
output = self.getOutputFromName(self.OUTPUT)
77+
writer = output.getVectorWriter(layer.fields(),
78+
layer.geometryType(), layer.crs())
79+
80+
for (i, feat) in enumerate(features):
81+
if feat.id() in selectedSet:
82+
writer.addFeature(feat)
83+
progress.setPercentage(100 * i / float(featureCount))
84+
del writer
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
RandomExtract.py
6+
---------------------
7+
Date : August 2012
8+
Copyright : (C) 2012 by Victor Olaya
9+
Email : volayaf 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__ = 'Victor Olaya'
21+
__date__ = 'August 2012'
22+
__copyright__ = '(C) 2012, Victor Olaya'
23+
24+
# This will get replaced with a git SHA1 when you do a git archive
25+
26+
__revision__ = '$Format:%H$'
27+
28+
import random
29+
30+
from PyQt4.QtCore import *
31+
from qgis.core import *
32+
33+
from processing.core.GeoAlgorithm import GeoAlgorithm
34+
from processing.core.GeoAlgorithmExecutionException import \
35+
GeoAlgorithmExecutionException
36+
from processing.parameters.ParameterSelection import ParameterSelection
37+
from processing.parameters.ParameterVector import ParameterVector
38+
from processing.parameters.ParameterNumber import ParameterNumber
39+
from processing.outputs.OutputVector import OutputVector
40+
from processing.tools import dataobjects, vector
41+
42+
43+
class RandomExtract(GeoAlgorithm):
44+
45+
INPUT = 'INPUT'
46+
OUTPUT = 'OUTPUT'
47+
METHOD = 'METHOD'
48+
NUMBER = 'NUMBER'
49+
50+
METHODS = ['Number of selected features',
51+
'Percentage of selected features']
52+
def defineCharacteristics(self):
53+
self.name = 'Random extract'
54+
self.group = 'Vector selection tools'
55+
56+
self.addParameter(ParameterVector(self.INPUT, 'Input layer',
57+
[ParameterVector.VECTOR_TYPE_ANY]))
58+
self.addParameter(ParameterSelection(self.METHOD, 'Method',
59+
self.METHODS, 0))
60+
self.addParameter(ParameterNumber(self.NUMBER,
61+
'Number/percentage of selected features', 0, None,
62+
10))
63+
self.addOutput(OutputVector(self.OUTPUT, 'Selection'))
64+
65+
def processAlgorithm(self, progress):
66+
filename = self.getParameterValue(self.INPUT)
67+
layer = dataobjects.getObjectFromUri(filename)
68+
method = self.getParameterValue(self.METHOD)
69+
70+
features = vector.features(layer)
71+
featureCount = len(features)
72+
value = int(self.getParameterValue(self.NUMBER))
73+
74+
if method == 0:
75+
if value > featureCount:
76+
raise GeoAlgorithmExecutionException(
77+
'Selected number is greater than feature count. \
78+
Choose a lower value and try again.')
79+
else:
80+
if value > 100:
81+
raise GeoAlgorithmExecutionException(
82+
"Percentage can't be greater than 100. Set a \
83+
different value and try again.")
84+
value = int(round(value / 100.0000, 4) * featureCount)
85+
86+
selran = random.sample(xrange(0, featureCount), value)
87+
88+
output = self.getOutputFromName(self.OUTPUT)
89+
writer = output.getVectorWriter(layer.fields(),
90+
layer.geometryType(), layer.crs())
91+
92+
for (i, feat) in enumerate(features):
93+
if i in selran:
94+
writer.addFeature(feat)
95+
progress.setPercentage(100 * i / float(featureCount))
96+
del writer
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
RandomSelectionWithinSubsets.py
6+
---------------------
7+
Date : August 2012
8+
Copyright : (C) 2012 by Victor Olaya
9+
Email : volayaf 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__ = 'Victor Olaya'
21+
__date__ = 'August 2012'
22+
__copyright__ = '(C) 2012, Victor Olaya'
23+
24+
# This will get replaced with a git SHA1 when you do a git archive
25+
26+
__revision__ = '$Format:%H$'
27+
28+
import random
29+
from PyQt4.QtCore import *
30+
from qgis.core import *
31+
from processing.core.GeoAlgorithm import GeoAlgorithm
32+
from processing.core.GeoAlgorithmExecutionException import \
33+
GeoAlgorithmExecutionException
34+
from processing.parameters.ParameterSelection import ParameterSelection
35+
from processing.parameters.ParameterVector import ParameterVector
36+
from processing.parameters.ParameterNumber import ParameterNumber
37+
from processing.parameters.ParameterTableField import ParameterTableField
38+
from processing.outputs.OutputVector import OutputVector
39+
from processing.tools import dataobjects, vector
40+
41+
42+
class RandomExtractWithinSubsets(GeoAlgorithm):
43+
44+
INPUT = 'INPUT'
45+
METHOD = 'METHOD'
46+
NUMBER = 'NUMBER'
47+
FIELD = 'FIELD'
48+
OUTPUT = 'OUTPUT'
49+
50+
METHODS = ['Number of selected features',
51+
'Percentage of selected features']
52+
53+
def defineCharacteristics(self):
54+
self.name = 'Random extract within subsets'
55+
self.group = 'Vector selection tools'
56+
57+
self.addParameter(ParameterVector(self.INPUT, 'Input layer',
58+
[ParameterVector.VECTOR_TYPE_ANY]))
59+
self.addParameter(ParameterTableField(self.FIELD, 'ID Field',
60+
self.INPUT))
61+
self.addParameter(ParameterSelection(self.METHOD, 'Method',
62+
self.METHODS, 0))
63+
self.addParameter(ParameterNumber(self.NUMBER,
64+
'Number/percentage of selected features', 1, None,
65+
10))
66+
67+
self.addOutput(OutputVector(self.OUTPUT, 'Selection'))
68+
69+
def processAlgorithm(self, progress):
70+
filename = self.getParameterValue(self.INPUT)
71+
72+
layer = dataobjects.getObjectFromUri(filename)
73+
field = self.getParameterValue(self.FIELD)
74+
method = self.getParameterValue(self.METHOD)
75+
76+
index = layer.fieldNameIndex(field)
77+
78+
features = vector.features(layer)
79+
featureCount = len(features)
80+
unique = vector.getUniqueValues(layer, index)
81+
value = int(self.getParameterValue(self.NUMBER))
82+
if method == 0:
83+
if value > featureCount:
84+
raise GeoAlgorithmExecutionException(
85+
'Selected number is greater that feature count. \
86+
Choose lesser value and try again.')
87+
else:
88+
if value > 100:
89+
raise GeoAlgorithmExecutionException(
90+
"Percentage can't be greater than 100. Set correct \
91+
value and try again.")
92+
value = value / 100.0
93+
94+
95+
output = self.getOutputFromName(self.OUTPUT)
96+
writer = output.getVectorWriter(layer.fields(),
97+
layer.geometryType(), layer.crs())
98+
99+
selran = []
100+
current = 0
101+
total = 100.0 / float(featureCount * len(unique))
102+
features = vector.features(layer)
103+
104+
if not len(unique) == featureCount:
105+
for classValue in unique:
106+
classFeatures = []
107+
for i, feature in enumerate(features):
108+
attrs = feature.attributes()
109+
if attrs[index] == classValue:
110+
classFeatures.append(i)
111+
current += 1
112+
progress.setPercentage(int(current * total))
113+
114+
if method == 1:
115+
selValue = int(round(value * len(classFeatures), 0))
116+
else:
117+
selValue = value
118+
119+
if selValue >= len(classFeatures):
120+
selFeat = classFeatures
121+
else:
122+
selFeat = random.sample(classFeatures, selValue)
123+
124+
selran.extend(selFeat)
125+
else:
126+
selran = range(0, featureCount)
127+
128+
129+
features = vector.features(layer)
130+
for (i, feat) in enumerate(features):
131+
if i in selran:
132+
writer.addFeature(feat)
133+
progress.setPercentage(100 * i / float(featureCount))
134+
del writer

‎python/plugins/processing/algs/ftools/RandomSelectionWithinSubsets.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,6 @@ class RandomSelectionWithinSubsets(GeoAlgorithm):
5050
METHODS = ['Number of selected features',
5151
'Percentage of selected features']
5252

53-
# =========================================================================
54-
# def getIcon(self):
55-
# return QIcon(os.path.dirname(__file__) + \
56-
# "/icons/random_selection.png")
57-
# =========================================================================
58-
5953
def defineCharacteristics(self):
6054
self.allowOnlyOpenedLayers = True
6155
self.name = 'Random selection within subsets'

‎python/plugins/processing/algs/ftools/SelectByLocation.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ class SelectByLocation(GeoAlgorithm):
4444
METHODS = ['creating new selection', 'adding to current selection',
4545
'removing from current selection']
4646

47-
# =========================================================================
48-
# def getIcon(self):
49-
# return QIcon(os.path.dirname(__file__) + "/icons/select_location.png")
50-
# =========================================================================
5147

5248
def defineCharacteristics(self):
5349
self.allowOnlyOpenedLayers = True
@@ -70,9 +66,8 @@ def processAlgorithm(self, progress):
7066
selectLayer = dataobjects.getObjectFromUri(filename)
7167

7268
oldSelection = set(inputLayer.selectedFeaturesIds())
73-
index = spatialIndex = vector.spatialindex(inputLayer)
74-
75-
feat = QgsFeature()
69+
index = vector.spatialindex(inputLayer)
70+
7671
geom = QgsGeometry()
7772
selectedSet = []
7873
current = 0

0 commit comments

Comments
 (0)
Please sign in to comment.