Skip to content

Commit

Permalink
[processing] restore one side buffer algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Oct 11, 2017
1 parent f59e3a3 commit 3750a5b
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 166 deletions.
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/gdal/Buffer.py
Expand Up @@ -117,7 +117,7 @@ def getConsoleCommands(self, parameters, context, feedback):
arguments.append('sqlite')
arguments.append('-sql')

if dissolve or field:
if dissolve or fieldName:
sql = "SELECT ST_Union(ST_Buffer({}, {})), * FROM '{}'".format(geometry, distance, layerName)
else:
sql = "SELECT ST_Buffer({}, {}), * FROM '{}'".format(geometry, distance, layerName)
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/gdal/GdalAlgorithmProvider.py
Expand Up @@ -77,14 +77,14 @@
from .OffsetCurve import OffsetCurve
from .ogrinfo import ogrinfo
from .OgrToPostGis import OgrToPostGis
from .OneSideBuffer import OneSideBuffer
from .PointsAlongLines import PointsAlongLines

# from .ogr2ogr import Ogr2Ogr
# from .ogr2ogrclip import Ogr2OgrClip
# from .ogr2ogrclipextent import Ogr2OgrClipExtent
# from .ogr2ogrtopostgislist import Ogr2OgrToPostGisList
# from .ogr2ogrdissolve import Ogr2OgrDissolve
# from .onesidebuffer import OneSideBuffer
# from .ogr2ogrtabletopostgislist import Ogr2OgrTableToPostGisList
# from .ogrsql import OgrSql

Expand Down Expand Up @@ -179,13 +179,13 @@ def loadAlgorithms(self):
OffsetCurve(),
ogrinfo(),
OgrToPostGis(),
OneSideBuffer(),
PointsAlongLines(),
# Ogr2Ogr(),
# Ogr2OgrClip(),
# Ogr2OgrClipExtent(),
# Ogr2OgrToPostGisList(),
# Ogr2OgrDissolve(),
# OneSideBuffer(),
# Ogr2OgrTableToPostGisList(),
# OgrSql(),
]
Expand Down
148 changes: 148 additions & 0 deletions python/plugins/processing/algs/gdal/OneSideBuffer.py
@@ -0,0 +1,148 @@
# -*- coding: utf-8 -*-

