Skip to content

Commit c494c47

Browse files
authoredOct 29, 2016
Merge pull request #3690 from nyalldawson/extract_by_exp
[FEATURE][processing] New extract by expression algorithm
2 parents 9166142 + 74e6464 commit c494c47

File tree

6 files changed

+151
-5
lines changed

6 files changed

+151
-5
lines changed
 

‎python/plugins/processing/algs/help/qgis.yaml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,15 @@ qgis:exportaddgeometrycolumns: >
159159
Depending on the geometry type of the vector layer, the attributes added to the table will be different.
160160

161161
qgis:extractbyattribute: >
162-
This algorithms creates new vector layer that only contain certain features from an input layer. The criteria for adding features to the resulting layer is defined based on the values of an attribute from the input layer.
162+
This algorithms creates a new vector layer that only contains matching features from an input layer. The criteria for adding features to the resulting layer is defined based on the values of an attribute from the input layer.
163+
164+
qgis:extractbyexpression: >
165+
This algorithms creates a new vector layer that only contains matching features from an input layer. The criteria for adding features to the resulting layer is based on a QGIS expression.
166+
167+
For more information about expressions see the <a href ="{qgisdocs}/user_manual/working_with_vector/expression.html">user manual</a>
163168

164169
qgis:extractbylocation: >
165-
This algorithms creates new vector layer that only contain certain features from an input layer. The criteria for adding features to the resulting layer is defined based on the spatial relationship between each feature and the features in an additional layer.
170+
This algorithms creates a new vector layer that only contains matching features from an input layer. The criteria for adding features to the resulting layer is defined based on the spatial relationship between each feature and the features in an additional layer.
166171

167172
qgis:extractnodes: >
168173
This algorithm takes a line or polygon layer and generates a point layer with points representing the nodes in the input lines or polygons. The attributes associated to each point are the same ones associated to the line or polygon that the point belongs to.
@@ -419,7 +424,7 @@ qgis:selectbyattributesum:
419424
qgis:selectbyexpression: >
420425
This algorithms creates a selection in a vector layer. The criteria for selecting features is based on a QGIS expression.
421426

422-
For more information about expressions see the<a href ="{qgisdocs}/user_manual/working_with_vector/expression.html">user manual</a>
427+
For more information about expressions see the <a href ="{qgisdocs}/user_manual/working_with_vector/expression.html">user manual</a>
423428

424429

