Skip to content

Commit 5eb875d

Browse files
committed
[processing] Add algorithm to strip null geometries
1 parent 12a7bb3 commit 5eb875d

File tree

6 files changed

+164
-2
lines changed

6 files changed

+164
-2
lines changed

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

+2
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,8 @@ qgis:refactorfields: >
397397

398398
qgis:regularpoints:
399399

400+
qgis:removenullgeometries: >
401+
This algorithms removes any features which do not have a geometry from a vector layer. All other features will be copied unchanged.
400402

401403
qgis:reprojectlayer: >
402404
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.

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@
169169
from .TinInterpolationZValue import TinInterpolationZValue
170170
from .TinInterpolationAttribute import TinInterpolationAttribute
171171
from .ZonalStatisticsQgis import ZonalStatisticsQgis
172-
172+
from .RemoveNullGeometry import RemoveNullGeometry
173173

174174
pluginPath = os.path.normpath(os.path.join(
175175
os.path.split(os.path.dirname(__file__))[0], os.pardir))
@@ -230,7 +230,8 @@ def __init__(self):
230230
Aspect(), Slope(), Ruggedness(), Hillshade(),
231231
ReliefAuto(), ZonalStatisticsQgis(),
232232
IdwInterpolationZValue(), IdwInterpolationAttribute(),
233-
TinInterpolationZValue(), TinInterpolationAttribute()
233+
TinInterpolationZValue(), TinInterpolationAttribute(),
234+
RemoveNullGeometry()
234235
]
235236