"""
***************************************************************************
OneSideBuffer.py
---------------------
Date : Janaury 2015
Copyright : (C) 2015 by Giovanni Manghi
Email : giovanni dot manghi at naturalgis dot pt
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""

__author__ = 'Giovanni Manghi'
__date__ = 'January 2015'
__copyright__ = '(C) 2015, Giovanni Manghi'

# This will get replaced with a git SHA1 when you do a git archive

__revision__ = '$Format:%H$'

from qgis.core import (QgsProcessing,
QgsProcessingParameterDefinition,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterEnum,
QgsProcessingParameterField,
QgsProcessingParameterString,
QgsProcessingParameterNumber,
QgsProcessingParameterBoolean,
QgsProcessingParameterVectorDestination)
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
from processing.algs.gdal.GdalUtils import GdalUtils


class OneSideBuffer(GdalAlgorithm):

INPUT = 'INPUT'
FIELD = 'FIELD'
BUFFER_SIDE = 'BUFFER_SIDE'
GEOMETRY = 'GEOMETRY'
DISTANCE = 'DISTANCE'
DISSOLVE = 'DISSOLVE'
EXPLODE_COLLECTIONS = 'EXPLODE_COLLECTIONS'
OPTIONS = 'OPTIONS'
OUTPUT = 'OUTPUT'

def __init__(self):
super().__init__()

def initAlgorithm(self, config=None):
self.bufferSides = [self.tr('Right'), self.tr('Left')]

self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
self.tr('Input layer'),
[QgsProcessing.TypeVectorLine]))
self.addParameter(QgsProcessingParameterString(self.GEOMETRY,
self.tr('Geometry column name'),
defaultValue='geometry'))
self.addParameter(QgsProcessingParameterNumber(self.DISTANCE,
self.tr('Buffer distance'),
type=QgsProcessingParameterNumber.Double,
defaultValue=10))
self.addParameter(QgsProcessingParameterEnum(self.BUFFER_SIDE,
self.tr('Buffer side'),
options=self.bufferSides,
allowMultiple=False,
defaultValue=0))
self.addParameter(QgsProcessingParameterField(self.FIELD,
self.tr('Dissolve by attribute'),
None,
self.INPUT,
QgsProcessingParameterField.Any,
optional=True))
self.addParameter(QgsProcessingParameterBoolean(self.DISSOLVE,
self.tr('Dissolve all results'),
defaultValue=False))
self.addParameter(QgsProcessingParameterBoolean(self.EXPLODE_COLLECTIONS,
self.tr('Produce one feature for each geometry in any kind of geometry collection in the source file'),
defaultValue=False))

options_param = QgsProcessingParameterString(self.OPTIONS,
self.tr('Additional creation options'),
defaultValue='',
optional=True)
options_param.setFlags(options_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
self.addParameter(options_param)

self.addParameter(QgsProcessingParameterVectorDestination(self.OUTPUT,
self.tr('One-sided buffer'),
QgsProcessing.TypeVectorPolygon))

def name(self):
return 'onesidebuffer'

def displayName(self):
return self.tr('One side buffer')

def group(self):
return self.tr('Vector geoprocessing')

def commandName(self):
return 'ogr2ogr'

def getConsoleCommands(self, parameters, context, feedback):
ogrLayer, layerName = self.getOgrCompatibleSource(self.INPUT, parameters, context, feedback)
geometry = self.parameterAsString(parameters, self.GEOMETRY, context)
distance = self.parameterAsDouble(parameters, self.DISTANCE, context)
side = self.parameterAsEnum(parameters, self.BUFFER_SIDE, context)
fieldName = self.parameterAsString(parameters, self.FIELD, context)
dissolve = self.parameterAsString(parameters, self.DISSOLVE, context)
options = self.parameterAsString(parameters, self.OPTIONS, context)
outFile = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)

output, outputFormat = GdalUtils.ogrConnectionStringAndFormat(outFile, context)

arguments = []
arguments.append(output)
arguments.append(ogrLayer)
arguments.append('-dialect')
arguments.append('sqlite')
arguments.append('-sql')

if dissolve or fieldName:
sql = "SELECT ST_Union(ST_SingleSidedBuffer({}, {}, {})), * FROM '{}'".format(geometry, distance, side, layerName)
else:
sql = "SELECT ST_SingleSidedBuffer({}, {}, {}), * FROM '{}'".format(geometry, distance, side, layerName)

if fieldName:
sql = '"{} GROUP BY {}"'.format(sql, fieldName)

arguments.append(sql)

if self.parameterAsBool(parameters, self.EXPLODE_COLLECTIONS, context):
arguments.append('-explodecollections')

if options:
arguments.append(options)

if outputFormat:
arguments.append('-f {}'.format(outputFormat))

return ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]
144 changes: 0 additions & 144 deletions python/plugins/processing/algs/gdal/onesidebuffer.py

This file was deleted.

37 changes: 18 additions & 19 deletions python/plugins/processing/tests/testdata/gdal_algorithm_tests.yaml
Expand Up @@ -545,22 +545,21 @@ tests:
geometry:
precision: 7

# - algorithm: gdal:singlesidedbufferforlines
# name: One-side buffer for lines (left-handed)
# params:
# DISSOLVEALL: false
# GEOMETRY: geometry
# INPUT_LAYER:
# name: lines.gml
# type: vector
# LEFTRIGHT: '1'
# MULTI: false
# RADIUS: 0.5
# results:
# OUTPUT_LAYER:
# name: expected/gdal/one_side_buffer.gml
# type: vector
# compare:
# geometry:
# precision: 7
#
- algorithm: gdal:singlesidedbuffer
name: One-side buffer for lines (left-handed)
params:
DISSOLVEALL: false
GEOMETRY: geometry
INPUT_LAYER:
name: lines.gml
type: vector
LEFTRIGHT: '1'
MULTI: false
RADIUS: 0.5
results:
OUTPUT_LAYER:
name: expected/gdal/one_side_buffer.gml
type: vector
compare:
geometry:
precision: 7

0 comments on commit 3750a5b

Please sign in to comment.