Skip to content

Commit f7bdd66

Browse files
committed
Merge pull request #2481 from nyalldawson/processing
Processing enhancements and fixes
2 parents e0bedc2 + d0625b8 commit f7bdd66

File tree

6 files changed

+214
-13
lines changed

6 files changed

+214
-13
lines changed

python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@
131131
from Datasources2Vrt import Datasources2Vrt
132132
from CheckValidity import CheckValidity
133133
from OrientedMinimumBoundingBox import OrientedMinimumBoundingBox
134+
from Smooth import Smooth
135+
from ReverseLineDirection import ReverseLineDirection
134136

135137
pluginPath = os.path.normpath(os.path.join(
136138
os.path.split(os.path.dirname(__file__))[0], os.pardir))
@@ -179,7 +181,8 @@ def __init__(self):
179181
SelectByExpression(), HypsometricCurves(),
180182
SplitLinesWithLines(), CreateConstantRaster(),
181183
FieldsMapper(), SelectByAttributeSum(), Datasources2Vrt(),
182-
CheckValidity(), OrientedMinimumBoundingBox()
184+
CheckValidity(), OrientedMinimumBoundingBox(), Smooth(),
185+
ReverseLineDirection()
183186
]
184187

185188
if hasMatplotlib:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
ReverseLineDirection.py
6+
-----------------------
7+
Date : November 2015
8+
Copyright : (C) 2015 by Nyall Dawson
9+
Email : nyall dot dawson at gmail dot com
10+
***************************************************************************
11+
* *
12+
* This program is free software; you can redistribute it and/or modify *
13+
* it under the terms of the GNU General Public License as published by *
14+
* the Free Software Foundation; either version 2 of the License, or *
15+
* (at your option) any later version. *
16+
* *
17+
***************************************************************************
18+
"""
19+
20+
__author__ = 'Nyall Dawson'
21+
__date__ = 'November 2015'
22+
__copyright__ = '(C) 2015, Nyall Dawson'
23+
24+
# This will get replaced with a git SHA1 when you do a git archive323
25+
26+
__revision__ = '$Format:%H$'
27+
28+
from qgis.core import QGis, QgsGeometry, QgsFeature
29+
from processing.core.GeoAlgorithm import GeoAlgorithm
30+
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
31+
from processing.core.parameters import ParameterVector, ParameterNumber
32+
from processing.core.outputs import OutputVector
33+
from processing.tools import dataobjects, vector
34+
35+
36+
class ReverseLineDirection(GeoAlgorithm):
37+
38+
INPUT_LAYER = 'INPUT_LAYER'
39+
OUTPUT_LAYER = 'OUTPUT_LAYER'
40+
41+
def defineCharacteristics(self):
42+
self.name, self.i18n_name = self.trAlgorithm('Reverse line direction')
43+
self.group, self.i18n_group = self.trAlgorithm('Vector geometry tools')
44+
45+
self.addParameter(ParameterVector(self.INPUT_LAYER,
46+
self.tr('Input layer'), [ParameterVector.VECTOR_TYPE_LINE]))
47+
self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Reversed')))
48+
49+
def processAlgorithm(self, progress):
50+
layer = dataobjects.getObjectFromUri(
51+
self.getParameterValue(self.INPUT_LAYER))
52+
provider = layer.dataProvider()
53+
54+
writer = self.getOutputFromName(
55+
self.OUTPUT_LAYER).getVectorWriter(
56+
layer.fields().toList(),
57+
provider.geometryType(),
58+
layer.crs())
59+
60+
outFeat = QgsFeature()
61+
62+
features = vector.features(layer)
63+
total = 100.0 / float(len(features))
64+
current = 0
65+
66+
for inFeat in features:
67+
inGeom = inFeat.constGeometry()
68+
attrs = inFeat.attributes()
69+
70+
outGeom = None
71+
if inGeom and not inGeom.isEmpty():
72+
reversedLine = inGeom.geometry().reversed()
73+
if reversedLine is None:
74+
raise GeoAlgorithmExecutionException(
75+
self.tr('Error reversing line'))
76+
outGeom = QgsGeometry(reversedLine)
77+
78+
outFeat.setGeometry(outGeom)
79+
outFeat.setAttributes(attrs)
80+
writer.addFeature(outFeat)
81+
current += 1
82+
progress.setPercentage(int(current * total))
83+
84+
del writer
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
Smooth.py
6+
---------
7+
Date : November 2015
8+
Copyright : (C) 2015 by Nyall Dawson
9+
Email : nyall dot dawson at gmail dot com
10+
***************************************************************************
11+
* *
12+
* This program is free software; you can redistribute it and/or modify *
13+
* it under the terms of the GNU General Public License as published by *
14+
* the Free Software Foundation; either version 2 of the License, or *
15+
* (at your option) any later version. *
16+
* *
17+
***************************************************************************
18+
"""
19+
20+
__author__ = 'Nyall Dawson'
21+
__date__ = 'November 2015'
22+
__copyright__ = '(C) 2015, Nyall Dawson'
23+
24+
# This will get replaced with a git SHA1 when you do a git archive323
25+
26+
__revision__ = '$Format:%H$'
27+
28+
from qgis.core import QGis, QgsGeometry, QgsFeature
29+
from processing.core.GeoAlgorithm import GeoAlgorithm
30+
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
31+
from processing.core.parameters import ParameterVector, ParameterNumber
32+
from processing.core.outputs import OutputVector
33+
from processing.tools import dataobjects, vector
34+
35+
36+
class Smooth(GeoAlgorithm):
37+
38+
INPUT_LAYER = 'INPUT_LAYER'
39+
OUTPUT_LAYER = 'OUTPUT_LAYER'
40+
ITERATIONS = 'ITERATIONS'
41+
OFFSET = 'OFFSET'
42+
43+
def defineCharacteristics(self):
44+
self.name, self.i18n_name = self.trAlgorithm('Smooth geometry')
45+
self.group, self.i18n_group = self.trAlgorithm('Vector geometry tools')
46+
47+
self.addParameter(ParameterVector(self.INPUT_LAYER,
48+
self.tr('Input layer'), [ParameterVector.VECTOR_TYPE_POLYGON, ParameterVector.VECTOR_TYPE_LINE]))
49+
self.addParameter(ParameterNumber(self.ITERATIONS,
50+
self.tr('Iterations'), default=1, minValue=1, maxValue=10))
51+
self.addParameter(ParameterNumber(self.OFFSET,
52+
self.tr('Offset'), default=0.25, minValue=0.0, maxValue=0.5))
53+
self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Smoothed')))
54+
55+
def processAlgorithm(self, progress):
56+
layer = dataobjects.getObjectFromUri(
57+
self.getParameterValue(self.INPUT_LAYER))
58+
provider = layer.dataProvider()
59+
iterations = self.getParameterValue(self.ITERATIONS)
60+
offset = self.getParameterValue(self.OFFSET)
61+
62+
writer = self.getOutputFromName(
63+
self.OUTPUT_LAYER).getVectorWriter(
64+
layer.fields().toList(),
65+
provider.geometryType(),
66+
layer.crs())
67+
68+
outFeat = QgsFeature()
69+
70+
features = vector.features(layer)
71+
total = 100.0 / float(len(features))
72+
current = 0
73+
74+
for inFeat in features:
75+
inGeom = inFeat.constGeometry()
76+
attrs = inFeat.attributes()
77+
78+
outGeom = inGeom.smooth(iterations, offset)
79+
if outGeom is None:
80+
raise GeoAlgorithmExecutionException(
81+
self.tr('Error smoothing geometry'))
82+
83+
outFeat.setGeometry(outGeom)
84+
outFeat.setAttributes(attrs)
85+
writer.addFeature(outFeat)
86+
current += 1
87+
progress.setPercentage(int(current * total))
88+
89+
del writer

