]> gerrit.simantics Code Review - simantics/district.git/commitdiff
Added PipeStruct column to CSV generator script. 38/2838/1
authorReino Ruusu <reino.ruusu@semantum.fi>
Thu, 4 Apr 2019 12:39:40 +0000 (15:39 +0300)
committerReino Ruusu <reino.ruusu@semantum.fi>
Thu, 4 Apr 2019 13:12:06 +0000 (16:12 +0300)
gitlab #40

Change-Id: I2016eb992246dd0c3e8a74c49b820bc48ba5ec6f

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

index 5a9197a4edc64d5040963d85c2829145a2a34b1f..e4d51508d98e75f7beaa47ee581c8dbe0555ea22 100644 (file)
@@ -27,7 +27,7 @@ from qgis.core import (QgsProcessing,
                        QgsUnitTypes)
 import processing
 import re
-
+from operator import itemgetter
 
 class SpanCoordinatesAlgorithm(QgsProcessingAlgorithm):
     """
@@ -207,6 +207,7 @@ class SpanCoordinatesAlgorithm(QgsProcessingAlgorithm):
         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))
+        if not ('PipeStruct' in sourceNames): outputFields.append(QgsField('PipeStruct', QVariant.String))
         
         (output, outputId) = self.parameterAsSink(
             parameters,
@@ -234,6 +235,9 @@ class SpanCoordinatesAlgorithm(QgsProcessingAlgorithm):
         # Dictionary from span feature ids to lists of lists of QgsPoint objects
         strings = dict()
         
+        # Dictionary for the PipeStruct field values
+        types = dict()
+        
         spanFeatures = span.getFeatures()
         pipeFeatures = pipe.getFeatures()
         
@@ -246,7 +250,9 @@ class SpanCoordinatesAlgorithm(QgsProcessingAlgorithm):
             fatherID = feature[self.FATHER_ID]
             
             # Length
-            myLength = geometry.length()
+            myLength = feature['Length']
+            if myLength == None:
+                myLength = geometry.length()
             
             oldLength = lengths.get(fatherID, 0.0)
             lengths[fatherID] = oldLength + myLength
@@ -265,6 +271,14 @@ class SpanCoordinatesAlgorithm(QgsProcessingAlgorithm):
             pointList.append(mylist)
             strings[fatherID] = pointList
             
+            # Store the value of PipeStruct
+            t = feature['PipeStruct']
+            tt = types.get(fatherID, {})
+            types[fatherID] = tt
+            c = tt.get(t, 0)
+            c += myLength
+            tt[t] = c
+            
             # Update the progress bar
             feedback.setProgress(int(counter * total))
 
@@ -353,6 +367,16 @@ class SpanCoordinatesAlgorithm(QgsProcessingAlgorithm):
             outputFeature['Length'] = feature['Length'] # myLength
             outputFeature['LineString'] = result
             
+            # Handle pipe type codes
+            mytypes = list(types.get(id, {}).items())
+            if len(mytypes) == 0:
+                feedback.pushInfo('No type codes for feature {}'.format(id))
+            else:
+                if len(mytypes) > 1:
+                    mytypes.sort(key = itemgetter(1))
+                    feedback.pushInfo('No unique type code for feature {}: {}'.format(id, mytypes))
+                outputFeature['PipeStruct'] = mytypes[-1][0]
+            
             label = feature['Label']
             m = self.DN_PATTERN.fullmatch(label)
             if m: