|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +*************************************************************************** |
| 5 | + PointsDisplacement.py |
| 6 | + --------------------- |
| 7 | + Date : July 2013 |
| 8 | + Copyright : (C) 2013 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__ = 'July 2013' |
| 22 | +__copyright__ = '(C) 2013, Alexander Bruy' |
| 23 | +# This will get replaced with a git SHA1 when you do a git archive |
| 24 | +__revision__ = '$Format:%H$' |
| 25 | + |
| 26 | +import math |
| 27 | + |
| 28 | +from PyQt4.QtCore import * |
| 29 | + |
| 30 | +from qgis.core import * |
| 31 | + |
| 32 | +from sextante.core.GeoAlgorithm import GeoAlgorithm |
| 33 | +from sextante.core.QGisLayers import QGisLayers |
| 34 | + |
| 35 | +from sextante.parameters.ParameterVector import ParameterVector |
| 36 | +from sextante.parameters.ParameterNumber import ParameterNumber |
| 37 | +from sextante.parameters.ParameterBoolean import ParameterBoolean |
| 38 | +from sextante.outputs.OutputVector import OutputVector |
| 39 | + |
| 40 | +class PointsDisplacement(GeoAlgorithm): |
| 41 | + |
| 42 | + INPUT_LAYER = "INPUT_LAYER" |
| 43 | + DISTANCE = "DISTANCE" |
| 44 | + HORIZONTAL = "HORIZONTAL" |
| 45 | + OUTPUT_LAYER = "OUTPUT_LAYER" |
| 46 | + |
| 47 | + def defineCharacteristics(self): |
| 48 | + self.name = "Points displacement" |
| 49 | + self.group = "Vector geometry tools" |
| 50 | + |
| 51 | + self.addParameter(ParameterVector(self.INPUT_LAYER, "Input layer", ParameterVector.VECTOR_TYPE_POINT)) |
| 52 | + self.addParameter(ParameterNumber(self.DISTANCE, "Displacement distance", 0.00001, 999999999.999990, 0.00015)) |
| 53 | + self.addParameter(ParameterBoolean(self.HORIZONTAL, "Horizontal distribution for two point case")) |
| 54 | + self.addOutput(OutputVector(self.OUTPUT_LAYER, "Output layer")) |
| 55 | + |
| 56 | + def processAlgorithm(self, progress): |
| 57 | + radius = self.getParameterValue(self.DISTANCE) |
| 58 | + horizontal = self.getParameterValue(self.HORIZONTAL) |
| 59 | + output = self.getOutputFromName(self.OUTPUT_LAYER) |
| 60 | + |
| 61 | + layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT_LAYER)) |
| 62 | + |
| 63 | + provider = layer.dataProvider() |
| 64 | + writer = output.getVectorWriter(provider.fields(), provider.geometryType(), provider.crs()) |
| 65 | + |
| 66 | + features = QGisLayers.features(layer) |
| 67 | + |
| 68 | + current = 0 |
| 69 | + total = 100.0 / len(features) |
| 70 | + |
| 71 | + duplicates = dict() |
| 72 | + for f in features: |
| 73 | + wkt = f.geometry().exportToWkt() |
| 74 | + if wkt not in duplicates: |
| 75 | + duplicates[wkt] = [f.id()] |
| 76 | + else: |
| 77 | + duplicates[wkt].extend([f.id()]) |
| 78 | + |
| 79 | + current += 1 |
| 80 | + progress.setPercentage(int(current * total)) |
| 81 | + |
| 82 | + current = 0 |
| 83 | + total = 100.0 / len(duplicates) |
| 84 | + progress.setPercentage(0) |
| 85 | + |
| 86 | + fullPerimeter = 2 * math.pi |
| 87 | + |
| 88 | + request = QgsFeatureRequest() |
| 89 | + for geom, fids in duplicates.iteritems(): |
| 90 | + count = len(fids) |
| 91 | + if count == 1: |
| 92 | + f = layer.getFeatures(request.setFilterFid(fids[0])).next() |
| 93 | + writer.addFeature(f) |
| 94 | + else: |
| 95 | + angleStep = fullPerimeter / count |
| 96 | + if count == 2 and horizontal: |
| 97 | + currentAngle = math.pi / 2 |
| 98 | + else: |
| 99 | + currentAngle = 0 |
| 100 | + |
| 101 | + old_point = QgsGeometry.fromWkt(geom).asPoint() |
| 102 | + for fid in fids: |
| 103 | + sinusCurrentAngle = math.sin(currentAngle) |
| 104 | + cosinusCurrentAngle = math.cos(currentAngle) |
| 105 | + dx = radius * sinusCurrentAngle |
| 106 | + dy = radius * cosinusCurrentAngle |
| 107 | + |
| 108 | + f = layer.getFeatures(request.setFilterFid(fid)).next() |
| 109 | + |
| 110 | + new_point = QgsPoint(old_point.x() + dx, old_point.y() + dy) |
| 111 | + out_feature = QgsFeature() |
| 112 | + out_feature.setGeometry(QgsGeometry.fromPoint(new_point)) |
| 113 | + out_feature.setAttributes(f.attributes()) |
| 114 | + |
| 115 | + writer.addFeature(out_feature) |
| 116 | + currentAngle += angleStep |
| 117 | + |
| 118 | + current += 1 |
| 119 | + progress.setPercentage(int(current * total)) |
| 120 | + |
| 121 | + del writer |
0 commit comments