]> gerrit.simantics Code Review - simantics/district.git/commitdiff
Add minimum span length paremeter to CSV generator script. 37/2837/1
authorReino Ruusu <reino.ruusu@semantum.fi>
Wed, 3 Apr 2019 14:41:43 +0000 (17:41 +0300)
committerReino Ruusu <reino.ruusu@semantum.fi>
Thu, 4 Apr 2019 13:12:05 +0000 (16:12 +0300)
Also set units for distance parameters.

gitlab #40

Change-Id: If5d39eccf4f93d8efd472c765ec4d8a79bffc203

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

index c724d89e770bb1b6b258f7460f474b1af1dd9214..5a9197a4edc64d5040963d85c2829145a2a34b1f 100644 (file)
@@ -23,7 +23,8 @@ from qgis.core import (QgsProcessing,
                        QgsProcessingParameterDistance,
                        QgsVectorDataProvider,
                        QgsFields,
-                       QgsField)
+                       QgsField,
+                       QgsUnitTypes)
 import processing
 import re
 
@@ -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)
@@ -227,10 +246,10 @@ class SpanCoordinatesAlgorithm(QgsProcessingAlgorithm):
             fatherID = feature[self.FATHER_ID]
             
             # Length
-            length = geometry.length()
+            myLength = geometry.length()
             
             oldLength = lengths.get(fatherID, 0.0)
-            lengths[fatherID] = oldLength + length
+            lengths[fatherID] = oldLength + myLength
             
             # Segment points
             pointList = strings.get(fatherID, [])
@@ -267,7 +286,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,7 +350,7 @@ 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
             
             label = feature['Label']