From 51d71647a8ff8ab892b51f2265a1ccb14451eb7e Mon Sep 17 00:00:00 2001 From: Reino Ruusu Date: Wed, 3 Apr 2019 17:40:46 +0300 Subject: [PATCH] Added DimensionDN column to CSV generator script. gitlab #40 Change-Id: I14717e4e1a35a17029e4dfc36715afe72f4c6d9d --- .../rootFiles/QGIS scripts/generateCSV.py | 33 +++++++++++++++---- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/org.simantics.district.feature/rootFiles/QGIS scripts/generateCSV.py b/org.simantics.district.feature/rootFiles/QGIS scripts/generateCSV.py index 80c596b2..c724d89e 100644 --- a/org.simantics.district.feature/rootFiles/QGIS scripts/generateCSV.py +++ b/org.simantics.district.feature/rootFiles/QGIS scripts/generateCSV.py @@ -25,6 +25,7 @@ from qgis.core import (QgsProcessing, QgsFields, QgsField) import processing +import re class SpanCoordinatesAlgorithm(QgsProcessingAlgorithm): @@ -53,6 +54,9 @@ class SpanCoordinatesAlgorithm(QgsProcessingAlgorithm): # Constants for feature field names FATHER_ID = 'FatherId' LINESTRING = 'LineString' + + # RegExp for DN values in + DN_PATTERN = re.compile(r"DN(\d+)(-.*)?") def tr(self, string): """ @@ -183,6 +187,7 @@ class SpanCoordinatesAlgorithm(QgsProcessingAlgorithm): if not ('z2' in sourceNames): outputFields.append(QgsField('z2', QVariant.Double)) if not ('Length' in sourceNames): outputFields.append(QgsField('Length', QVariant.Double)) if not ('LineString' in sourceNames): outputFields.append(QgsField('LineString', QVariant.String)) + if not ('DimensionDN' in sourceNames): outputFields.append(QgsField('DimensionDN', QVariant.Int)) (output, outputId) = self.parameterAsSink( parameters, @@ -265,14 +270,14 @@ class SpanCoordinatesAlgorithm(QgsProcessingAlgorithm): length = lengths.get(id, None) # Vertices + mypoints = list(feature.geometry().vertices()) mylist = strings.get(id, None) if mylist == None: feedback.pushInfo('No points for feature {}'.format(id)) - continue + mylist = [mypoints] #feedback.pushInfo('Points: {}'.format("|".join(map(lambda x: ";".join(('{} {}'.format(p.x(), p.y()) for p in x)), mylist)))) - mypoints = list(feature.geometry().vertices()) head = feature.geometry().vertices().next() resultList = [head] @@ -283,21 +288,32 @@ class SpanCoordinatesAlgorithm(QgsProcessingAlgorithm): mylist = list(map(lambda x: list(reversed(x)), mylist)) i = next((i for i, x in enumerate(mylist) if head.distance(x[0]) <= eps), None) if i == None: - feedback.pushInfo('No matching start vertex for feature {}'.format(id)) - continue + feedback.pushInfo('Warning: No matching start vertex for feature {}'.format(id)) + mylist = [mypoints] + i = 0 + + vertices = mylist.pop(i) while i != None: - vertices = mylist.pop(i) tail = vertices[-1] resultList.extend(vertices[1:]) + if tail.distance(mypoints[-1]) <= eps: + break + i = next((i for i, x in enumerate(mylist) if tail.distance(x[0]) <= eps), None) + if i != None: + vertices = mylist.pop(i) + else: + i = next((i for i, x in enumerate(mylist) if tail.distance(x[-1]) <= eps), None) + if i != None: + vertices = list(reversed(mylist.pop(i))) # feedback.pushInfo(str(resultList)) # Convert to string result = ";".join(('{} {}'.format(p.x(), p.y()) for p in resultList)) - feedback.pushInfo('Feature {}: {}'.format(id, result)) + # feedback.pushInfo('Feature {}: {}'.format(id, result)) outputFeature = QgsFeature() outputFeature.setFields(outputFields) @@ -314,6 +330,11 @@ class SpanCoordinatesAlgorithm(QgsProcessingAlgorithm): outputFeature['Length'] = feature['length'] # length outputFeature['LineString'] = result + label = feature['Label'] + m = self.DN_PATTERN.fullmatch(label) + if m: + outputFeature['DimensionDN'] = int(m.group(1)) + output.addFeature(outputFeature) feedback.pushInfo('Loop done') -- 2.47.1