2014-04-30 17:27:48 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
PointsToPaths.py
|
|
|
|
---------------------
|
|
|
|
Date : April 2014
|
|
|
|
Copyright : (C) 2014 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-21 18:24:26 +02:00
|
|
|
from builtins import str
|
2014-04-30 17:27:48 +03:00
|
|
|
|
|
|
|
__author__ = 'Alexander Bruy'
|
|
|
|
__date__ = 'April 2014'
|
|
|
|
__copyright__ = '(C) 2014, Alexander Bruy'
|
|
|
|
|
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
|
|
|
|
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2014-05-05 16:00:54 +03:00
|
|
|
import os
|
2014-04-30 17:27:48 +03:00
|
|
|
from datetime import datetime
|
|
|
|
|
2016-04-22 10:38:48 +02:00
|
|
|
from qgis.PyQt.QtCore import QVariant
|
2017-03-29 10:42:42 +10:00
|
|
|
from qgis.core import (QgsApplication,
|
|
|
|
QgsFeature,
|
2017-06-23 14:34:38 +10:00
|
|
|
QgsFeatureSink,
|
2017-03-29 10:42:42 +10:00
|
|
|
QgsFields,
|
|
|
|
QgsField,
|
|
|
|
QgsGeometry,
|
|
|
|
QgsDistanceArea,
|
2017-04-25 17:53:32 +10:00
|
|
|
QgsWkbTypes,
|
|
|
|
QgsProcessingUtils)
|
2014-04-30 17:27:48 +03:00
|
|
|
|
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 ParameterTableField
|
|
|
|
from processing.core.parameters import ParameterString
|
|
|
|
from processing.core.outputs import OutputVector
|
|
|
|
from processing.core.outputs import OutputDirectory
|
2017-05-02 14:47:58 +10:00
|
|
|
from processing.tools import dataobjects
|
2014-05-05 16:00:54 +03:00
|
|
|
|
2014-04-30 17:27:48 +03:00
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
class PointsToPaths(QgisAlgorithm):
|
2014-04-30 17:27:48 +03:00
|
|
|
|
|
|
|
VECTOR = 'VECTOR'
|
|
|
|
GROUP_FIELD = 'GROUP_FIELD'
|
|
|
|
ORDER_FIELD = 'ORDER_FIELD'
|
|
|
|
DATE_FORMAT = 'DATE_FORMAT'
|
2014-05-05 12:33:25 +03:00
|
|
|
#GAP_PERIOD = 'GAP_PERIOD'
|
2014-05-05 16:00:54 +03:00
|
|
|
OUTPUT_LINES = 'OUTPUT_LINES'
|
|
|
|
OUTPUT_TEXT = 'OUTPUT_TEXT'
|
2014-04-30 17:27:48 +03:00
|
|
|
|
2017-03-29 12:04:09 +10:00
|
|
|
def group(self):
|
|
|
|
return self.tr('Vector creation tools')
|
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2014-04-30 17:27:48 +03:00
|
|
|
self.addParameter(ParameterVector(self.VECTOR,
|
2016-08-23 19:33:42 +03:00
|
|
|
self.tr('Input point layer'), [dataobjects.TYPE_VECTOR_POINT]))
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterTableField(self.GROUP_FIELD,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Group field'), self.VECTOR))
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterTableField(self.ORDER_FIELD,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Order field'), self.VECTOR))
|
2014-04-30 17:27:48 +03:00
|
|
|
self.addParameter(ParameterString(self.DATE_FORMAT,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Date format (if order field is DateTime)'), '', optional=True))
|
2014-05-05 12:33:25 +03:00
|
|
|
#self.addParameter(ParameterNumber(
|
|
|
|
# self.GAP_PERIOD,
|
|
|
|
# 'Gap period (if order field is DateTime)', 0, 60, 0))
|
2016-08-23 19:33:42 +03:00
|
|
|
self.addOutput(OutputVector(self.OUTPUT_LINES, self.tr('Paths'), datatype=[dataobjects.TYPE_VECTOR_LINE]))
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addOutput(OutputDirectory(self.OUTPUT_TEXT, self.tr('Directory')))
|
2014-04-30 17:27:48 +03:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'pointstopath'
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Points to path')
|
|
|
|
|
2017-05-15 16:19:46 +10:00
|
|
|
def processAlgorithm(self, parameters, context, feedback):
|
2017-05-02 13:40:49 +10:00
|
|
|
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.VECTOR), context)
|
2014-04-30 17:27:48 +03:00
|
|
|
groupField = self.getParameterValue(self.GROUP_FIELD)
|
|
|
|
orderField = self.getParameterValue(self.ORDER_FIELD)
|
2016-09-21 18:24:26 +02:00
|
|
|
dateFormat = str(self.getParameterValue(self.DATE_FORMAT))
|
2014-05-05 12:33:25 +03:00
|
|
|
#gap = int(self.getParameterValue(self.GAP_PERIOD))
|
2014-05-05 16:00:54 +03:00
|
|
|
dirName = self.getOutputValue(self.OUTPUT_TEXT)
|
2014-04-30 17:27:48 +03:00
|
|
|
|
|
|
|
fields = QgsFields()
|
|
|
|
fields.append(QgsField('group', QVariant.String, '', 254, 0))
|
|
|
|
fields.append(QgsField('begin', QVariant.String, '', 254, 0))
|
|
|
|
fields.append(QgsField('end', QVariant.String, '', 254, 0))
|
2017-04-26 14:33:53 +10:00
|
|
|
writer = self.getOutputFromName(self.OUTPUT_LINES).getVectorWriter(fields, QgsWkbTypes.LineString, layer.crs(),
|
|
|
|
context)
|
2014-04-30 17:27:48 +03:00
|
|
|
|
|
|
|
points = dict()
|
2017-04-25 17:59:35 +10:00
|
|
|
features = QgsProcessingUtils.getFeatures(layer, context)
|
2017-06-23 13:49:32 +10:00
|
|
|
total = 100.0 / layer.featureCount() if layer.featureCount() else 0
|
2016-02-17 09:36:59 +02:00
|
|
|
for current, f in enumerate(features):
|
2014-04-30 17:27:48 +03:00
|
|
|
point = f.geometry().asPoint()
|
|
|
|
group = f[groupField]
|
|
|
|
order = f[orderField]
|
|
|
|
if dateFormat != '':
|
2016-09-21 18:24:26 +02:00
|
|
|
order = datetime.strptime(str(order), dateFormat)
|
2014-04-30 17:27:48 +03:00
|
|
|
if group in points:
|
|
|
|
points[group].append((order, point))
|
|
|
|
else:
|
|
|
|
points[group] = [(order, point)]
|
|
|
|
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(int(current * total))
|
2014-04-30 17:27:48 +03:00
|
|
|
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(0)
|
2014-04-30 17:27:48 +03:00
|
|
|
|
2014-05-05 16:00:54 +03:00
|
|
|
da = QgsDistanceArea()
|
|
|
|
|
2016-02-17 09:36:59 +02:00
|
|
|
current = 0
|
2017-06-23 13:49:32 +10:00
|
|
|
total = 100.0 / len(points) if points else 1
|
2016-09-27 19:51:06 +02:00
|
|
|
for group, vertices in list(points.items()):
|
2014-05-05 16:00:54 +03:00
|
|
|
vertices.sort()
|
2014-04-30 17:27:48 +03:00
|
|
|
f = QgsFeature()
|
|
|
|
f.initAttributes(len(fields))
|
|
|
|
f.setFields(fields)
|
2014-05-05 16:00:54 +03:00
|
|
|
f['group'] = group
|
|
|
|
f['begin'] = vertices[0][0]
|
|
|
|
f['end'] = vertices[-1][0]
|
|
|
|
|
2014-05-07 20:09:10 +03:00
|
|
|
fileName = os.path.join(dirName, '%s.txt' % group)
|
2014-05-05 16:00:54 +03:00
|
|
|
|
2016-11-07 10:35:15 +10:00
|
|
|
with open(fileName, 'w') as fl:
|
|
|
|
fl.write('angle=Azimuth\n')
|
|
|
|
fl.write('heading=Coordinate_System\n')
|
|
|
|
fl.write('dist_units=Default\n')
|
|
|
|
|
|
|
|
line = []
|
|
|
|
i = 0
|
|
|
|
for node in vertices:
|
|
|
|
line.append(node[1])
|
|
|
|
|
|
|
|
if i == 0:
|
|
|
|
fl.write('startAt=%f;%f;90\n' % (node[1].x(), node[1].y()))
|
|
|
|
fl.write('survey=Polygonal\n')
|
|
|
|
fl.write('[data]\n')
|
|
|
|
else:
|
|
|
|
angle = line[i - 1].azimuth(line[i])
|
|
|
|
distance = da.measureLine(line[i - 1], line[i])
|
|
|
|
fl.write('%f;%f;90\n' % (angle, distance))
|
|
|
|
|
|
|
|
i += 1
|
2014-05-05 16:00:54 +03:00
|
|
|
|
2014-04-30 17:27:48 +03:00
|
|
|
f.setGeometry(QgsGeometry.fromPolyline(line))
|
2017-06-23 14:34:38 +10:00
|
|
|
writer.addFeature(f, QgsFeatureSink.FastInsert)
|
2016-02-17 09:36:59 +02:00
|
|
|
current += 1
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(int(current * total))
|
2014-04-30 17:27:48 +03:00
|
|
|
|
|
|
|
del writer
|