|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +*************************************************************************** |
| 5 | + Orthagonalize.py |
| 6 | + ---------------- |
| 7 | + Date : December 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__ = 'December 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 | + |
| 29 | +from processing.core.GeoAlgorithm import GeoAlgorithm |
| 30 | +from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException |
| 31 | +from processing.core.parameters import ParameterVector, ParameterNumber |
| 32 | +from processing.core.outputs import OutputVector |
| 33 | +from processing.tools import dataobjects, vector |
| 34 | + |
| 35 | + |
| 36 | +class Orthagonalize(GeoAlgorithm): |
| 37 | + |
| 38 | + INPUT_LAYER = 'INPUT_LAYER' |
| 39 | + OUTPUT_LAYER = 'OUTPUT_LAYER' |
| 40 | + MAX_ITERATIONS = 'MAX_ITERATIONS' |
| 41 | + DISTANCE_THRESHOLD = 'DISTANCE_THRESHOLD' |
| 42 | + ANGLE_TOLERANCE = 'ANGLE_TOLERANCE' |
| 43 | + |
| 44 | + def defineCharacteristics(self): |
| 45 | + self.name, self.i18n_name = self.trAlgorithm('Orthagonalize') |
| 46 | + self.group, self.i18n_group = self.trAlgorithm('Vector geometry tools') |
| 47 | + self.tags = self.tr('rectangle,perpendicular,right,angles,square,quadrilateralise') |
| 48 | + |
| 49 | + self.addParameter(ParameterVector(self.INPUT_LAYER, |
| 50 | + self.tr('Input layer'), [dataobjects.TYPE_VECTOR_LINE, |
| 51 | + dataobjects.TYPE_VECTOR_POLYGON])) |
| 52 | + self.addParameter(ParameterNumber(self.ANGLE_TOLERANCE, |
| 53 | + self.tr('Maximum angle tolerance (degrees)'), |
| 54 | + 0.0, 45.0, 15.0)) |
| 55 | + |
| 56 | + max_iterations = ParameterNumber(self.MAX_ITERATIONS, |
| 57 | + self.tr('Maximum algorithm iterations'), |
| 58 | + 1, 10000, 1000) |
| 59 | + max_iterations.isAdvanced = True |
| 60 | + self.addParameter(max_iterations) |
| 61 | + |
| 62 | + self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Orthagonalized'))) |
| 63 | + |
| 64 | + def processAlgorithm(self, progress): |
| 65 | + layer = dataobjects.getObjectFromUri( |
| 66 | + self.getParameterValue(self.INPUT_LAYER)) |
| 67 | + max_iterations = self.getParameterValue(self.MAX_ITERATIONS) |
| 68 | + angle_tolerance = self.getParameterValue(self.ANGLE_TOLERANCE) |
| 69 | + writer = self.getOutputFromName( |
| 70 | + self.OUTPUT_LAYER).getVectorWriter( |
| 71 | + layer.fields(), |
| 72 | + layer.wkbType(), |
| 73 | + layer.crs()) |
| 74 | + |
| 75 | + features = vector.features(layer) |
| 76 | + total = 100.0 / len(features) |
| 77 | + |
| 78 | + for current, input_feature in enumerate(features): |
| 79 | + output_feature = input_feature |
| 80 | + input_geometry = input_feature.geometry() |
| 81 | + if input_geometry: |
| 82 | + output_geometry = input_geometry.orthagonalize(1.0e-8, max_iterations, angle_tolerance) |
| 83 | + if not output_geometry: |
| 84 | + raise GeoAlgorithmExecutionException( |
| 85 | + self.tr('Error orthagonalizing geometry')) |
| 86 | + |
| 87 | + output_feature.setGeometry(output_geometry) |
| 88 | + |
| 89 | + writer.addFeature(output_feature) |
| 90 | + progress.setPercentage(int(current * total)) |
| 91 | + |
| 92 | + del writer |
0 commit comments