From 5eb875d63bd761cab63220a72444d79e26a8bb99 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Mon, 24 Oct 2016 11:07:00 +1000 Subject: [PATCH] [processing] Add algorithm to strip null geometries --- python/plugins/processing/algs/help/qgis.yaml | 2 + .../algs/qgis/QGISAlgorithmProvider.py | 5 +- .../algs/qgis/RemoveNullGeometry.py | 65 +++++++++++++++++++ .../testdata/expected/remove_null_polys.gfs | 31 +++++++++ .../testdata/expected/remove_null_polys.gml | 52 +++++++++++++++ .../tests/testdata/qgis_algorithm_tests.yaml | 11 ++++ 6 files changed, 164 insertions(+), 2 deletions(-) create mode 100644 python/plugins/processing/algs/qgis/RemoveNullGeometry.py create mode 100644 python/plugins/processing/tests/testdata/expected/remove_null_polys.gfs create mode 100644 python/plugins/processing/tests/testdata/expected/remove_null_polys.gml diff --git a/python/plugins/processing/algs/help/qgis.yaml b/python/plugins/processing/algs/help/qgis.yaml index 27d5518a25e8..bc1d4b3a2d1b 100644 --- a/python/plugins/processing/algs/help/qgis.yaml +++ b/python/plugins/processing/algs/help/qgis.yaml @@ -397,6 +397,8 @@ qgis:refactorfields: > qgis:regularpoints: +qgis:removenullgeometries: > + This algorithms removes any features which do not have a geometry from a vector layer. All other features will be copied unchanged. qgis:reprojectlayer: > This algorithm reprojects a vector layer. It creates a new layer with the same features as the input one, but with geometries reprojected to a new CRS. diff --git a/python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py b/python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py index 9203fcd402d1..9cb4a1cab645 100644 --- a/python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py +++ b/python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py @@ -169,7 +169,7 @@ from .TinInterpolationZValue import TinInterpolationZValue from .TinInterpolationAttribute import TinInterpolationAttribute from .ZonalStatisticsQgis import ZonalStatisticsQgis - +from .RemoveNullGeometry import RemoveNullGeometry pluginPath = os.path.normpath(os.path.join( os.path.split(os.path.dirname(__file__))[0], os.pardir)) @@ -230,7 +230,8 @@ def __init__(self): Aspect(), Slope(), Ruggedness(), Hillshade(), ReliefAuto(), ZonalStatisticsQgis(), IdwInterpolationZValue(), IdwInterpolationAttribute(), - TinInterpolationZValue(), TinInterpolationAttribute() + TinInterpolationZValue(), TinInterpolationAttribute(), + RemoveNullGeometry() ] if hasMatplotlib: diff --git a/python/plugins/processing/algs/qgis/RemoveNullGeometry.py b/python/plugins/processing/algs/qgis/RemoveNullGeometry.py new file mode 100644 index 000000000000..eca461ed0010 --- /dev/null +++ b/python/plugins/processing/algs/qgis/RemoveNullGeometry.py @@ -0,0 +1,65 @@ +# -*- coding: utf-8 -*- + +""" +*************************************************************************** + RemoveNullGeometry.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 processing.core.GeoAlgorithm import GeoAlgorithm +from processing.core.parameters import ParameterVector +from processing.core.outputs import OutputVector +from processing.tools import dataobjects, vector + + +class RemoveNullGeometry(GeoAlgorithm): + + INPUT_LAYER = 'INPUT_LAYER' + OUTPUT_LAYER = 'OUTPUT_LAYER' + + def defineCharacteristics(self): + self.name, self.i18n_name = self.trAlgorithm('Remove null geometries') + self.group, self.i18n_group = self.trAlgorithm('Vector selection tools') + + self.addParameter(ParameterVector(self.INPUT_LAYER, + self.tr('Input layer'), [dataobjects.TYPE_VECTOR_ANY])) + self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Removed null geometry'))) + + def processAlgorithm(self, progress): + layer = dataobjects.getObjectFromUri( + self.getParameterValue(self.INPUT_LAYER)) + writer = self.getOutputFromName( + self.OUTPUT_LAYER).getVectorWriter( + layer.fields(), + layer.wkbType(), + layer.crs()) + + features = vector.features(layer) + total = 100.0 / len(features) + + for current, input_feature in enumerate(features): + if input_feature.hasGeometry(): + writer.addFeature(input_feature) + + progress.setPercentage(int(current * total)) + + del writer diff --git a/python/plugins/processing/tests/testdata/expected/remove_null_polys.gfs b/python/plugins/processing/tests/testdata/expected/remove_null_polys.gfs new file mode 100644 index 000000000000..9fcc91606be3 --- /dev/null +++ b/python/plugins/processing/tests/testdata/expected/remove_null_polys.gfs @@ -0,0 +1,31 @@ + + + remove_null_polys + remove_null_polys + 3 + EPSG:4326 + + 5 + -1.00000 + 10.00000 + -3.00000 + 6.00000 + + + name + name + String + 5 + + + intval + intval + Integer + + + floatval + floatval + Real + + + diff --git a/python/plugins/processing/tests/testdata/expected/remove_null_polys.gml b/python/plugins/processing/tests/testdata/expected/remove_null_polys.gml new file mode 100644 index 000000000000..68b92d965fe1 --- /dev/null +++ b/python/plugins/processing/tests/testdata/expected/remove_null_polys.gml @@ -0,0 +1,52 @@ + + + + + -1-3 + 106 + + + + + + -1,-1 -1,3 3,3 3,2 2,2 2,-1 -1,-1 + aaaaa + 33 + 44.123456 + + + + + 5,5 6,4 4,4 5,5 + Aaaaa + -33 + 0 + + + + + 2,5 2,6 3,6 3,5 2,5 + bbaaa + 0.123 + + + + + 6,1 10,1 10,-3 6,-3 6,17,0 7,-2 9,-2 9,0 7,0 + ASDF + 0 + + + + + 3,2 6,1 6,-3 2,-1 2,2 3,2 + elim + 2 + 3.33 + + + diff --git a/python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml b/python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml index f3a13971f3ea..ea2bc5dd3ca9 100644 --- a/python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml +++ b/python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml @@ -1202,3 +1202,14 @@ tests: #TRIANULATION_FILE: # name: expected/triangulation.gml # type: vector + + - algorithm: qgis:removenullgeometries + name: Remove null geometries + params: + INPUT_LAYER: + name: polys.gml + type: vector + results: + OUTPUT_LAYER: + name: expected/remove_null_polys.gml + type: vector \ No newline at end of file