python/plugins/processing/gui/ConfigDialog.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@
3232
from PyQt4.QtCore import Qt, QEvent, QPyNullVariant
3333
from PyQt4.QtGui import (QFileDialog, QDialog, QIcon, QStyle,
3434
QStandardItemModel, QStandardItem, QMessageBox, QStyledItemDelegate,
35-
QLineEdit, QSpinBox, QDoubleSpinBox, QWidget, QToolButton, QHBoxLayout,
35+
QLineEdit, QWidget, QToolButton, QHBoxLayout,
3636
QComboBox)
37+
from qgis.gui import QgsDoubleSpinBox, QgsSpinBox
3738

3839
from processing.core.ProcessingConfig import ProcessingConfig, Setting
3940
from processing.core.Processing import Processing
@@ -198,11 +199,11 @@ def createEditor(
198199
else:
199200
value = self.convertValue(index.model().data(index, Qt.EditRole))
200201
if isinstance(value, (int, long)):
201-
spnBox = QSpinBox(parent)
202+
spnBox = QgsSpinBox(parent)
202203
spnBox.setRange(-999999999, 999999999)
203204
return spnBox
204205
elif isinstance(value, float):
205-
spnBox = QDoubleSpinBox(parent)
206+
spnBox = QgsDoubleSpinBox(parent)
206207
spnBox.setRange(-999999999.999999, 999999999.999999)
207208
spnBox.setDecimals(6)
208209
return spnBox

python/plugins/processing/gui/NumberInputPanel.py

+24-8
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
from PyQt4 import uic
3131

32+
from math import log10, floor
3233
from processing.gui.NumberInputDialog import NumberInputDialog
3334

3435
pluginPath = os.path.split(os.path.dirname(__file__))[0]
@@ -45,16 +46,22 @@ def __init__(self, number, minimum, maximum, isInteger):
4546
self.isInteger = isInteger
4647
if self.isInteger:
4748
self.spnValue.setDecimals(0)
48-
if maximum == 0 or maximum:
49-
self.spnValue.setMaximum(maximum)
50-
else:
51-
self.spnValue.setMaximum(99999999)
52-
if minimum == 0 or minimum:
53-
self.spnValue.setMinimum(minimum)
54-
else:
55-
self.spnValue.setMinimum(-99999999)
49+
else:
50+
#Guess reasonable step value
51+
if (maximum == 0 or maximum) and (minimum == 0 or minimum):
52+
self.spnValue.setSingleStep(self.calculateStep(minimum, maximum))
53+
54+
if maximum == 0 or maximum:
55+
self.spnValue.setMaximum(maximum)
56+
else:
57+
self.spnValue.setMaximum(99999999)
58+
if minimum == 0 or minimum:
59+
self.spnValue.setMinimum(minimum)
60+
else:
61+
self.spnValue.setMinimum(-99999999)
5662

5763
self.spnValue.setValue(float(number))
64+
self.spnValue.setClearValue(float(number))
5865

5966
self.btnCalc.clicked.connect(self.showNumberInputDialog)
6067

@@ -66,3 +73,12 @@ def showNumberInputDialog(self):
6673

6774
def getValue(self):
6875
return self.spnValue.value()
76+
77+
def calculateStep(self, minimum, maximum):
78+
valueRange = maximum - minimum
79+
if valueRange <= 1.0:
80+
step = valueRange / 10.0
81+
# round to 1 significant figure
82+
return round(step, -int(floor(log10(step))))
83+
else:
84+
return 1.0

python/plugins/processing/ui/widgetNumberSelector.ui

+9-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<number>0</number>
2222
</property>
2323
<item>
24-
<widget class="QDoubleSpinBox" name="spnValue">
24+
<widget class="QgsDoubleSpinBox" name="spnValue">
2525
<property name="decimals">
2626
<number>6</number>
2727
</property>
@@ -45,6 +45,14 @@
4545
</item>
4646
</layout>
4747
</widget>
48+
<customwidgets>
49+
<customwidget>
50+
<class>QgsDoubleSpinBox</class>
51+
<extends>QDoubleSpinBox</extends>
52+
<header>qgis.gui</header>
53+
<container>1</container>
54+
</customwidget>
55+
</customwidgets>
4856
<resources/>
4957
<connections/>
5058
</ui>

0 commit comments

Comments
 (0)