2013-07-30 19:43:01 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
PointsDisplacement.py
|
|
|
|
---------------------
|
|
|
|
Date : July 2013
|
|
|
|
Copyright : (C) 2013 by Alexander Bruy
|
|
|
|
Email : alexander dot bruy at gmail dot com
|
|
|
|
***************************************************************************
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
2016-09-27 19:51:06 +02:00
|
|
|
from builtins import next
|
2013-07-30 19:43:01 +03:00
|
|
|
|
|
|
|
__author__ = 'Alexander Bruy'
|
|
|
|
__date__ = 'July 2013'
|
|
|
|
__copyright__ = '(C) 2013, Alexander Bruy'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2013-07-30 19:43:01 +03:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2013-07-30 19:43:01 +03:00
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
|
|
|
import math
|
2017-03-29 10:42:42 +10:00
|
|
|
from qgis.core import (QgsApplication,
|
|
|
|
QgsFeatureRequest,
|
|
|
|
QgsFeature,
|
2017-06-23 14:34:38 +10:00
|
|
|
QgsFeatureSink,
|
2017-03-29 10:42:42 +10:00
|
|
|
QgsGeometry,
|
2017-06-01 12:18:43 +02:00
|
|
|
QgsPointXY,
|
2017-04-25 17:53:32 +10:00
|
|
|
QgsProcessingUtils)
|
2017-05-02 14:47:58 +10:00
|
|
|
from processing.tools import dataobjects
|
2017-06-06 13:41:42 +10:00
|
|
|
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
2014-07-14 14:19:09 +02:00
|
|
|
from processing.core.parameters import ParameterVector
|
|
|
|
from processing.core.parameters import ParameterNumber
|
|
|
|
from processing.core.parameters import ParameterBoolean
|
|
|
|
from processing.core.outputs import OutputVector
|
2013-07-30 19:43:01 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
class PointsDisplacement(QgisAlgorithm):
|
2013-07-30 19:43:01 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
INPUT_LAYER = 'INPUT_LAYER'
|
|
|
|
DISTANCE = 'DISTANCE'
|
|
|
|
HORIZONTAL = 'HORIZONTAL'
|
|
|
|
OUTPUT_LAYER = 'OUTPUT_LAYER'
|
2013-07-30 19:43:01 +03:00
|
|
|
|
2017-03-29 12:04:09 +10:00
|
|
|
def group(self):
|
|
|
|
return self.tr('Vector geometry tools')
|
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterVector(self.INPUT_LAYER,
|
2016-08-23 19:33:42 +03:00
|
|
|
self.tr('Input layer'), [dataobjects.TYPE_VECTOR_POINT]))
|
2013-10-01 20:52:22 +03:00
|
|
|
self.addParameter(ParameterNumber(self.DISTANCE,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Displacement distance'),
|
|
|
|
0.00001, 999999999.999990, 0.00015))
|
2013-10-01 20:52:22 +03:00
|
|
|
self.addParameter(ParameterBoolean(self.HORIZONTAL,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Horizontal distribution for two point case')))
|
2016-08-23 19:33:42 +03:00
|
|
|
self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Displaced'), datatype=[dataobjects.TYPE_VECTOR_POINT]))
|
2013-07-30 19:43:01 +03:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'pointsdisplacement'
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Points displacement')
|
|
|
|
|
2017-05-15 16:19:46 +10:00
|
|
|
def processAlgorithm(self, parameters, context, feedback):
|
2013-07-30 19:43:01 +03:00
|
|
|
radius = self.getParameterValue(self.DISTANCE)
|
|
|
|
horizontal = self.getParameterValue(self.HORIZONTAL)
|
|
|
|
output = self.getOutputFromName(self.OUTPUT_LAYER)
|
|
|
|
|
2017-05-02 13:40:49 +10:00
|
|
|
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT_LAYER), context)
|
2013-07-30 19:43:01 +03:00
|
|
|
|
2017-04-26 14:33:53 +10:00
|
|
|
writer = output.getVectorWriter(layer.fields(), layer.wkbType(), layer.crs(), context)
|
2013-07-30 19:43:01 +03:00
|
|
|
|
2017-04-25 17:59:35 +10:00
|
|
|
features = QgsProcessingUtils.getFeatures(layer, context)
|
2013-07-30 19:43:01 +03:00
|
|
|
|
2017-06-23 13:49:32 +10:00
|
|
|
total = 100.0 / layer.featureCount() if layer.featureCount() else 0
|
2013-07-30 19:43:01 +03:00
|
|
|
|
|
|
|
duplicates = dict()
|
2016-02-17 09:36:59 +02:00
|
|
|
for current, f in enumerate(features):
|
2013-07-30 19:43:01 +03:00
|
|
|
wkt = f.geometry().exportToWkt()
|
|
|
|
if wkt not in duplicates:
|
|
|
|
duplicates[wkt] = [f.id()]
|
|
|
|
else:
|
|
|
|
duplicates[wkt].extend([f.id()])
|
|
|
|
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(int(current * total))
|
2013-07-30 19:43:01 +03:00
|
|
|
|
|
|
|
current = 0
|
2017-06-23 13:49:32 +10:00
|
|
|
total = 100.0 / len(duplicates) if duplicates else 1
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(0)
|
2013-07-30 19:43:01 +03:00
|
|
|
|
|
|
|
fullPerimeter = 2 * math.pi
|
|
|
|
|
2016-09-27 19:51:06 +02:00
|
|
|
for (geom, fids) in list(duplicates.items()):
|
2013-07-30 19:43:01 +03:00
|
|
|
count = len(fids)
|
|
|
|
if count == 1:
|
2016-10-17 10:30:55 +10:00
|
|
|
f = next(layer.getFeatures(QgsFeatureRequest().setFilterFid(fids[0])))
|
2017-06-23 14:34:38 +10:00
|
|
|
writer.addFeature(f, QgsFeatureSink.FastInsert)
|
2013-07-30 19:43:01 +03:00
|
|
|
else:
|
|
|
|
angleStep = fullPerimeter / count
|
|
|
|
if count == 2 and horizontal:
|
|
|
|
currentAngle = math.pi / 2
|
|
|
|
else:
|
|
|
|
currentAngle = 0
|
|
|
|
|
|
|
|
old_point = QgsGeometry.fromWkt(geom).asPoint()
|
2016-10-17 10:30:55 +10:00
|
|
|
|
|
|
|
request = QgsFeatureRequest().setFilterFids(fids).setFlags(QgsFeatureRequest.NoGeometry)
|
|
|
|
for f in layer.getFeatures(request):
|
2013-07-30 19:43:01 +03:00
|
|
|
sinusCurrentAngle = math.sin(currentAngle)
|
|
|
|
cosinusCurrentAngle = math.cos(currentAngle)
|
|
|
|
dx = radius * sinusCurrentAngle
|
|
|
|
dy = radius * cosinusCurrentAngle
|
|
|
|
|
2017-06-01 12:18:43 +02:00
|
|
|
new_point = QgsPointXY(old_point.x() + dx, old_point.y() + dy)
|
2013-07-30 19:43:01 +03:00
|
|
|
out_feature = QgsFeature()
|
|
|
|
out_feature.setGeometry(QgsGeometry.fromPoint(new_point))
|
|
|
|
out_feature.setAttributes(f.attributes())
|
|
|
|
|
2017-06-23 14:34:38 +10:00
|
|
|
writer.addFeature(out_feature, QgsFeatureSink.FastInsert)
|
2013-07-30 19:43:01 +03:00
|
|
|
currentAngle += angleStep
|
|
|
|
|
|
|
|
current += 1
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(int(current * total))
|
2013-07-30 19:43:01 +03:00
|
|
|
|
|
|
|
del writer
|