Skip to content

Commit 3750a5b

Browse files
committedOct 11, 2017
[processing] restore one side buffer algorithm
1 parent f59e3a3 commit 3750a5b

File tree

5 files changed

+169
-166
lines changed

5 files changed

+169
-166
lines changed
 

‎python/plugins/processing/algs/gdal/Buffer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def getConsoleCommands(self, parameters, context, feedback):
117117
arguments.append('sqlite')
118118
arguments.append('-sql')
119119

120-
if dissolve or field:
120+
if dissolve or fieldName:
121121
sql = "SELECT ST_Union(ST_Buffer({}, {})), * FROM '{}'".format(geometry, distance, layerName)
122122
else:
123123
sql = "SELECT ST_Buffer({}, {}), * FROM '{}'".format(geometry, distance, layerName)

‎python/plugins/processing/algs/gdal/GdalAlgorithmProvider.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,14 @@
7777
from .OffsetCurve import OffsetCurve
7878
from .ogrinfo import ogrinfo
7979
from .OgrToPostGis import OgrToPostGis
80+
from .OneSideBuffer import OneSideBuffer
8081
from .PointsAlongLines import PointsAlongLines
8182

8283
# from .ogr2ogr import Ogr2Ogr
8384
# from .ogr2ogrclip import Ogr2OgrClip
8485
# from .ogr2ogrclipextent import Ogr2OgrClipExtent
8586
# from .ogr2ogrtopostgislist import Ogr2OgrToPostGisList
8687
# from .ogr2ogrdissolve import Ogr2OgrDissolve
87-
# from .onesidebuffer import OneSideBuffer
8888
# from .ogr2ogrtabletopostgislist import Ogr2OgrTableToPostGisList
8989
# from .ogrsql import OgrSql
9090

