-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[processing] added extract algorithm as alternative to selection algo…
…rithms that can be used in the modeler
- Loading branch information
Showing
7 changed files
with
488 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
python/plugins/processing/algs/ftools/ExtractByLocation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
*************************************************************************** | ||
ExtractByLocation.py | ||
--------------------- | ||
Date : August 2012 | ||
Copyright : (C) 2012 by Victor Olaya | ||
Email : volayaf 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__ = 'Victor Olaya' | ||
__date__ = 'August 2012' | ||
__copyright__ = '(C) 2012, Victor Olaya' | ||
|
||
# This will get replaced with a git SHA1 when you do a git archive | ||
|
||
__revision__ = '$Format:%H$' | ||
|
||
from PyQt4.QtCore import * | ||
from qgis.core import * | ||
from processing.core.GeoAlgorithm import GeoAlgorithm | ||
from processing.parameters.ParameterVector import ParameterVector | ||
from processing.outputs.OutputVector import OutputVector | ||
from processing.tools import dataobjects, vector | ||
|
||
|
||
class ExtractByLocation(GeoAlgorithm): | ||
|
||
INPUT = 'INPUT' | ||
INTERSECT = 'INTERSECT' | ||
OUTPUT = 'OUTPUT' | ||
|
||
def defineCharacteristics(self): | ||
self.name = 'Extract by location' | ||
self.group = 'Vector selection tools' | ||
self.addParameter(ParameterVector(self.INPUT, 'Layer to select from', | ||
[ParameterVector.VECTOR_TYPE_ANY])) | ||
self.addParameter(ParameterVector(self.INTERSECT, | ||
'Additional layer (intersection layer)', | ||
[ParameterVector.VECTOR_TYPE_ANY])) | ||
self.addOutput(OutputVector(self.OUTPUT, 'Selection')) | ||
|
||
def processAlgorithm(self, progress): | ||
filename = self.getParameterValue(self.INPUT) | ||
layer = dataobjects.getObjectFromUri(filename) | ||
filename = self.getParameterValue(self.INTERSECT) | ||
selectLayer = dataobjects.getObjectFromUri(filename) | ||
index = vector.spatialindex(layer) | ||
|
||
geom = QgsGeometry() | ||
selectedSet = [] | ||
current = 0 | ||
features = vector.features(selectLayer) | ||
featureCount = len(features) | ||
total = 100.0 / float(len(features)) | ||
for current,f in enumerate(features): | ||
geom = QgsGeometry(f.geometry()) | ||
intersects = index.intersects(geom.boundingBox()) | ||
for i in intersects: | ||
request = QgsFeatureRequest().setFilterFid(i) | ||
feat = layer.getFeatures(request).next() | ||
tmpGeom = QgsGeometry(feat.geometry()) | ||
if geom.intersects(tmpGeom): | ||
selectedSet.append(feat.id()) | ||
progress.setPercentage(int(current * total)) | ||
|
||
output = self.getOutputFromName(self.OUTPUT) | ||
writer = output.getVectorWriter(layer.fields(), | ||
layer.geometryType(), layer.crs()) | ||
|
||
for (i, feat) in enumerate(features): | ||
if feat.id() in selectedSet: | ||
writer.addFeature(feat) | ||
progress.setPercentage(100 * i / float(featureCount)) | ||
del writer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
*************************************************************************** | ||
RandomExtract.py | ||
--------------------- | ||
Date : August 2012 | ||
Copyright : (C) 2012 by Victor Olaya | ||
Email : volayaf 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__ = 'Victor Olaya' | ||
__date__ = 'August 2012' | ||
__copyright__ = '(C) 2012, Victor Olaya' | ||
|
||
# This will get replaced with a git SHA1 when you do a git archive | ||
|
||
__revision__ = '$Format:%H$' | ||
|
||
import random | ||
|
||
from PyQt4.QtCore import * | ||
from qgis.core import * | ||
|
||
from processing.core.GeoAlgorithm import GeoAlgorithm | ||
from processing.core.GeoAlgorithmExecutionException import \ | ||
GeoAlgorithmExecutionException | ||
from processing.parameters.ParameterSelection import ParameterSelection | ||
from processing.parameters.ParameterVector import ParameterVector | ||
from processing.parameters.ParameterNumber import ParameterNumber | ||
from processing.outputs.OutputVector import OutputVector | ||
from processing.tools import dataobjects, vector | ||
|
||
|
||
class RandomExtract(GeoAlgorithm): | ||
|
||
INPUT = 'INPUT' | ||
OUTPUT = 'OUTPUT' | ||
METHOD = 'METHOD' | ||
NUMBER = 'NUMBER' | ||
|
||
METHODS = ['Number of selected features', | ||
'Percentage of selected features'] | ||
def defineCharacteristics(self): | ||
self.name = 'Random extract' | ||
self.group = 'Vector selection tools' | ||
|
||
self.addParameter(ParameterVector(self.INPUT, 'Input layer', | ||
[ParameterVector.VECTOR_TYPE_ANY])) | ||
self.addParameter(ParameterSelection(self.METHOD, 'Method', | ||
self.METHODS, 0)) | ||
self.addParameter(ParameterNumber(self.NUMBER, | ||
'Number/percentage of selected features', 0, None, | ||
10)) | ||
self.addOutput(OutputVector(self.OUTPUT, 'Selection')) | ||
|
||
def processAlgorithm(self, progress): | ||
filename = self.getParameterValue(self.INPUT) | ||
layer = dataobjects.getObjectFromUri(filename) | ||
method = self.getParameterValue(self.METHOD) | ||
|
||
features = vector.features(layer) | ||
featureCount = len(features) | ||
value = int(self.getParameterValue(self.NUMBER)) | ||
|
||
if method == 0: | ||
if value > featureCount: | ||
raise GeoAlgorithmExecutionException( | ||
'Selected number is greater than feature count. \ | ||
Choose a lower value and try again.') | ||
else: | ||
if value > 100: | ||
raise GeoAlgorithmExecutionException( | ||
"Percentage can't be greater than 100. Set a \ | ||
different value and try again.") | ||
value = int(round(value / 100.0000, 4) * featureCount) | ||
|
||
selran = random.sample(xrange(0, featureCount), value) | ||
|
||
output = self.getOutputFromName(self.OUTPUT) | ||
writer = output.getVectorWriter(layer.fields(), | ||
layer.geometryType(), layer.crs()) | ||
|
||
for (i, feat) in enumerate(features): | ||
if i in selran: | ||
writer.addFeature(feat) | ||
progress.setPercentage(100 * i / float(featureCount)) | ||
del writer |
134 changes: 134 additions & 0 deletions
134
python/plugins/processing/algs/ftools/RandomExtractWithinSubsets.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
*************************************************************************** | ||
RandomSelectionWithinSubsets.py | ||
--------------------- | ||
Date : August 2012 | ||
Copyright : (C) 2012 by Victor Olaya | ||
Email : volayaf 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__ = 'Victor Olaya' | ||
__date__ = 'August 2012' | ||
__copyright__ = '(C) 2012, Victor Olaya' | ||
|
||
# This will get replaced with a git SHA1 when you do a git archive | ||
|
||
__revision__ = '$Format:%H$' | ||
|
||
import random | ||
from PyQt4.QtCore import * | ||
from qgis.core import * | ||
from processing.core.GeoAlgorithm import GeoAlgorithm | ||
from processing.core.GeoAlgorithmExecutionException import \ | ||
GeoAlgorithmExecutionException | ||
from processing.parameters.ParameterSelection import ParameterSelection | ||
from processing.parameters.ParameterVector import ParameterVector | ||
from processing.parameters.ParameterNumber import ParameterNumber | ||
from processing.parameters.ParameterTableField import ParameterTableField | ||
from processing.outputs.OutputVector import OutputVector | ||
from processing.tools import dataobjects, vector | ||
|
||
|
||
class RandomExtractWithinSubsets(GeoAlgorithm): | ||
|
||
INPUT = 'INPUT' | ||
METHOD = 'METHOD' | ||
NUMBER = 'NUMBER' | ||
FIELD = 'FIELD' | ||
OUTPUT = 'OUTPUT' | ||
|
||
METHODS = ['Number of selected features', | ||
'Percentage of selected features'] | ||
|
||
def defineCharacteristics(self): | ||
self.name = 'Random extract within subsets' | ||
self.group = 'Vector selection tools' | ||
|
||
self.addParameter(ParameterVector(self.INPUT, 'Input layer', | ||
[ParameterVector.VECTOR_TYPE_ANY])) | ||
self.addParameter(ParameterTableField(self.FIELD, 'ID Field', | ||
self.INPUT)) | ||
self.addParameter(ParameterSelection(self.METHOD, 'Method', | ||
self.METHODS, 0)) | ||
self.addParameter(ParameterNumber(self.NUMBER, | ||
'Number/percentage of selected features', 1, None, | ||
10)) | ||
|
||
self.addOutput(OutputVector(self.OUTPUT, 'Selection')) | ||
|
||
def processAlgorithm(self, progress): | ||
filename = self.getParameterValue(self.INPUT) | ||
|
||
layer = dataobjects.getObjectFromUri(filename) | ||
field = self.getParameterValue(self.FIELD) | ||
method = self.getParameterValue(self.METHOD) | ||
|
||
index = layer.fieldNameIndex(field) | ||
|
||
features = vector.features(layer) | ||
featureCount = len(features) | ||
unique = vector.getUniqueValues(layer, index) | ||
value = int(self.getParameterValue(self.NUMBER)) | ||
if method == 0: | ||
if value > featureCount: | ||
raise GeoAlgorithmExecutionException( | ||
'Selected number is greater that feature count. \ | ||
Choose lesser value and try again.') | ||
else: | ||
if value > 100: | ||
raise GeoAlgorithmExecutionException( | ||
"Percentage can't be greater than 100. Set correct \ | ||
value and try again.") | ||
value = value / 100.0 | ||
|
||
|
||
output = self.getOutputFromName(self.OUTPUT) | ||
writer = output.getVectorWriter(layer.fields(), | ||
layer.geometryType(), layer.crs()) | ||
|
||
selran = [] | ||
current = 0 | ||
total = 100.0 / float(featureCount * len(unique)) | ||
features = vector.features(layer) | ||
|
||
if not len(unique) == featureCount: | ||
for classValue in unique: | ||
classFeatures = [] | ||
for i, feature in enumerate(features): | ||
attrs = feature.attributes() | ||
if attrs[index] == classValue: | ||
classFeatures.append(i) | ||
current += 1 | ||
progress.setPercentage(int(current * total)) | ||
|
||
if method == 1: | ||
selValue = int(round(value * len(classFeatures), 0)) | ||
else: | ||
selValue = value | ||
|
||
if selValue >= len(classFeatures): | ||
selFeat = classFeatures | ||
else: | ||
selFeat = random.sample(classFeatures, selValue) | ||
|
||
selran.extend(selFeat) | ||
else: | ||
selran = range(0, featureCount) | ||
|
||
|
||
features = vector.features(layer) | ||
for (i, feat) in enumerate(features): | ||
if i in selran: | ||
writer.addFeature(feat) | ||
progress.setPercentage(100 * i / float(featureCount)) | ||
del writer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.