Skip to content
Permalink
Browse files

[FEATURE[processing] New algorithm to compute geometry by expression

This algorithm updates existing geometries (or creates new
geometries) for input features by use of a QGIS expression. This
allows complex geometry modifications which can utilise all the
flexibility of the QGIS expression engine to manipulate and create
geometries for output features.
  • Loading branch information
nyalldawson committed Nov 2, 2016
1 parent 5e3bef7 commit f65e770242d2e5e55167934f0a1a191726c01104
@@ -201,6 +201,11 @@ qgis:fixeddistancebuffer: >
qgis:frequencyanalysis: >
This algorithms generates a table with frequency analysis of the values of a selected attribute from an input vector layer

qgis:geometrybyexpression: >
This algorithm updates existing geometries (or creates new geometries) for input features by use of a QGIS expression. This allows complex geometry modifications which can utilise all the flexibility of the QGIS expression engine to manipulate and create geometries for output features.

For help with QGIS expression functions, see the inbuilt help for specific functions which is available in the expression builder.

qgis:generatepointspixelcentroidsalongline:


@@ -0,0 +1,129 @@
# -*- coding: utf-8 -*-

"""
***************************************************************************
GeometryByExpression.py
-----------------------
Date : October 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__ = 'October 2016'
__copyright__ = '(C) 2016, Nyall Dawson'

# This will get replaced with a git SHA1 when you do a git archive323

__revision__ = '$Format:%H$'

from qgis.core import QgsWkbTypes, QgsExpression, QgsExpressionContext, QgsExpressionContextUtils, QgsGeometry

from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.ProcessingLog import ProcessingLog
from processing.core.parameters import ParameterVector, ParameterSelection, ParameterBoolean, ParameterString
from processing.core.outputs import OutputVector
from processing.tools import dataobjects, vector


class GeometryByExpression(GeoAlgorithm):

INPUT_LAYER = 'INPUT_LAYER'
OUTPUT_LAYER = 'OUTPUT_LAYER'
OUTPUT_GEOMETRY = 'OUTPUT_GEOMETRY'
WITH_Z = 'WITH_Z'
WITH_M = 'WITH_M'
EXPRESSION = 'EXPRESSION'

def defineCharacteristics(self):
self.name, self.i18n_name = self.trAlgorithm('Geometry by expression')
self.group, self.i18n_group = self.trAlgorithm('Vector geometry tools')

self.addParameter(ParameterVector(self.INPUT_LAYER,
self.tr('Input layer')))

self.geometry_types = [self.tr('Polygon'),
'Line',
'Point']
self.addParameter(ParameterSelection(
self.OUTPUT_GEOMETRY,
self.tr('Output geometry type'),
self.geometry_types, default=0))
self.addParameter(ParameterBoolean(self.WITH_Z,
self.tr('Output geometry has z dimension'), False))
self.addParameter(ParameterBoolean(self.WITH_M,
self.tr('Output geometry has m values'), False))

self.addParameter(ParameterString(self.EXPRESSION,
self.tr("Geometry expression"), '$geometry'))

self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Modified geometry')))

def processAlgorithm(self, progress):
layer = dataobjects.getObjectFromUri(
self.getParameterValue(self.INPUT_LAYER))

geometry_type = self.getParameterValue(self.OUTPUT_GEOMETRY)
wkb_type = None
if geometry_type == 0:
wkb_type = QgsWkbTypes.Polygon
elif geometry_type == 1:
wkb_type = QgsWkbTypes.LineString
else:
wkb_type = QgsWkbTypes.Point
if self.getParameterValue(self.WITH_Z):
wkb_type = QgsWkbTypes.addZ(wkb_type)
if self.getParameterValue(self.WITH_M):
wkb_type = QgsWkbTypes.addM(wkb_type)

writer = self.getOutputFromName(
self.OUTPUT_LAYER).getVectorWriter(
layer.fields(),
wkb_type,
layer.crs())

expression = QgsExpression(self.getParameterValue(self.EXPRESSION))
if expression.hasParserError():
raise GeoAlgorithmExecutionException(expression.parserErrorString())

exp_context = QgsExpressionContext()
exp_context.appendScope(QgsExpressionContextUtils.globalScope())
exp_context.appendScope(QgsExpressionContextUtils.projectScope())
exp_context.appendScope(QgsExpressionContextUtils.layerScope(layer))

if not expression.prepare(exp_context):
raise GeoAlgorithmExecutionException(
self.tr('Evaluation error: %s' % expression.evalErrorString()))

features = vector.features(layer)
total = 100.0 / len(features)
for current, input_feature in enumerate(features):
output_feature = input_feature

exp_context.setFeature(input_feature)
value = expression.evaluate(exp_context)
if expression.hasEvalError():
raise GeoAlgorithmExecutionException(
self.tr('Evaluation error: %s' % expression.evalErrorString()))

if not value:
output_feature.setGeometry(QgsGeometry())
else:
if not isinstance(value, QgsGeometry):
raise GeoAlgorithmExecutionException(
self.tr('{} is not a geometry').format(value))
output_feature.setGeometry(value)

writer.addFeature(output_feature)
progress.setPercentage(int(current * total))

del writer
@@ -174,6 +174,7 @@
from .RemoveNullGeometry import RemoveNullGeometry
from .ExtendLines import ExtendLines
from .ExtractSpecificNodes import ExtractSpecificNodes
from .GeometryByExpression import GeometryByExpression

pluginPath = os.path.normpath(os.path.join(
os.path.split(os.path.dirname(__file__))[0], os.pardir))
@@ -236,7 +237,7 @@ def __init__(self):
IdwInterpolationZValue(), IdwInterpolationAttribute(),
TinInterpolationZValue(), TinInterpolationAttribute(),
RemoveNullGeometry(), ExtractByExpression(), ExtendLines(),
ExtractSpecificNodes()
ExtractSpecificNodes(), GeometryByExpression()
]

if hasMatplotlib:
@@ -0,0 +1,16 @@
<GMLFeatureClassList>
<GMLFeatureClass>
<Name>geometry_by_expression_line</Name>
<ElementPath>geometry_by_expression_line</ElementPath>
<!--LINESTRING-->
<GeometryType>2</GeometryType>
<SRSName>EPSG:4326</SRSName>
<DatasetSpecificInfo>
<FeatureCount>7</FeatureCount>
<ExtentXMin>0.00000</ExtentXMin>
<ExtentXMax>12.00000</ExtentXMax>
<ExtentYMin>-2.00000</ExtentYMin>
<ExtentYMax>6.00000</ExtentYMax>
</DatasetSpecificInfo>
</GMLFeatureClass>
</GMLFeatureClassList>
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="utf-8" ?>
<ogr:FeatureCollection
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=""
xmlns:ogr="http://ogr.maptools.org/"
xmlns:gml="http://www.opengis.net/gml">
<gml:boundedBy>
<gml:Box>
<gml:coord><gml:X>0</gml:X><gml:Y>-2</gml:Y></gml:coord>
<gml:coord><gml:X>12</gml:X><gml:Y>6</gml:Y></gml:coord>
</gml:Box>
</gml:boundedBy>

<gml:featureMember>
<ogr:geometry_by_expression_line fid="lines.0">
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>7,3 10,3 10,4 12,6</gml:coordinates></gml:LineString></ogr:geometryProperty>
</ogr:geometry_by_expression_line>
</gml:featureMember>
<gml:featureMember>
<ogr:geometry_by_expression_line fid="lines.1">
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>0,0 2,0</gml:coordinates></gml:LineString></ogr:geometryProperty>
</ogr:geometry_by_expression_line>
</gml:featureMember>
<gml:featureMember>
<ogr:geometry_by_expression_line fid="lines.2">
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>3,1 3,3 4,3 4,4</gml:coordinates></gml:LineString></ogr:geometryProperty>
</ogr:geometry_by_expression_line>
</gml:featureMember>
<gml:featureMember>
<ogr:geometry_by_expression_line fid="lines.3">
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>4,2 6,2</gml:coordinates></gml:LineString></ogr:geometryProperty>
</ogr:geometry_by_expression_line>
</gml:featureMember>
<gml:featureMember>
<ogr:geometry_by_expression_line fid="lines.4">
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>8,-2 11,-2</gml:coordinates></gml:LineString></ogr:geometryProperty>
</ogr:geometry_by_expression_line>
</gml:featureMember>
<gml:featureMember>
<ogr:geometry_by_expression_line fid="lines.5">
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>7,-2 11,2</gml:coordinates></gml:LineString></ogr:geometryProperty>
</ogr:geometry_by_expression_line>
</gml:featureMember>
<gml:featureMember>
<ogr:geometry_by_expression_line fid="lines.6">
</ogr:geometry_by_expression_line>
</gml:featureMember>
</ogr:FeatureCollection>
@@ -0,0 +1,16 @@
<GMLFeatureClassList>
<GMLFeatureClass>
<Name>geometry_by_expression_point</Name>
<ElementPath>geometry_by_expression_point</ElementPath>
<!--POINT-->
<GeometryType>1</GeometryType>
<SRSName>EPSG:4326</SRSName>
<DatasetSpecificInfo>
<FeatureCount>9</FeatureCount>
<ExtentXMin>1.00000</ExtentXMin>
<ExtentXMax>9.00000</ExtentXMax>
<ExtentYMin>-4.00000</ExtentYMin>
<ExtentYMax>4.00000</ExtentYMax>
</DatasetSpecificInfo>
</GMLFeatureClass>
</GMLFeatureClassList>
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="utf-8" ?>
<ogr:FeatureCollection
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=""
xmlns:ogr="http://ogr.maptools.org/"
xmlns:gml="http://www.opengis.net/gml">
<gml:boundedBy>
<gml:Box>
<gml:coord><gml:X>1</gml:X><gml:Y>-4</gml:Y></gml:coord>
<gml:coord><gml:X>9</gml:X><gml:Y>4</gml:Y></gml:coord>
</gml:Box>
</gml:boundedBy>

<gml:featureMember>
<ogr:geometry_by_expression_point fid="points.0">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>2,2</gml:coordinates></gml:Point></ogr:geometryProperty>
</ogr:geometry_by_expression_point>
</gml:featureMember>
<gml:featureMember>
<ogr:geometry_by_expression_point fid="points.1">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>4,4</gml:coordinates></gml:Point></ogr:geometryProperty>
</ogr:geometry_by_expression_point>
</gml:featureMember>
<gml:featureMember>
<ogr:geometry_by_expression_point fid="points.2">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>3,3</gml:coordinates></gml:Point></ogr:geometryProperty>
</ogr:geometry_by_expression_point>
</gml:featureMember>
<gml:featureMember>
<ogr:geometry_by_expression_point fid="points.3">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>6,3</gml:coordinates></gml:Point></ogr:geometryProperty>
</ogr:geometry_by_expression_point>
</gml:featureMember>
<gml:featureMember>
<ogr:geometry_by_expression_point fid="points.4">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>5,2</gml:coordinates></gml:Point></ogr:geometryProperty>
</ogr:geometry_by_expression_point>
</gml:featureMember>
<gml:featureMember>
<ogr:geometry_by_expression_point fid="points.5">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>1,-4</gml:coordinates></gml:Point></ogr:geometryProperty>
</ogr:geometry_by_expression_point>
</gml:featureMember>
<gml:featureMember>
<ogr:geometry_by_expression_point fid="points.6">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>9,0</gml:coordinates></gml:Point></ogr:geometryProperty>
</ogr:geometry_by_expression_point>
</gml:featureMember>
<gml:featureMember>
<ogr:geometry_by_expression_point fid="points.7">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>8,0</gml:coordinates></gml:Point></ogr:geometryProperty>
</ogr:geometry_by_expression_point>
</gml:featureMember>
<gml:featureMember>
<ogr:geometry_by_expression_point fid="points.8">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>1,0</gml:coordinates></gml:Point></ogr:geometryProperty>
</ogr:geometry_by_expression_point>
</gml:featureMember>
</ogr:FeatureCollection>
@@ -0,0 +1,32 @@
<GMLFeatureClassList>
<GMLFeatureClass>
<Name>geometry_by_expression_poly</Name>
<ElementPath>geometry_by_expression_poly</ElementPath>
<!--POINT-->
<GeometryType>1</GeometryType>
<SRSName>EPSG:4326</SRSName>
<DatasetSpecificInfo>
<FeatureCount>6</FeatureCount>
<ExtentXMin>1.65385</ExtentXMin>
<ExtentXMax>9.00000</ExtentXMax>
<ExtentYMin>0.00000</ExtentYMin>
<ExtentYMax>6.50000</ExtentYMax>
</DatasetSpecificInfo>
<PropertyDefn>
<Name>name</Name>
<ElementPath>name</ElementPath>
<Type>String</Type>
<Width>5</Width>
</PropertyDefn>
<PropertyDefn>
<Name>intval</Name>
<ElementPath>intval</ElementPath>
<Type>Integer</Type>
</PropertyDefn>
<PropertyDefn>
<Name>floatval</Name>
<ElementPath>floatval</ElementPath>
<Type>Real</Type>
</PropertyDefn>
</GMLFeatureClass>
</GMLFeatureClassList>
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8" ?>
<ogr:FeatureCollection
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=""
xmlns:ogr="http://ogr.maptools.org/"
xmlns:gml="http://www.opengis.net/gml">
<gml:boundedBy>
<gml:Box>
<gml:coord><gml:X>1.653846153846154</gml:X><gml:Y>0</gml:Y></gml:coord>
<gml:coord><gml:X>9</gml:X><gml:Y>6.5</gml:Y></gml:coord>
</gml:Box>
</gml:boundedBy>

<gml:featureMember>
<ogr:geometry_by_expression_poly fid="polys.0">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>1.653846153846154,2.115384615384615</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:name>aaaaa</ogr:name>
<ogr:intval>33</ogr:intval>
<ogr:floatval>44.123456</ogr:floatval>
</ogr:geometry_by_expression_poly>
</gml:featureMember>
<gml:featureMember>
<ogr:geometry_by_expression_poly fid="polys.1">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>6.0,5.333333333333333</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:name>Aaaaa</ogr:name>
<ogr:intval>-33</ogr:intval>
<ogr:floatval>0</ogr:floatval>
</ogr:geometry_by_expression_poly>
</gml:featureMember>
<gml:featureMember>
<ogr:geometry_by_expression_poly fid="polys.2">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>3.5,6.5</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:name>bbaaa</ogr:name>
<ogr:floatval>0.123</ogr:floatval>
</ogr:geometry_by_expression_poly>
</gml:featureMember>
<gml:featureMember>
<ogr:geometry_by_expression_poly fid="polys.3">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>9,0</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:name>ASDF</ogr:name>
<ogr:intval>0</ogr:intval>
</ogr:geometry_by_expression_poly>
</gml:featureMember>
<gml:featureMember>
<ogr:geometry_by_expression_poly fid="polys.4">
<ogr:intval>120</ogr:intval>
<ogr:floatval>-100291.43213</ogr:floatval>
</ogr:geometry_by_expression_poly>
</gml:featureMember>
<gml:featureMember>
<ogr:geometry_by_expression_poly fid="polys.5">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>5.080459770114943,0.781609195402299</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:name>elim</ogr:name>
<ogr:intval>2</ogr:intval>
<ogr:floatval>3.33</ogr:floatval>
</ogr:geometry_by_expression_poly>
</gml:featureMember>
</ogr:FeatureCollection>

0 comments on commit f65e770

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