@@ -179,13 +179,13 @@ def loadAlgorithms(self):
179179
OffsetCurve(),
180180
ogrinfo(),
181181
OgrToPostGis(),
182+
OneSideBuffer(),
182183
PointsAlongLines(),
183184
# Ogr2Ogr(),
184185
# Ogr2OgrClip(),
185186
# Ogr2OgrClipExtent(),
186187
# Ogr2OgrToPostGisList(),
187188
# Ogr2OgrDissolve(),
188-
# OneSideBuffer(),
189189
# Ogr2OgrTableToPostGisList(),
190190
# OgrSql(),
191191
]
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
OneSideBuffer.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+
QgsProcessingParameterEnum,
32+
QgsProcessingParameterField,
33+
QgsProcessingParameterString,
34+
QgsProcessingParameterNumber,
35+
QgsProcessingParameterBoolean,
36+
QgsProcessingParameterVectorDestination)
37+
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
38+
from processing.algs.gdal.GdalUtils import GdalUtils
39+
40+
41+
class OneSideBuffer(GdalAlgorithm):
42+
43+
INPUT = 'INPUT'
44+
FIELD = 'FIELD'
45+
BUFFER_SIDE = 'BUFFER_SIDE'
46+
GEOMETRY = 'GEOMETRY'
47+
DISTANCE = 'DISTANCE'
48+
DISSOLVE = 'DISSOLVE'
49+
EXPLODE_COLLECTIONS = 'EXPLODE_COLLECTIONS'
50+
OPTIONS = 'OPTIONS'
51+
OUTPUT = 'OUTPUT'
52+
53+
def __init__(self):
54+
super().__init__()
55+
56+
def initAlgorithm(self, config=None):
57+
self.bufferSides = [self.tr('Right'), self.tr('Left')]
58+
59+
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
60+
self.tr('Input layer'),
61+
[QgsProcessing.TypeVectorLine]))
62+
self.addParameter(QgsProcessingParameterString(self.GEOMETRY,
63+
self.tr('Geometry column name'),
64+
defaultValue='geometry'))
65+
self.addParameter(QgsProcessingParameterNumber(self.DISTANCE,
66+
self.tr('Buffer distance'),
67+
type=QgsProcessingParameterNumber.Double,
68+
defaultValue=10))
69+
self.addParameter(QgsProcessingParameterEnum(self.BUFFER_SIDE,
70+
self.tr('Buffer side'),
71+
options=self.bufferSides,
72+
allowMultiple=False,
73+
defaultValue=0))
74+
self.addParameter(QgsProcessingParameterField(self.FIELD,
75+
self.tr('Dissolve by attribute'),
76+
None,
77+
self.INPUT,
78+
QgsProcessingParameterField.Any,
79+
optional=True))
80+
self.addParameter(QgsProcessingParameterBoolean(self.DISSOLVE,
81+
self.tr('Dissolve all results'),
82+
defaultValue=False))
83+
self.addParameter(QgsProcessingParameterBoolean(self.EXPLODE_COLLECTIONS,
84+
self.tr('Produce one feature for each geometry in any kind of geometry collection in the source file'),
85+
defaultValue=False))
86+
87+
options_param = QgsProcessingParameterString(self.OPTIONS,
88+
self.tr('Additional creation options'),
89+
defaultValue='',
90+
optional=True)
91+
options_param.setFlags(options_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
92+
self.addParameter(options_param)
93+
94+
self.addParameter(QgsProcessingParameterVectorDestination(self.OUTPUT,
95+
self.tr('One-sided buffer'),
96+
QgsProcessing.TypeVectorPolygon))
97+
98+
def name(self):
99+
return 'onesidebuffer'
100+
101+
def displayName(self):
102+
return self.tr('One side buffer')
103+
104+
def group(self):
105+
return self.tr('Vector geoprocessing')
106+
107+
def commandName(self):
108+
return 'ogr2ogr'
109+
110+
def getConsoleCommands(self, parameters, context, feedback):
111+
ogrLayer, layerName = self.getOgrCompatibleSource(self.INPUT, parameters, context, feedback)
112+
geometry = self.parameterAsString(parameters, self.GEOMETRY, context)
113+
distance = self.parameterAsDouble(parameters, self.DISTANCE, context)
114+
side = self.parameterAsEnum(parameters, self.BUFFER_SIDE, context)
115+
fieldName = self.parameterAsString(parameters, self.FIELD, context)
116+
dissolve = self.parameterAsString(parameters, self.DISSOLVE, context)
117+
options = self.parameterAsString(parameters, self.OPTIONS, context)
118+
outFile = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
119+
120+
output, outputFormat = GdalUtils.ogrConnectionStringAndFormat(outFile, context)
121+
122+
arguments = []
123+
arguments.append(output)
124+
arguments.append(ogrLayer)
125+
arguments.append('-dialect')
126+
arguments.append('sqlite')
127+
arguments.append('-sql')
128+
129+
if dissolve or fieldName:
130+
sql = "SELECT ST_Union(ST_SingleSidedBuffer({}, {}, {})), * FROM '{}'".format(geometry, distance, side, layerName)
131+
else:
132+
sql = "SELECT ST_SingleSidedBuffer({}, {}, {}), * FROM '{}'".format(geometry, distance, side, layerName)
133+
134+
if fieldName:
135+
sql = '"{} GROUP BY {}"'.format(sql, fieldName)
136+
137+
arguments.append(sql)
138+
139+
if self.parameterAsBool(parameters, self.EXPLODE_COLLECTIONS, context):
140+
arguments.append('-explodecollections')
141+
142+
if options:
143+
arguments.append(options)
144+
145+
if outputFormat:
146+
arguments.append('-f {}'.format(outputFormat))
147+
148+
return ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]

‎python/plugins/processing/algs/gdal/onesidebuffer.py

Lines changed: 0 additions & 144 deletions
This file was deleted.

‎python/plugins/processing/tests/testdata/gdal_algorithm_tests.yaml

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -545,22 +545,21 @@ tests:
545545
geometry:
546546
precision: 7
547547

548-
# - algorithm: gdal:singlesidedbufferforlines
549-
# name: One-side buffer for lines (left-handed)
550-
# params:
551-
# DISSOLVEALL: false
552-
# GEOMETRY: geometry
553-
# INPUT_LAYER:
554-
# name: lines.gml
555-
# type: vector
556-
# LEFTRIGHT: '1'
557-
# MULTI: false
558-
# RADIUS: 0.5
559-
# results:
560-
# OUTPUT_LAYER:
561-
# name: expected/gdal/one_side_buffer.gml
562-
# type: vector
563-
# compare:
564-
# geometry:
565-
# precision: 7
566-
#
548+
- algorithm: gdal:singlesidedbuffer
549+
name: One-side buffer for lines (left-handed)
550+
params:
551+
DISSOLVEALL: false
552+
GEOMETRY: geometry
553+
INPUT_LAYER:
554+
name: lines.gml
555+
type: vector
556+
LEFTRIGHT: '1'
557+
MULTI: false
558+
RADIUS: 0.5
559+
results:
560+
OUTPUT_LAYER:
561+
name: expected/gdal/one_side_buffer.gml
562+
type: vector
563+
compare:
564+
geometry:
565+
precision: 7

0 commit comments

Comments
 (0)
Please sign in to comment.