]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.feature/rootFiles/QGIS scripts/generateCSV.py
Hide "enabled" column for non-component type tech type tables
[simantics/district.git] / org.simantics.district.feature / rootFiles / QGIS scripts / generateCSV.py
index c724d89e770bb1b6b258f7460f474b1af1dd9214..e4d51508d98e75f7beaa47ee581c8dbe0555ea22 100644 (file)
@@ -23,10 +23,11 @@ from qgis.core import (QgsProcessing,
                        QgsProcessingParameterDistance,
                        QgsVectorDataProvider,
                        QgsFields,
-                       QgsField)
+                       QgsField,
+                       QgsUnitTypes)
 import processing
 import re
-
+from operator import itemgetter
 
 class SpanCoordinatesAlgorithm(QgsProcessingAlgorithm):
     """
@@ -49,6 +50,7 @@ class SpanCoordinatesAlgorithm(QgsProcessingAlgorithm):
     PIPE = 'PIPE'
     SPAN = 'SPAN'
     TOLERANCE = 'TOLERANCE'
+    MINIMUM_LENGTH = 'MINIMUM_LENGTH'
     OUTPUT = 'OUTPUT'
     
     # Constants for feature field names
@@ -133,14 +135,23 @@ class SpanCoordinatesAlgorithm(QgsProcessingAlgorithm):
             )
         )
         
-        self.addParameter(
-            QgsProcessingParameterDistance(
-                self.TOLERANCE,
-                self.tr('Location tolerance'),
-                0.0,
-                minValue = 0.0
-            )
+        tol = QgsProcessingParameterDistance(
+            self.TOLERANCE,
+            self.tr('Location tolerance'),
+            0.001,
+            minValue = 0.0
         )
+        tol.setDefaultUnit(QgsUnitTypes.DistanceMeters)
+        self.addParameter( tol )
+        
+        dist = QgsProcessingParameterDistance(
+            self.MINIMUM_LENGTH,
+            self.tr('Minimum span length'),
+            0.25,
+            minValue = 0.0
+        )
+        dist.setDefaultUnit(QgsUnitTypes.DistanceMeters)
+        self.addParameter( dist )
         
         self.addParameter(
             QgsProcessingParameterFeatureSink(
@@ -176,6 +187,14 @@ class SpanCoordinatesAlgorithm(QgsProcessingAlgorithm):
             context
         )
         
+        minLength = self.parameterAsDouble(
+            parameters,
+            self.MINIMUM_LENGTH,
+            context
+        )
+        
+        feedback.pushInfo('Tolerance: {} m\nMinimum span length: {} m'.format(eps, minLength))
+        
         sourceFields = span.fields()
         sourceNames = sourceFields.names()
         outputFields = QgsFields(sourceFields)
@@ -188,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,
@@ -215,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()
         
@@ -227,10 +250,12 @@ class SpanCoordinatesAlgorithm(QgsProcessingAlgorithm):
             fatherID = feature[self.FATHER_ID]
             
             # Length
-            length = geometry.length()
+            myLength = feature['Length']
+            if myLength == None:
+                myLength = geometry.length()
             
             oldLength = lengths.get(fatherID, 0.0)
-            lengths[fatherID] = oldLength + length
+            lengths[fatherID] = oldLength + myLength
             
             # Segment points
             pointList = strings.get(fatherID, [])
@@ -246,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))
 
@@ -267,7 +300,11 @@ class SpanCoordinatesAlgorithm(QgsProcessingAlgorithm):
             #feedback.pushInfo(str(id))
 
             # Length
-            length = lengths.get(id, None)
+            myLength = feature['Length']
+            
+            # Ignore short stumps
+            if myLength <= minLength:
+                continue
             
             # Vertices
             mypoints = list(feature.geometry().vertices())
@@ -327,9 +364,19 @@ class SpanCoordinatesAlgorithm(QgsProcessingAlgorithm):
             outputFeature['x2'] = feature['x2']
             outputFeature['y2'] = feature['y2']
             outputFeature['z2'] = feature['z2']
-            outputFeature['Length'] = feature['length'] # length
+            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: