From 78f2f724d5ac36e59fbd7a303ec8b2e9e738f28f Mon Sep 17 00:00:00 2001 From: mauroalberti Date: Sun, 2 Jun 2019 18:55:20 +0200 Subject: [PATCH] Implemented smoothing parameters --- beePen_QWidget.py | 2 +- beePen_gui.py | 58 ++++++++++++++++++-------- dialog_line.ui | 93 ++++++++++++++++++++++++++++-------------- freehandeditingtool.py | 1 + 4 files changed, 105 insertions(+), 49 deletions(-) diff --git a/beePen_QWidget.py b/beePen_QWidget.py index baec9a7..9d85788 100644 --- a/beePen_QWidget.py +++ b/beePen_QWidget.py @@ -136,7 +136,7 @@ def setup_gui(self): pen_layout.addWidget(self.pencolor_QgsColorButtonV2) - line_advanced_QPushButton = QPushButton("Advanced parameters") + line_advanced_QPushButton = QPushButton("Smooth parameters") line_advanced_QPushButton.clicked.connect(self.open_advanced_form) pen_layout.addWidget(line_advanced_QPushButton) diff --git a/beePen_gui.py b/beePen_gui.py index 4b225e0..cde66eb 100644 --- a/beePen_gui.py +++ b/beePen_gui.py @@ -206,37 +206,61 @@ def createFeature(self, geom): layerCRSSrsid = layer_crs.srsid() projectCRSSrsid = project_crs.srsid() - f = QgsFeature() + feature = QgsFeature() + + # on-the-fly re-projection + + if layerCRSSrsid != projectCRSSrsid: + geom.transform( + QgsCoordinateTransform( + project_crs, + layer_crs, + QgsProject.instance())) + + # simplification and smoothing section + + settings = QSettings() + simplify_tolerance=settings.value("/%s/simplify_tolerance" % self.plugin_name, 0.0, type=float) + smooth_iterations=settings.value("/%s/smooth_iterations" % self.plugin_name, 0, type=int) + smooth_offset=settings.value("/%s/smooth_offset" % self.plugin_name, 0.25, type=float) + smooth_mindistance=settings.value("/%s/smooth_mindistance" % self.plugin_name, -1.0, type=float) + smooth_maxangle=settings.value("/%s/smooth_maxangle" % self.plugin_name, 180.0, type=float) + + # let the feature unchanged when CRS is in polar coordinates if layer.crs().projectionAcronym() == "longlat": - tolerance = 0.000 + simplify_tolerance = 0.0 + smooth_iterations = 0 + + if simplify_tolerance: + s = geom.simplify(simplify_tolerance) else: - settings = QSettings() - tolerance = settings.value("/%s/tolerance" % self.plugin_name, - 0.000, - type=float) + s = geom - # on-the-fly re-projection + if smooth_iterations: + s = s.smooth( + iterations=smooth_iterations, + offset=smooth_offset, + minimumDistance=smooth_mindistance, + maxAngle=smooth_maxangle + ) - if layerCRSSrsid != projectCRSSrsid: - geom.transform(QgsCoordinateTransform(project_crs, - layer_crs, - QgsProject.instance())) - s = geom.simplify(tolerance) - del geom + feature.setGeometry(s) - f.setGeometry(s) + del geom # add attribute fields to feature fields = layer.fields() - f.initAttributes(fields.count()) + feature.initAttributes(fields.count()) record_values = [self.beePen_QWidget.pencil_width, self.beePen_QWidget.color_name, note] for ndx, value in enumerate(record_values): - f.setAttribute(ndx, value) - layer.addFeature(f) + feature.setAttribute(ndx, value) + + layer.addFeature(feature) + layer.commitChanges() def deleteFeatures(self, geom): diff --git a/dialog_line.ui b/dialog_line.ui index 6a5f1d5..fe54dd1 100644 --- a/dialog_line.ui +++ b/dialog_line.ui @@ -6,8 +6,8 @@ 0 0 - 291 - 430 + 344 + 459 @@ -16,8 +16,8 @@ - 30 - 360 + 50 + 410 191 32 @@ -32,8 +32,8 @@ - 171 - 70 + 200 + 133 71 32 @@ -45,8 +45,8 @@ - 31 - 30 + 60 + 93 141 18 @@ -69,8 +69,8 @@ - 31 - 130 + 60 + 187 141 18 @@ -93,8 +93,8 @@ - 40 - 77 + 69 + 140 60 18 @@ -106,8 +106,8 @@ - 171 - 169 + 200 + 226 71 32 @@ -115,15 +115,12 @@ <html><head/><body><p>Number of smoothing iterations to run. More iterations results in a smoother geometry</p></body></html> - - 1 - - 40 - 176 + 69 + 233 60 18 @@ -138,8 +135,8 @@ - 40 - 214 + 69 + 271 38 18 @@ -154,8 +151,8 @@ - 171 - 207 + 200 + 264 71 32 @@ -176,8 +173,8 @@ - 171 - 245 + 200 + 302 71 32 @@ -195,8 +192,8 @@ - 40 - 252 + 69 + 309 114 18 @@ -211,8 +208,8 @@ - 40 - 290 + 69 + 347 99 18 @@ -227,8 +224,8 @@ - 171 - 283 + 200 + 340 71 32 @@ -243,6 +240,40 @@ 180.000000000000000 + + + + 10 + 10 + 701 + 51 + + + + + 11 + 75 + true + true + + + + Line smoothing parameters + + + + + + 20 + 50 + 321 + 18 + + + + (work only for projected layers, i.e. not in lat-long) + + diff --git a/freehandeditingtool.py b/freehandeditingtool.py index 9cf5e8e..eef655d 100644 --- a/freehandeditingtool.py +++ b/freehandeditingtool.py @@ -264,6 +264,7 @@ def canvasReleaseEvent(self, event): geom = None # reset rubberband and refresh the canvas + self.rb.reset() self.rb = None self.canvas.refresh()