|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +*************************************************************************** |
| 5 | + offsetcurve.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 processing.core.parameters import ParameterVector |
| 29 | +from processing.core.parameters import ParameterString |
| 30 | +from processing.core.parameters import ParameterBoolean |
| 31 | +from processing.core.parameters import ParameterTableField |
| 32 | +from processing.core.parameters import ParameterSelection |
| 33 | +from processing.core.outputs import OutputVector |
| 34 | + |
| 35 | +from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm |
| 36 | +from processing.algs.gdal.GdalUtils import GdalUtils |
| 37 | + |
| 38 | +from processing.tools import dataobjects |
| 39 | +from processing.tools.system import isWindows |
| 40 | +from processing.tools.vector import ogrConnectionString, ogrLayerName |
| 41 | + |
| 42 | + |
| 43 | +class OffsetCurve(GdalAlgorithm): |
| 44 | + |
| 45 | + OUTPUT_LAYER = 'OUTPUT_LAYER' |
| 46 | + INPUT_LAYER = 'INPUT_LAYER' |
| 47 | + GEOMETRY = 'GEOMETRY' |
| 48 | + RADIUS = 'RADIUS' |
| 49 | + LEFTRIGHT = 'LEFTRIGHT' |
| 50 | + LEFTRIGHTLIST = ['Right', 'Left'] |
| 51 | + DISSOLVEALL = 'DISSOLVEALL' |
| 52 | + FIELD = 'FIELD' |
| 53 | + MULTI = 'MULTI' |
| 54 | + OPTIONS = 'OPTIONS' |
| 55 | + |
| 56 | + def defineCharacteristics(self): |
| 57 | + self.name, self.i18n_name = self.trAlgorithm('Offset lines for lines') |
| 58 | + self.group, self.i18n_group = self.trAlgorithm('[OGR] Geoprocessing') |
| 59 | + |
| 60 | + self.addParameter(ParameterVector(self.INPUT_LAYER, |
| 61 | + self.tr('Input layer'), [dataobjects.TYPE_VECTOR_LINE], False)) |
| 62 | + self.addParameter(ParameterString(self.GEOMETRY, |
| 63 | + self.tr('Geometry column name ("geometry" for Shapefiles, may be different for other formats)'), |
| 64 | + 'geometry', optional=False)) |
| 65 | + self.addParameter(ParameterString(self.RADIUS, |
| 66 | + self.tr('Offset distance'), '1000', optional=False)) |
| 67 | + self.addParameter(ParameterSelection(self.LEFTRIGHT, |
| 68 | + self.tr('Offset side'), self.LEFTRIGHTLIST, 0)) |
| 69 | + self.addParameter(ParameterBoolean(self.DISSOLVEALL, |
| 70 | + self.tr('Dissolve all results'), False)) |
| 71 | + self.addParameter(ParameterTableField(self.FIELD, |
| 72 | + self.tr('Dissolve by attribute'), self.INPUT_LAYER, optional=True)) |
| 73 | + self.addParameter(ParameterBoolean(self.MULTI, |
| 74 | + self.tr('Output as singlepart geometries (only used when dissolving by attribute)'), False)) |
| 75 | + self.addParameter(ParameterString(self.OPTIONS, |
| 76 | + self.tr('Additional creation options (see ogr2ogr manual)'), |
| 77 | + '', optional=True)) |
| 78 | + |
| 79 | + self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Offset line'))) |
| 80 | + |
| 81 | + def getConsoleCommands(self): |
| 82 | + inLayer = self.getParameterValue(self.INPUT_LAYER) |
| 83 | + geometry = self.getParameterValue(self.GEOMETRY) |
| 84 | + distance = self.getParameterValue(self.RADIUS) |
| 85 | + leftright = self.getParameterValue(self.LEFTRIGHT) |
| 86 | + dissolveall = self.getParameterValue(self.DISSOLVEALL) |
| 87 | + field = self.getParameterValue(self.FIELD) |
| 88 | + multi = self.getParameterValue(self.MULTI) |
| 89 | + options = self.getParameterValue(self.OPTIONS) |
| 90 | + |
| 91 | + ogrLayer = ogrConnectionString(inLayer)[1:-1] |
| 92 | + layername = "'" + ogrLayerName(inLayer) + "'" |
| 93 | + |
| 94 | + output = self.getOutputFromName(self.OUTPUT_LAYER) |
| 95 | + outFile = output.value |
| 96 | + output = ogrConnectionString(outFile) |
| 97 | + |
| 98 | + layername = ogrLayerName(inLayer) |
| 99 | + |
| 100 | + arguments = [] |
| 101 | + arguments.append(output) |
| 102 | + arguments.append(ogrLayer) |
| 103 | + arguments.append(layername) |
| 104 | + arguments.append('-dialect') |
| 105 | + arguments.append('sqlite') |
| 106 | + arguments.append('-sql') |
| 107 | + |
| 108 | + if dissolveall or field is not None: |
| 109 | + sql = "SELECT ST_Union(ST_OffsetCurve({}, {}, {})) * FROM '{}'".format(geometry, distance, leftright, layername) |
| 110 | + else: |
| 111 | + sql = "SELECT ST_OffsetCurve({}, {}, {}), * FROM '{}'".format(geometry, distance, leftright, layername) |
| 112 | + |
| 113 | + if field is not None: |
| 114 | + sql = '"{} GROUP BY {}"'.format(sql, field) |
| 115 | + |
| 116 | + arguments.append(sql) |
| 117 | + |
| 118 | + if field is not None and multi: |
| 119 | + arguments.append('-explodecollections') |
| 120 | + |
| 121 | + if len(options) > 0: |
| 122 | + arguments.append(options) |
| 123 | + |
| 124 | + commands = [] |
| 125 | + if isWindows(): |
| 126 | + commands = ['cmd.exe', '/C ', 'ogr2ogr.exe', |
| 127 | + GdalUtils.escapeAndJoin(arguments)] |
| 128 | + else: |
| 129 | + commands = ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)] |
| 130 | + |
| 131 | + return commands |
| 132 | + |
| 133 | + def commandName(self): |
| 134 | + return 'ogr2ogr' |
0 commit comments