Skip to content

Commit

Permalink
[processing] added extract algorithm as alternative to selection algo…
Browse files Browse the repository at this point in the history
…rithms that can be used in the modeler
  • Loading branch information
volaya committed Oct 7, 2013
1 parent 0150fe7 commit cecfff0
Show file tree
Hide file tree
Showing 7 changed files with 488 additions and 175 deletions.
12 changes: 10 additions & 2 deletions python/plugins/processing/algs/QGISAlgorithmProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
__revision__ = '$Format:%H$'

from PyQt4.QtGui import *
from processing.algs.ftools.RandomExtract import RandomExtract
from processing.algs.ftools.RandomExtractWithinSubsets import \
RandomExtractWithinSubsets
from processing.algs.ftools.ExtractByLocation import ExtractByLocation

from processing.core.AlgorithmProvider import AlgorithmProvider
from processing.algs.ftools.PointsInPolygon import PointsInPolygon
from processing.algs.ftools.PointsInPolygonUnique import PointsInPolygonUnique
Expand Down Expand Up @@ -80,7 +85,8 @@
mmqgisx_geometry_convert_algorithm, mmqgisx_grid_algorithm, \
mmqgisx_gridify_algorithm, mmqgisx_hub_distance_algorithm, \
mmqgisx_hub_lines_algorithm, mmqgisx_merge_algorithm, \
mmqgisx_select_algorithm, mmqgisx_text_to_float_algorithm
mmqgisx_select_algorithm, mmqgisx_text_to_float_algorithm,\
mmqgisx_extract_algorithm

from processing.algs.Polygonize import Polygonize
from processing.algs.RasterLayerStatistics import RasterLayerStatistics
Expand Down Expand Up @@ -128,7 +134,8 @@ def __init__(self):
VariableDistanceBuffer(), Dissolve(), Difference(),
Intersection(), Union(), Clip(), ExtentFromLayer(),
RandomSelection(), RandomSelectionWithinSubsets(),
SelectByLocation(),
SelectByLocation(), RandomExtract(), RandomExtractWithinSubsets(),
ExtractByLocation(),
# ------ mmqgisx ------
mmqgisx_delete_columns_algorithm(),
mmqgisx_delete_duplicate_geometries_algorithm(),
Expand All @@ -139,6 +146,7 @@ def __init__(self):
mmqgisx_hub_lines_algorithm(),
mmqgisx_merge_algorithm(),
mmqgisx_select_algorithm(),
mmqgisx_extract_algorithm(),
mmqgisx_text_to_float_algorithm(),
# ------ native algs ------
AddTableField(), FieldsCalculator(),
Expand Down
84 changes: 84 additions & 0 deletions python/plugins/processing/algs/ftools/ExtractByLocation.py
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
96 changes: 96 additions & 0 deletions python/plugins/processing/algs/ftools/RandomExtract.py
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 python/plugins/processing/algs/ftools/RandomExtractWithinSubsets.py
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
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,6 @@ class RandomSelectionWithinSubsets(GeoAlgorithm):
METHODS = ['Number of selected features',
'Percentage of selected features']

# =========================================================================
# def getIcon(self):
# return QIcon(os.path.dirname(__file__) + \
# "/icons/random_selection.png")
# =========================================================================

def defineCharacteristics(self):
self.allowOnlyOpenedLayers = True
self.name = 'Random selection within subsets'
Expand Down
9 changes: 2 additions & 7 deletions python/plugins/processing/algs/ftools/SelectByLocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ class SelectByLocation(GeoAlgorithm):
METHODS = ['creating new selection', 'adding to current selection',
'removing from current selection']

# =========================================================================
# def getIcon(self):
# return QIcon(os.path.dirname(__file__) + "/icons/select_location.png")
# =========================================================================

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

oldSelection = set(inputLayer.selectedFeaturesIds())
index = spatialIndex = vector.spatialindex(inputLayer)

feat = QgsFeature()
index = vector.spatialindex(inputLayer)

geom = QgsGeometry()
selectedSet = []
current = 0
Expand Down
Loading

0 comments on commit cecfff0

Please sign in to comment.