425430
qgis:selectbylocation: >
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
ExtractByExpression.py
6+
---------------------
7+
Date : October 2016
8+
Copyright : (C) 2016 by Nyall Dawson
9+
***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************
17+
"""
18+
19+
__author__ = 'Nyall Dawson'
20+
__date__ = 'October 2016'
21+
__copyright__ = '(C) 2016, Nyall Dawson'
22+
23+
# This will get replaced with a git SHA1 when you do a git archive
24+
25+
__revision__ = '$Format:%H$'
26+
27+
import processing
28+
from qgis.core import QgsExpression, QgsVectorLayer, QgsFeatureRequest
29+
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
30+
from processing.core.parameters import ParameterVector
31+
from processing.core.outputs import OutputVector
32+
from processing.core.GeoAlgorithm import GeoAlgorithm
33+
from processing.core.parameters import ParameterString
34+
from processing.tools import dataobjects
35+
36+
37+
class ExtractByExpression(GeoAlgorithm):
38+
39+
INPUT = 'INPUT'
40+
EXPRESSION = 'EXPRESSION'
41+
OUTPUT = 'OUTPUT'
42+
43+
def defineCharacteristics(self):
44+
self.name, self.i18n_name = self.trAlgorithm('Extract by expression')
45+
self.group, self.i18n_group = self.trAlgorithm('Vector selection tools')
46+
47+
self.addParameter(ParameterVector(self.INPUT,
48+
self.tr('Input Layer')))
49+
self.addParameter(ParameterString(self.EXPRESSION,
50+
self.tr("Expression")))
51+
self.addOutput(OutputVector(self.OUTPUT, self.tr('Extracted (expression)')))
52+
53+
def processAlgorithm(self, progress):
54+
layer = dataobjects.getObjectFromUri(self.getParameterValue(self.INPUT))
55+
expression_string = self.getParameterValue(self.EXPRESSION)
56+
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(layer.fields(), layer.wkbType(), layer.crs())
57+
58+
expression = QgsExpression(expression_string)
59+
if not expression.hasParserError():
60+
req = QgsFeatureRequest().setFilterExpression(expression_string)
61+
else:
62+
raise GeoAlgorithmExecutionException(expression.parserErrorString())
63+
64+
for f in layer.getFeatures(req):
65+
writer.addFeature(f)
66+
67+
del writer

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
from .RandomExtract import RandomExtract
5757
from .RandomExtractWithinSubsets import RandomExtractWithinSubsets
5858
from .ExtractByLocation import ExtractByLocation
59+
from .ExtractByExpression import ExtractByExpression
5960
from .PointsInPolygon import PointsInPolygon
6061
from .PointsInPolygonUnique import PointsInPolygonUnique
6162
from .PointsInPolygonWeighted import PointsInPolygonWeighted
@@ -231,7 +232,7 @@ def __init__(self):
231232
ReliefAuto(), ZonalStatisticsQgis(),
232233
IdwInterpolationZValue(), IdwInterpolationAttribute(),
233234
TinInterpolationZValue(), TinInterpolationAttribute(),
234-
RemoveNullGeometry()
235+
RemoveNullGeometry(), ExtractByExpression()
235236
]
236237

237238
if hasMatplotlib:
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<GMLFeatureClassList>
2+
<GMLFeatureClass>
3+
<Name>extract_expression</Name>
4+
<ElementPath>extract_expression</ElementPath>
5+
<!--POLYGON-->
6+
<GeometryType>3</GeometryType>
7+
<SRSName>EPSG:4326</SRSName>
8+
<DatasetSpecificInfo>
9+
<FeatureCount>2</FeatureCount>
10+
<ExtentXMin>4.00000</ExtentXMin>
11+
<ExtentXMax>10.00000</ExtentXMax>
12+
<ExtentYMin>-3.00000</ExtentYMin>
13+
<ExtentYMax>5.00000</ExtentYMax>
14+
</DatasetSpecificInfo>
15+
<PropertyDefn>
16+
<Name>name</Name>
17+
<ElementPath>name</ElementPath>
18+
<Type>String</Type>
19+
<Width>5</Width>
20+
</PropertyDefn>
21+
<PropertyDefn>
22+
<Name>intval</Name>
23+
<ElementPath>intval</ElementPath>
24+
<Type>Integer</Type>
25+
</PropertyDefn>
26+
<PropertyDefn>
27+
<Name>floatval</Name>
28+
<ElementPath>floatval</ElementPath>
29+
<Type>Integer</Type>
30+
</PropertyDefn>
31+
</GMLFeatureClass>
32+
</GMLFeatureClassList>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ogr:FeatureCollection
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation=""
5+
xmlns:ogr="http://ogr.maptools.org/"
6+
xmlns:gml="http://www.opengis.net/gml">
7+
<gml:boundedBy>
8+
<gml:Box>
9+
<gml:coord><gml:X>4</gml:X><gml:Y>-3</gml:Y></gml:coord>
10+
<gml:coord><gml:X>10</gml:X><gml:Y>5</gml:Y></gml:coord>
11+
</gml:Box>
12+
</gml:boundedBy>
13+
14+
<gml:featureMember>
15+
<ogr:extract_expression fid="polys.1">
16+
<ogr:geometryProperty><gml:Polygon srsName="EPSG:4326"><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>5,5 6,4 4,4 5,5</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></ogr:geometryProperty>
17+
<ogr:name>Aaaaa</ogr:name>
18+
<ogr:intval>-33</ogr:intval>
19+
<ogr:floatval>0</ogr:floatval>
20+
</ogr:extract_expression>
21+
</gml:featureMember>
22+
<gml:featureMember>
23+
<ogr:extract_expression fid="polys.3">
24+
<ogr:geometryProperty><gml:Polygon srsName="EPSG:4326"><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>6,1 10,1 10,-3 6,-3 6,1</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs><gml:innerBoundaryIs><gml:LinearRing><gml:coordinates>7,0 7,-2 9,-2 9,0 7,0</gml:coordinates></gml:LinearRing></gml:innerBoundaryIs></gml:Polygon></ogr:geometryProperty>
25+
<ogr:name>ASDF</ogr:name>
26+
<ogr:intval>0</ogr:intval>
27+
</ogr:extract_expression>
28+
</gml:featureMember>
29+
</ogr:FeatureCollection>

‎python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1212,4 +1212,16 @@ tests:
12121212
results:
12131213
OUTPUT_LAYER:
12141214
name: expected/remove_null_polys.gml
1215-
type: vector
1215+
type: vector
1216+
1217+
- algorithm: qgis:extractbyexpression
1218+
name: Extract by Expression
1219+
params:
1220+
EXPRESSION: left( "Name",1)='A'
1221+
INPUT:
1222+
name: polys.gml
1223+
type: vector
1224+
results:
1225+
OUTPUT:
1226+
name: expected/extract_expression.gml
1227+
type: vector

0 commit comments

Comments
 (0)
Please sign in to comment.