Skip to content

Commit 3a78f9f

Browse files
committed
[processing] Fix multipart to singlepart handling of null geometry
(cherry-picked from 0455b66)
1 parent ebc4a74 commit 3a78f9f

7 files changed

+336
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
Smooth.py
6+
---------
7+
Date : July 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__ = 'July 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+
import os
29+
30+
from qgis.core import QgsFeature
31+
32+
from qgis.PyQt.QtGui import QIcon
33+
34+
from processing.core.GeoAlgorithm import GeoAlgorithm
35+
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
36+
from processing.core.parameters import ParameterVector
37+
from processing.core.outputs import OutputVector
38+
from processing.tools import dataobjects, vector
39+
40+
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
41+
42+
43+
class MergeLines(GeoAlgorithm):
44+
45+
INPUT_LAYER = 'INPUT_LAYER'
46+
OUTPUT_LAYER = 'OUTPUT_LAYER'
47+
48+
def getIcon(self):
49+
return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'to_lines.png'))
50+
51+
def defineCharacteristics(self):
52+
self.name, self.i18n_name = self.trAlgorithm('Merge lines')
53+
self.group, self.i18n_group = self.trAlgorithm('Vector geometry tools')
54+
55+
self.addParameter(ParameterVector(self.INPUT_LAYER,
56+
self.tr('Input layer'), [ParameterVector.VECTOR_TYPE_LINE]))
57+
self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Merged')))
58+
59+
def processAlgorithm(self, progress):
60+
layer = dataobjects.getObjectFromUri(
61+
self.getParameterValue(self.INPUT_LAYER))
62+
63+
writer = self.getOutputFromName(
64+
self.OUTPUT_LAYER).getVectorWriter(
65+
layer.fields().toList(),
66+
layer.wkbType(),
67+
layer.crs())
68+
69+
features = vector.features(layer)
70+
total = 100.0 / len(features)
71+
72+
for current, inFeat in enumerate(features):
73+
outFeat = QgsFeature()
74+
attrs = inFeat.attributes()
75+
outFeat.setAttributes(attrs)
76+
77+
inGeom = inFeat.geometry()
78+
if inGeom:
79+
outGeom = inGeom.mergeLines()
80+
if outGeom is None:
81+
raise GeoAlgorithmExecutionException(
82+
self.tr('Error merging lines'))
83+
84+
outFeat.setGeometry(outGeom)
85+
86+
writer.addFeature(outFeat)
87+
progress.setPercentage(int(current * total))
88+
89+
del writer
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
Smooth.py
6+
---------
7+
Date : July 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__ = 'July 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+
import os
29+
30+
from qgis.core import QgsFeature
31+
32+
from qgis.PyQt.QtGui import QIcon
33+
34+
from processing.core.GeoAlgorithm import GeoAlgorithm
35+
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
36+
from processing.core.parameters import ParameterVector
37+
from processing.core.outputs import OutputVector
38+
from processing.tools import dataobjects, vector
39+
40+
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
41+
42+
43+
class MergeLines(GeoAlgorithm):
44+
45+
INPUT_LAYER = 'INPUT_LAYER'
46+
OUTPUT_LAYER = 'OUTPUT_LAYER'
47+
48+
def getIcon(self):
49+
return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'to_lines.png'))
50+
51+
def defineCharacteristics(self):
52+
self.name, self.i18n_name = self.trAlgorithm('Merge lines')
53+
self.group, self.i18n_group = self.trAlgorithm('Vector geometry tools')
54+
55+
self.addParameter(ParameterVector(self.INPUT_LAYER,
56+
self.tr('Input layer'), [ParameterVector.VECTOR_TYPE_LINE]))
57+
self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Merged')))
58+
59+
def processAlgorithm(self, progress):
60+
layer = dataobjects.getObjectFromUri(
61+
self.getParameterValue(self.INPUT_LAYER))
62+
provider = layer.dataProvider()
63+
64+
writer = self.getOutputFromName(
65+
self.OUTPUT_LAYER).getVectorWriter(
66+
layer.fields().toList(),
67+
provider.wkbType(),
68+
layer.crs())
69+
70+
features = vector.features(layer)
71+
total = 100.0 / len(features)
72+
73+
for current, inFeat in enumerate(features):
74+
outFeat = QgsFeature()
75+
attrs = inFeat.attributes()
76+
outFeat.setAttributes(attrs)
77+
78+
inGeom = inFeat.geometry()
79+
if inGeom:
80+
outGeom = inGeom.mergeLines()
81+
if outGeom is None:
82+
raise GeoAlgorithmExecutionException(
83+
self.tr('Error merging lines'))
84+
85+
outFeat.setGeometry(outGeom)
86+
87+
writer.addFeature(outFeat)
88+
progress.setPercentage(int(current * total))
89+
90+
del writer

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

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
Smooth.py
6+
---------
7+
Date : July 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__ = 'July 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+
import os
29+
30+
from qgis.core import QgsFeature
31+
32+
from qgis.PyQt.QtGui import QIcon
33+
34+
from processing.core.GeoAlgorithm import GeoAlgorithm
35+
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
36+
from processing.core.parameters import ParameterVector
37+
from processing.core.outputs import OutputVector
38+
from processing.tools import dataobjects, vector
39+
40+
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
41+
42+
43+
class MergeLines(GeoAlgorithm):
44+
45+
INPUT_LAYER = 'INPUT_LAYER'
46+
OUTPUT_LAYER = 'OUTPUT_LAYER'
47+
48+
def getIcon(self):
49+
return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'to_lines.png'))
50+
51+
def defineCharacteristics(self):
52+
self.name, self.i18n_name = self.trAlgorithm('Merge lines')
53+
self.group, self.i18n_group = self.trAlgorithm('Vector geometry tools')
54+
55+
self.addParameter(ParameterVector(self.INPUT_LAYER,
56+
self.tr('Input layer'), [ParameterVector.VECTOR_TYPE_LINE]))
57+
self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Merged')))
58+
59+
def processAlgorithm(self, progress):
60+
layer = dataobjects.getObjectFromUri(
61+
self.getParameterValue(self.INPUT_LAYER))
62+
63+
writer = self.getOutputFromName(
64+
self.OUTPUT_LAYER).getVectorWriter(
65+
layer.fields().toList(),
66+
layer.wkbType(),
67+
layer.crs())
68+
69+
features = vector.features(layer)
70+
total = 100.0 / len(features)
71+
72+
for current, inFeat in enumerate(features):
73+
outFeat = QgsFeature()
74+
attrs = inFeat.attributes()
75+
outFeat.setAttributes(attrs)
76+
77+
inGeom = inFeat.geometry()
78+
if inGeom:
79+
outGeom = inGeom.mergeLines()
80+
if outGeom is None:
81+
raise GeoAlgorithmExecutionException(
82+
self.tr('Error merging lines'))
83+
84+
outFeat.setGeometry(outGeom)
85+
86+
writer.addFeature(outFeat)
87+
progress.setPercentage(int(current * total))
88+
89+
del writer

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