236237
if hasMatplotlib:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
RemoveNullGeometry.py
6+
--------------
7+
Date : October 2016
8+
Copyright : (C) 2016 by Nyall Dawson
9+
Email : nyall dot dawson at gmail dot com
10+
***************************************************************************
11+
* *
12+
* This program is free software; you can redistribute it and/or modify *
13+
* it under the terms of the GNU General Public License as published by *
14+
* the Free Software Foundation; either version 2 of the License, or *
15+
* (at your option) any later version. *
16+
* *
17+
***************************************************************************
18+
"""
19+
20+
__author__ = 'Nyall Dawson'
21+
__date__ = 'October 2016'
22+
__copyright__ = '(C) 2016, Nyall Dawson'
23+
24+
# This will get replaced with a git SHA1 when you do a git archive323
25+
26+
__revision__ = '$Format:%H$'
27+
28+
from processing.core.GeoAlgorithm import GeoAlgorithm
29+
from processing.core.parameters import ParameterVector
30+
from processing.core.outputs import OutputVector
31+
from processing.tools import dataobjects, vector
32+
33+
34+
class RemoveNullGeometry(GeoAlgorithm):
35+
36+
INPUT_LAYER = 'INPUT_LAYER'
37+
OUTPUT_LAYER = 'OUTPUT_LAYER'
38+
39+
def defineCharacteristics(self):
40+
self.name, self.i18n_name = self.trAlgorithm('Remove null geometries')
41+
self.group, self.i18n_group = self.trAlgorithm('Vector selection tools')
42+
43+
self.addParameter(ParameterVector(self.INPUT_LAYER,
44+
self.tr('Input layer'), [dataobjects.TYPE_VECTOR_ANY]))
45+
self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Removed null geometry')))
46+
47+
def processAlgorithm(self, progress):
48+
layer = dataobjects.getObjectFromUri(
49+
self.getParameterValue(self.INPUT_LAYER))
50+
writer = self.getOutputFromName(
51+
self.OUTPUT_LAYER).getVectorWriter(
52+
layer.fields(),
53+
layer.wkbType(),
54+
layer.crs())
55+
56+
features = vector.features(layer)
57+
total = 100.0 / len(features)
58+
59+
for current, input_feature in enumerate(features):
60+
if input_feature.hasGeometry():
61+
writer.addFeature(input_feature)
62+
63+
progress.setPercentage(int(current * total))
64+
65+
del writer
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<GMLFeatureClassList>
2+
<GMLFeatureClass>
3+
<Name>remove_null_polys</Name>
4+
<ElementPath>remove_null_polys</ElementPath>
5+
<GeometryType>3</GeometryType>
6+
<SRSName>EPSG:4326</SRSName>
7+
<DatasetSpecificInfo>
8+
<FeatureCount>5</FeatureCount>
9+
<ExtentXMin>-1.00000</ExtentXMin>
10+
<ExtentXMax>10.00000</ExtentXMax>
11+
<ExtentYMin>-3.00000</ExtentYMin>
12+
<ExtentYMax>6.00000</ExtentYMax>
13+
</DatasetSpecificInfo>
14+
<PropertyDefn>
15+
<Name>name</Name>
16+
<ElementPath>name</ElementPath>
17+
<Type>String</Type>
18+
<Width>5</Width>
19+
</PropertyDefn>
20+
<PropertyDefn>
21+
<Name>intval</Name>
22+
<ElementPath>intval</ElementPath>
23+
<Type>Integer</Type>
24+
</PropertyDefn>
25+
<PropertyDefn>
26+
<Name>floatval</Name>
27+
<ElementPath>floatval</ElementPath>
28+
<Type>Real</Type>
29+
</PropertyDefn>
30+
</GMLFeatureClass>
31+
</GMLFeatureClassList>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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>-1</gml:X><gml:Y>-3</gml:Y></gml:coord>
10+
<gml:coord><gml:X>10</gml:X><gml:Y>6</gml:Y></gml:coord>
11+
</gml:Box>
12+
</gml:boundedBy>
13+
14+
<gml:featureMember>
15+
<ogr:remove_null_polys fid="polys.0">
16+
<ogr:geometryProperty><gml:Polygon srsName="EPSG:4326"><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>-1,-1 -1,3 3,3 3,2 2,2 2,-1 -1,-1</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>44.123456</ogr:floatval>
20+
</ogr:remove_null_polys>
21+
</gml:featureMember>
22+
<gml:featureMember>
23+
<ogr:remove_null_polys fid="polys.1">
24+
<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>
25+
<ogr:name>Aaaaa</ogr:name>
26+
<ogr:intval>-33</ogr:intval>
27+
<ogr:floatval>0</ogr:floatval>
28+
</ogr:remove_null_polys>
29+
</gml:featureMember>
30+
<gml:featureMember>
31+
<ogr:remove_null_polys fid="polys.2">
32+
<ogr:geometryProperty><gml:Polygon srsName="EPSG:4326"><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>2,5 2,6 3,6 3,5 2,5</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></ogr:geometryProperty>
33+
<ogr:name>bbaaa</ogr:name>
34+
<ogr:floatval>0.123</ogr:floatval>
35+
</ogr:remove_null_polys>
36+
</gml:featureMember>
37+
<gml:featureMember>
38+
<ogr:remove_null_polys fid="polys.3">
39+
<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>
40+
<ogr:name>ASDF</ogr:name>
41+
<ogr:intval>0</ogr:intval>
42+
</ogr:remove_null_polys>
43+
</gml:featureMember>
44+
<gml:featureMember>
45+
<ogr:remove_null_polys fid="polys.5">
46+
<ogr:geometryProperty><gml:Polygon srsName="EPSG:4326"><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>3,2 6,1 6,-3 2,-1 2,2 3,2</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></ogr:geometryProperty>
47+
<ogr:name>elim</ogr:name>
48+
<ogr:intval>2</ogr:intval>
49+
<ogr:floatval>3.33</ogr:floatval>
50+
</ogr:remove_null_polys>
51+
</gml:featureMember>
52+
</ogr:FeatureCollection>

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

+11
Original file line numberDiff line numberDiff line change
@@ -1202,3 +1202,14 @@ tests:
12021202
#TRIANULATION_FILE:
12031203
# name: expected/triangulation.gml
12041204
# type: vector
1205+
1206+
- algorithm: qgis:removenullgeometries
1207+
name: Remove null geometries
1208+
params:
1209+
INPUT_LAYER:
1210+
name: polys.gml
1211+
type: vector
1212+
results:
1213+
OUTPUT_LAYER:
1214+
name: expected/remove_null_polys.gml
1215+
type: vector

0 commit comments

Comments
 (0)