|
| 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)] |
0 commit comments