+10-8
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,22 @@ def processAlgorithm(self, progress):
6363
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(
6464
layer.pendingFields().toList(), geomType, layer.crs())
6565

66-
outFeat = QgsFeature()
67-
inGeom = QgsGeometry()
68-
6966
features = vector.features(layer)
7067
total = 100.0 / len(features)
7168
for current, f in enumerate(features):
72-
inGeom = f.geometry()
69+
outFeat = QgsFeature()
7370
attrs = f.attributes()
74-
75-
geometries = self.extractAsSingle(inGeom)
7671
outFeat.setAttributes(attrs)
7772

78-
for g in geometries:
79-
outFeat.setGeometry(g)
73+
if f.constGeometry():
74+
inGeom = QgsGeometry(f.constGeometry())
75+
geometries = self.extractAsSingle(inGeom)
76+
77+
for g in geometries:
78+
outFeat.setGeometry(g)
79+
writer.addFeature(outFeat)
80+
else:
81+
#input feature with null geometry
8082
writer.addFeature(outFeat)
8183

8284
progress.setPercentage(int(current * total))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ogr:FeatureCollection
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://ogr.maptools.org/ multi_to_single.xsd"
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>-1</gml:Y></gml:coord>
10+
<gml:coord><gml:X>5.58042226487524</gml:X><gml:Y>4.119769673704415</gml:Y></gml:coord>
11+
</gml:Box>
12+
</gml:boundedBy>
13+
14+
<gml:featureMember>
15+
<ogr:multi_to_single fid="lines.1">
16+
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>-1,-1 1,-1</gml:coordinates></gml:LineString></ogr:geometryProperty>
17+
</ogr:multi_to_single>
18+
</gml:featureMember>
19+
<gml:featureMember>
20+
<ogr:multi_to_single fid="lines.2">
21+
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>3,1 5,1</gml:coordinates></gml:LineString></ogr:geometryProperty>
22+
</ogr:multi_to_single>
23+
</gml:featureMember>
24+
<gml:featureMember>
25+
<ogr:multi_to_single fid="lines.2">
26+
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>5.024184261036468,2.414779270633399 5,1</gml:coordinates></gml:LineString></ogr:geometryProperty>
27+
</ogr:multi_to_single>
28+
</gml:featureMember>
29+
<gml:featureMember>
30+
<ogr:multi_to_single fid="lines.3">
31+
</ogr:multi_to_single>
32+
</gml:featureMember>
33+
<gml:featureMember>
34+
<ogr:multi_to_single fid="lines.4">
35+
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>2,0 2,2 3,2 3,3</gml:coordinates></gml:LineString></ogr:geometryProperty>
36+
</ogr:multi_to_single>
37+
</gml:featureMember>
38+
<gml:featureMember>
39+
<ogr:multi_to_single fid="lines.4">
40+
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>2.944337811900192,4.04721689059501 5.459500959692898,4.119769673704415</gml:coordinates></gml:LineString></ogr:geometryProperty>
41+
</ogr:multi_to_single>
42+
</gml:featureMember>
43+
<gml:featureMember>
44+
<ogr:multi_to_single fid="lines.4">
45+
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>3,3 5.58042226487524,2.946833013435702</gml:coordinates></gml:LineString></ogr:geometryProperty>
46+
</ogr:multi_to_single>
47+
</gml:featureMember>
48+
</ogr:FeatureCollection>

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

+10
Original file line numberDiff line numberDiff line change
@@ -330,4 +330,14 @@ tests:
330330
name: expected/merge_lines.gml
331331
type: vector
332332

333+
- algorithm: qgis:multiparttosingleparts
334+
name: Multiparts to singleparts
335+
params:
336+
INPUT:
337+
name: multilines.gml
338+
type: vector
339+
results:
340+
OUTPUT:
341+
name: expected/multi_to_single.gml
342+
type: vector
333343

0 commit comments

Comments
 (0)