Skip to content

Commit 0293bc7

Browse files
committedFeb 7, 2017
[FEATURE][processing] algorithm to fix invalid geometries using native
makeValid() implementation
1 parent ae74220 commit 0293bc7

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed
 
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
FixGeometry.py
6+
-----------------------
7+
Date : January 2017
8+
Copyright : (C) 2017 by Alexander Bruy
9+
Email : alexander dot bruy 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__ = 'Alexander Bruy'
21+
__date__ = 'January 2017'
22+
__copyright__ = '(C) 2017, Alexander Bruy'
23+
24+
# This will get replaced with a git SHA1 when you do a git archive
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.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
32+
from processing.tools import dataobjects, vector
33+
34+
35+
class FixGeometry(GeoAlgorithm):
36+
37+
INPUT = 'INPUT'
38+
OUTPUT = 'OUTPUT'
39+
40+
def defineCharacteristics(self):
41+
self.name, self.i18n_name = self.trAlgorithm('Fix geometries')
42+
self.group, self.i18n_group = self.trAlgorithm('Vector geometry tools')
43+
44+
self.addParameter(ParameterVector(self.INPUT,
45+
self.tr('Input Layer')))
46+
self.addOutput(OutputVector(self.OUTPUT,
47+
self.tr('Layer with fixed geometries')))
48+
49+
def processAlgorithm(self, feedback):
50+
layer = dataobjects.getObjectFromUri(
51+
self.getParameterValue(self.INPUT))
52+
53+
writer = self.getOutputFromName(
54+
self.OUTPUT).getVectorWriter(
55+
layer.fields(),
56+
layer.wkbType(),
57+
layer.crs())
58+
59+
features = vector.features(layer)
60+
if len(features) == 0:
61+
raise GeoAlgorithmExecutionException(self.tr('There are no features in the input layer'))
62+
63+
total = 100.0 / len(features)
64+
for current, inputFeature in enumerate(features):
65+
outputFeature = inputFeature
66+
if inputFeature.geometry():
67+
outputGeometry = inputFeature.geometry().makeValid()
68+
if not outputGeometry:
69+
ProcessingLog.addToLog(ProcessingLog.LOG_WARNING,
70+
'makeValid failed for feature {}'.format(inputFeature.id()))
71+
outputFeature.setGeometry(outputGeometry)
72+
73+
writer.addFeature(outputFeature)
74+
feedback.setProgress(int(current * total))
75+
76+
del writer

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@
184184
from .ServiceAreaFromLayer import ServiceAreaFromLayer
185185
from .TruncateTable import TruncateTable
186186
from .Polygonize import Polygonize
187+
from .FixGeometry import FixGeometry
187188

188189
pluginPath = os.path.normpath(os.path.join(
189190
os.path.split(os.path.dirname(__file__))[0], os.pardir))
@@ -252,7 +253,8 @@ def __init__(self):
252253
RasterCalculator(), Heatmap(), Orthogonalize(),
253254
ShortestPathPointToPoint(), ShortestPathPointToLayer(),
254255
ShortestPathLayerToPoint(), ServiceAreaFromPoint(),
255-
ServiceAreaFromLayer(), TruncateTable(), Polygonize()
256+
ServiceAreaFromLayer(), TruncateTable(), Polygonize(),
257+
FixGeometry()
256258
]
257259

258260
if hasMatplotlib:

0 commit comments

Comments
 (0)
Please sign in to comment.