2015-02-05 22:55:44 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
ogr2ogronesidebuffer.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 processing.core.parameters import ParameterVector
|
|
|
|
from processing.core.parameters import ParameterString
|
|
|
|
from processing.core.parameters import ParameterNumber
|
|
|
|
from processing.core.parameters import ParameterBoolean
|
|
|
|
from processing.core.parameters import ParameterTableField
|
|
|
|
from processing.core.parameters import ParameterSelection
|
|
|
|
from processing.core.outputs import OutputVector
|
|
|
|
|
|
|
|
from processing.tools.system import isWindows
|
|
|
|
|
|
|
|
from processing.algs.gdal.OgrAlgorithm import OgrAlgorithm
|
|
|
|
from processing.algs.gdal.GdalUtils import GdalUtils
|
|
|
|
|
2015-08-22 14:29:41 +02:00
|
|
|
|
2015-02-05 22:55:44 +00:00
|
|
|
class Ogr2OgrOneSideBuffer(OgrAlgorithm):
|
|
|
|
|
|
|
|
OUTPUT_LAYER = 'OUTPUT_LAYER'
|
|
|
|
INPUT_LAYER = 'INPUT_LAYER'
|
|
|
|
OPERATION = 'OPERATION'
|
2015-08-22 14:29:41 +02:00
|
|
|
OPERATIONLIST = ['Single Side Buffer', 'Offset Curve']
|
2015-02-05 22:55:44 +00:00
|
|
|
GEOMETRY = 'GEOMETRY'
|
|
|
|
RADIUS = 'RADIUS'
|
|
|
|
LEFTRIGHT = 'LEFTRIGHT'
|
2015-08-22 14:29:41 +02:00
|
|
|
LEFTRIGHTLIST = ['Right', 'Left']
|
2015-02-05 22:55:44 +00:00
|
|
|
DISSOLVEALL = 'DISSOLVEALL'
|
|
|
|
FIELD = 'FIELD'
|
2015-02-24 12:35:00 +02:00
|
|
|
MULTI = 'MULTI'
|
2015-02-05 22:55:44 +00:00
|
|
|
OPTIONS = 'OPTIONS'
|
|
|
|
|
|
|
|
def defineCharacteristics(self):
|
2015-07-26 03:48:27 +02:00
|
|
|
self.name, self.i18n_name = self.trAlgorithm('Single sided buffers (and offset lines) for lines')
|
|
|
|
self.group, self.i18n_group = self.trAlgorithm('[OGR] Geoprocessing')
|
2015-02-05 22:55:44 +00:00
|
|
|
|
2015-02-24 12:35:00 +02:00
|
|
|
self.addParameter(ParameterVector(self.INPUT_LAYER,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Input layer'), [ParameterVector.VECTOR_TYPE_LINE], False))
|
2015-02-24 12:35:00 +02:00
|
|
|
self.addParameter(ParameterSelection(self.OPERATION,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Operation'), self.OPERATIONLIST, 0))
|
2015-02-24 12:35:00 +02:00
|
|
|
self.addParameter(ParameterString(self.GEOMETRY,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Geometry column name ("geometry" for Shapefiles, may be different for other formats)'),
|
|
|
|
'geometry', optional=False))
|
2015-02-24 12:35:00 +02:00
|
|
|
self.addParameter(ParameterString(self.RADIUS,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Buffer distance'), '1000', optional=False))
|
2015-02-24 12:35:00 +02:00
|
|
|
self.addParameter(ParameterSelection(self.LEFTRIGHT,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Buffer side'), self.LEFTRIGHTLIST, 0))
|
2015-02-05 22:55:44 +00:00
|
|
|
self.addParameter(ParameterBoolean(self.DISSOLVEALL,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Dissolve all results'), False))
|
2015-02-24 12:35:00 +02:00
|
|
|
self.addParameter(ParameterTableField(self.FIELD,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Dissolve by attribute'), self.INPUT_LAYER, optional=True))
|
2015-02-05 22:55:44 +00:00
|
|
|
self.addParameter(ParameterBoolean(self.MULTI,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Output as singlepart geometries (only used when dissolving by attribute)'), False))
|
2015-02-24 12:35:00 +02:00
|
|
|
self.addParameter(ParameterString(self.OPTIONS,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Additional creation options (see ogr2ogr manual)'),
|
|
|
|
'', optional=True))
|
2015-02-05 22:55:44 +00:00
|
|
|
|
2015-05-18 09:29:41 +02:00
|
|
|
self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Single sided buffer')))
|
2015-02-05 22:55:44 +00:00
|
|
|
|
2015-05-07 13:14:01 +02:00
|
|
|
def getConsoleCommands(self):
|
2015-02-05 22:55:44 +00:00
|
|
|
inLayer = self.getParameterValue(self.INPUT_LAYER)
|
|
|
|
ogrLayer = self.ogrConnectionString(inLayer)[1:-1]
|
|
|
|
layername = "'" + self.ogrLayerName(inLayer) + "'"
|
|
|
|
operation = self.OPERATIONLIST[self.getParameterValue(self.OPERATION)]
|
|
|
|
geometry = unicode(self.getParameterValue(self.GEOMETRY))
|
|
|
|
distance = unicode(self.getParameterValue(self.RADIUS))
|
|
|
|
leftright = self.LEFTRIGHTLIST[self.getParameterValue(self.LEFTRIGHT)]
|
|
|
|
dissolveall = self.getParameterValue(self.DISSOLVEALL)
|
2015-02-24 12:35:00 +02:00
|
|
|
field = unicode(self.getParameterValue(self.FIELD))
|
2015-02-05 22:55:44 +00:00
|
|
|
multi = self.getParameterValue(self.MULTI)
|
2015-02-24 12:35:00 +02:00
|
|
|
|
2015-02-05 22:55:44 +00:00
|
|
|
output = self.getOutputFromName(self.OUTPUT_LAYER)
|
|
|
|
outFile = output.value
|
|
|
|
|
|
|
|
output = self.ogrConnectionString(outFile)
|
|
|
|
options = unicode(self.getParameterValue(self.OPTIONS))
|
|
|
|
|
|
|
|
arguments = []
|
|
|
|
arguments.append(output)
|
|
|
|
arguments.append(ogrLayer)
|
|
|
|
arguments.append(self.ogrLayerName(inLayer))
|
|
|
|
if dissolveall or field != 'None':
|
2015-08-22 14:29:41 +02:00
|
|
|
if operation == 'Single Side Buffer':
|
|
|
|
arguments.append('-dialect sqlite -sql "SELECT ST_Union(ST_SingleSidedBuffer(')
|
|
|
|
else:
|
|
|
|
arguments.append('-dialect sqlite -sql "SELECT ST_Union(ST_OffsetCurve(')
|
2015-02-05 22:55:44 +00:00
|
|
|
else:
|
2015-08-22 14:29:41 +02:00
|
|
|
if operation == 'Single Side Buffer':
|
|
|
|
arguments.append('-dialect sqlite -sql "SELECT ST_SingleSidedBuffer(')
|
|
|
|
else:
|
|
|
|
arguments.append('-dialect sqlite -sql "SELECT ST_OffsetCurve(')
|
2015-02-05 22:55:44 +00:00
|
|
|
arguments.append(geometry)
|
|
|
|
arguments.append(',')
|
|
|
|
arguments.append(distance)
|
|
|
|
if dissolveall or field != 'None':
|
2015-08-22 14:29:41 +02:00
|
|
|
if leftright == 'Left':
|
|
|
|
arguments.append(',0)),*')
|
|
|
|
else:
|
|
|
|
arguments.append(',1)),*')
|
2015-02-05 22:55:44 +00:00
|
|
|
else:
|
2015-08-22 14:29:41 +02:00
|
|
|
if leftright == 'Left':
|
|
|
|
arguments.append(',0),*')
|
|
|
|
else:
|
|
|
|
arguments.append(',1),*')
|
2015-02-05 22:55:44 +00:00
|
|
|
arguments.append('FROM')
|
|
|
|
arguments.append(layername)
|
|
|
|
if field != 'None':
|
2015-08-22 14:29:41 +02:00
|
|
|
arguments.append('GROUP')
|
|
|
|
arguments.append('BY')
|
|
|
|
arguments.append(field)
|
2015-02-24 12:35:00 +02:00
|
|
|
arguments.append('"')
|
2015-02-05 22:55:44 +00:00
|
|
|
if field != 'None' and multi:
|
2015-08-22 14:29:41 +02:00
|
|
|
arguments.append('-explodecollections')
|
2015-02-05 22:55:44 +00:00
|
|
|
|
|
|
|
if len(options) > 0:
|
|
|
|
arguments.append(options)
|
2015-02-24 12:35:00 +02:00
|
|
|
|
2015-02-05 22:55:44 +00:00
|
|
|
commands = []
|
|
|
|
if isWindows():
|
|
|
|
commands = ['cmd.exe', '/C ', 'ogr2ogr.exe',
|
|
|
|
GdalUtils.escapeAndJoin(arguments)]
|
|
|
|
else:
|
|
|
|
commands = ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]
|
|
|
|
|
2015-05-07 13:14:01 +02:00
|
|
|
return commands
|
2015-05-21 15:43:47 +02:00
|
|
|
|
|
|
|
def commandName(self):
|
2015-08-22 14:29:41 +02:00
|
|
|
return "ogr2ogr"
|