]> gerrit.simantics Code Review - simantics/district.git/commitdiff
Added DimensionDN column to CSV generator script. 36/2836/1
authorReino Ruusu <reino.ruusu@semantum.fi>
Wed, 3 Apr 2019 14:40:46 +0000 (17:40 +0300)
committerReino Ruusu <reino.ruusu@semantum.fi>
Thu, 4 Apr 2019 13:12:05 +0000 (16:12 +0300)
gitlab #40

Change-Id: I14717e4e1a35a17029e4dfc36715afe72f4c6d9d

org.simantics.district.feature/rootFiles/QGIS scripts/generateCSV.py

index 80c596b2c31733ec7eda00c734469d2eebca3dd1..c724d89e770bb1b6b258f7460f474b1af1dd9214 100644 (file)
@@ -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')