|
| 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 |
0 commit comments