Skip to content

Commit bba509e

Browse files
committed
[processing] fixes to Offset curve algorithm
1 parent fde5c89 commit bba509e

File tree

2 files changed

+150
-4
lines changed

2 files changed

+150
-4
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
Buffer.py
6+
---------------------
7+
Date : Janaury 2015
8+
Copyright : (C) 2015 by Giovanni Manghi
9+
Email : giovanni dot manghi at naturalgis dot pt
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__ = 'Giovanni Manghi'
21+
__date__ = 'January 2015'
22+
__copyright__ = '(C) 2015, Giovanni Manghi'
23+
24+
# This will get replaced with a git SHA1 when you do a git archive
25+
26+
__revision__ = '$Format:%H$'
27+
28+
from qgis.core import (QgsProcessing,
29+
QgsProcessingParameterDefinition,
30+
QgsProcessingParameterFeatureSource,
31+
QgsProcessingParameterField,
32+
QgsProcessingParameterString,
33+
QgsProcessingParameterNumber,
34+
QgsProcessingParameterBoolean,
35+
QgsProcessingParameterVectorDestination,
36+
QgsProcessingOutputVectorLayer)
37+
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
38+
from processing.algs.gdal.GdalUtils import GdalUtils
39+
40+
41+
class Buffer(GdalAlgorithm):
42+
43+
INPUT = 'INPUT'
44+
FIELD = 'FIELD'
45+
GEOMETRY = 'GEOMETRY'
46+
DISTANCE = 'DISTANCE'
47+
DISSOLVE = 'DISSOLVE'
48+
EXPLODE_COLLECTIONS = 'EXPLODE_COLLECTIONS'
49+
OPTIONS = 'OPTIONS'
50+
OUTPUT = 'OUTPUT'
51+
52+
def __init__(self):
53+
super().__init__()
54+
55+
def initAlgorithm(self, config=None):
56+
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
57+
self.tr('Input layer')))
58+
self.addParameter(QgsProcessingParameterString(self.GEOMETRY,
59+
self.tr('Geometry column name'),
60+
defaultValue='geometry'))
61+
self.addParameter(QgsProcessingParameterNumber(self.DISTANCE,
62+
self.tr('Buffer distance'),
63+
type=QgsProcessingParameterNumber.Double,
64+
minValue=0.0,
65+
defaultValue=10.0))
66+
self.addParameter(QgsProcessingParameterField(self.FIELD,
67+
self.tr('Dissolve by attribute'),
68+
None,
69+
self.INPUT,
70+
QgsProcessingParameterField.Any,
71+
optional=True))
72+
self.addParameter(QgsProcessingParameterBoolean(self.DISSOLVE,
73+
self.tr('Dissolve all results'),
74+
defaultValue=False))
75+
self.addParameter(QgsProcessingParameterBoolean(self.EXPLODE_COLLECTIONS,
76+
self.tr('Produce one feature for each geometry in any kind of geometry collection in the source file'),
77+
defaultValue=False))
78+
79+
options_param = QgsProcessingParameterString(self.OPTIONS,
80+
self.tr('Additional creation options'),
81+
defaultValue='',
82+
optional=True)
83+
options_param.setFlags(options_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
84+
self.addParameter(options_param)
85+
86+
self.addParameter(QgsProcessingParameterVectorDestination(self.OUTPUT,
87+
self.tr('Buffer'),
88+
QgsProcessing.TypeVectorPolygon))
89+
90+
def name(self):
91+
return 'buffervectors'
92+
93+
def displayName(self):
94+
return self.tr('Buffer vectors')
95+
96+
def group(self):
97+
return self.tr('Vector geoprocessing')
98+
99+
def commandName(self):
100+
return 'ogr2ogr'
101+
102+
def getConsoleCommands(self, parameters, context, feedback):
103+
ogrLayer, layerName = self.getOgrCompatibleSource(self.INPUT, parameters, context, feedback)
104+
geometry = self.parameterAsString(parameters, self.GEOMETRY, context)
105+
distance = self.parameterAsDouble(parameters, self.DISTANCE, context)
106+
fieldName = self.parameterAsString(parameters, self.FIELD, context)
107+
dissolve = self.parameterAsString(parameters, self.DISSOLVE, context)
108+
options = self.parameterAsString(parameters, self.OPTIONS, context)
109+
outFile = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
110+
111+
output, outputFormat = GdalUtils.ogrConnectionStringAndFormat(outFile, context)
112+
113+
arguments = []
114+
arguments.append(output)
115+
arguments.append(ogrLayer)
116+
arguments.append('-dialect')
117+
arguments.append('sqlite')
118+
arguments.append('-sql')
119+
120+
if dissolve or field:
121+
sql = "SELECT ST_Union(ST_Buffer({}, {})), * FROM '{}'".format(geometry, distance, layerName)
122+
else:
123+
sql = "SELECT ST_Buffer({}, {}), * FROM '{}'".format(geometry, distance, layerName)
124+
125+
if fieldName:
126+
sql = '{} GROUP BY {}'.format(sql, fieldName)
127+
128+
arguments.append(sql)
129+
130+
if self.parameterAsBool(parameters, self.EXPLODE_COLLECTIONS, context):
131+
arguments.append('-explodecollections')
132+
133+
if options:
134+
arguments.append(options)
135+
136+
if outputFormat:
137+
arguments.append('-f {}'.format(outputFormat))
138+
139+
return ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]

python/plugins/processing/algs/gdal/OffsetCurve.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
__revision__ = '$Format:%H$'
2727

2828
from qgis.core import (QgsProcessing,
29+
QgsProcessingParameterDefinition,
2930
QgsProcessingParameterFeatureSource,
3031
QgsProcessingParameterString,
3132
QgsProcessingParameterNumber,
@@ -57,10 +58,13 @@ def initAlgorithm(self, config=None):
5758
self.tr('Offset distance (left-sided: positive, right-sided: negative)'),
5859
type=QgsProcessingParameterNumber.Double,
5960
defaultValue=10))
60-
self.addParameter(QgsProcessingParameterString(self.OPTIONS,
61-
self.tr('Additional creation options'),
62-
defaultValue='',
63-
optional=True))
61+
62+
options_param = QgsProcessingParameterString(self.OPTIONS,
63+
self.tr('Additional creation options'),
64+
defaultValue='',
65+
optional=True)
66+
options_param.setFlags(options_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
67+
self.addParameter(options_param)
6468

6569
self.addParameter(QgsProcessingParameterVectorDestination(self.OUTPUT,
6670
self.tr('Offset curve'),
@@ -100,4 +104,7 @@ def getConsoleCommands(self, parameters, context, feedback):
100104
if options:
101105
arguments.append(options)
102106

107+
if outputFormat:
108+
arguments.append('-f {}'.format(outputFormat))
109+
103110
return ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]

0 commit comments

Comments
 (0)