Skip to content

Commit 750e80f

Browse files
committedOct 11, 2017
[processing] restore ogr2ogr algorithm
1 parent efa97b2 commit 750e80f

File tree

2 files changed

+37
-107
lines changed

2 files changed

+37
-107
lines changed
 

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@
7777
from .ClipVectorByExtent import ClipVectorByExtent
7878
from .ClipVectorByMask import ClipVectorByMask
7979
from .OffsetCurve import OffsetCurve
80+
from .ogr2ogr import ogr2ogr
8081
from .ogrinfo import ogrinfo
8182
from .OgrToPostGis import OgrToPostGis
8283
from .OneSideBuffer import OneSideBuffer
8384
from .PointsAlongLines import PointsAlongLines
8485

85-
# from .ogr2ogr import Ogr2Ogr
8686
# from .ogr2ogrtopostgislist import Ogr2OgrToPostGisList
8787
# from .ogr2ogrdissolve import Ogr2OgrDissolve
8888
# from .ogr2ogrtabletopostgislist import Ogr2OgrTableToPostGisList
@@ -179,11 +179,11 @@ def loadAlgorithms(self):
179179
ClipVectorByExtent(),
180180
ClipVectorByMask(),
181181
OffsetCurve(),
182+
ogr2ogr(),
182183
ogrinfo(),
183184
OgrToPostGis(),
184185
OneSideBuffer(),
185186
PointsAlongLines(),
186-
# Ogr2Ogr(),
187187
# Ogr2OgrToPostGisList(),
188188
# Ogr2OgrDissolve(),
189189
# Ogr2OgrTableToPostGisList(),

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

Lines changed: 35 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
* *
1717
***************************************************************************
1818
"""
19-
from builtins import str
2019

2120
__author__ = 'Victor Olaya'
2221
__date__ = 'November 2012'
@@ -28,89 +27,37 @@
2827

2928
import os
3029

31-
from processing.core.parameters import ParameterVector
32-
from processing.core.parameters import ParameterString
33-
from processing.core.parameters import ParameterSelection
34-
from processing.core.outputs import OutputVector
35-
30+
from qgis.core import (QgsProcessingException,
31+
QgsProcessingParameterDefinition,
32+
QgsProcessingParameterFeatureSource,
33+
QgsProcessingParameterString,
34+
QgsProcessingParameterVectorDestination)
3635
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
3736
from processing.algs.gdal.GdalUtils import GdalUtils
3837

39-
from processing.tools.system import isWindows
40-
41-
42-
FORMATS = [
43-
'ESRI Shapefile',
44-
'GeoJSON',
45-
'GeoRSS',
46-
'SQLite',
47-
'GMT',
48-
'MapInfo File',
49-
'INTERLIS 1',
50-
'INTERLIS 2',
51-
'GML',
52-
'Geoconcept',
53-
'DXF',
54-
'DGN',
55-
'CSV',
56-
'BNA',
57-
'S57',
58-
'KML',
59-
'GPX',
60-
'PGDump',
61-
'GPSTrackMaker',
62-
'ODS',
63-
'XLSX',
64-
'PDF',
65-
'GPKG',
66-
]
67-
68-
EXTS = [
69-
'.shp',
70-
'.geojson',
71-
'.xml',
72-
'.sqlite',
73-
'.gmt',
74-
'.tab',
75-
'.ili',
76-
'.ili',
77-
'.gml',
78-
'.txt',
79-
'.dxf',
80-
'.dgn',
81-
'.csv',
82-
'.bna',
83-
'.000',
84-
'.kml',
85-
'.gpx',
86-
'.pgdump',
87-
'.gtm',
88-
'.ods',
89-
'.xlsx',
90-
'.pdf',
91-
'.gpkg',
92-
]
93-
94-
95-
class Ogr2Ogr(GdalAlgorithm):
96-
97-
OUTPUT_LAYER = 'OUTPUT_LAYER'
98-
INPUT_LAYER = 'INPUT_LAYER'
99-
FORMAT = 'FORMAT'
38+
39+
class ogr2ogr(GdalAlgorithm):
40+
41+
INPUT = 'INPUT'
10042
OPTIONS = 'OPTIONS'
43+
OUTPUT = 'OUTPUT'
10144

10245
def __init__(self):
10346
super().__init__()
10447

10548
def initAlgorithm(self, config=None):
106-
self.addParameter(ParameterVector(self.INPUT_LAYER,
107-
self.tr('Input layer')))
108-
self.addParameter(ParameterSelection(self.FORMAT,
109-
self.tr('Destination Format'), FORMATS))
110-
self.addParameter(ParameterString(self.OPTIONS,
111-
self.tr('Creation options'), '', optional=True))
49+
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
50+
self.tr('Input layer')))
11251

113-
self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Converted')))
52+
options_param = QgsProcessingParameterString(self.OPTIONS,
53+
self.tr('Additional creation options'),
54+
defaultValue='',
55+
optional=True)
56+
options_param.setFlags(options_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
57+
self.addParameter(options_param)
58+
59+
self.addParameter(QgsProcessingParameterVectorDestination(self.OUTPUT,
60+
self.tr('Converted')))
11461

11562
def name(self):
11663
return 'convertformat'
@@ -121,45 +68,28 @@ def displayName(self):
12168
def group(self):
12269
return self.tr('Vector conversion')
12370

124-
def getConsoleCommands(self, parameters, context, feedback):
125-
inLayer = self.getParameterValue(self.INPUT_LAYER)
126-
ogrLayer = GdalUtils.ogrConnectionString(inLayer, context)[1:-1]
127-
128-
output = self.getOutputFromName(self.OUTPUT_LAYER)
129-
outFile = output.value
71+
def commandName(self):
72+
return 'ogr2ogr'
13073

131-
formatIdx = self.getParameterValue(self.FORMAT)
132-
outFormat = FORMATS[formatIdx]
133-
ext = EXTS[formatIdx]
134-
if not outFile.endswith(ext):
135-
outFile += ext
136-
output.value = outFile
74+
def getConsoleCommands(self, parameters, context, feedback):
75+
ogrLayer, layerName = self.getOgrCompatibleSource(self.INPUT, parameters, context, feedback)
76+
options = self.parameterAsString(parameters, self.OPTIONS, context)
77+
outFile = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
13778

138-
output = GdalUtils.ogrConnectionString(outFile, context)
139-
options = str(self.getParameterValue(self.OPTIONS))
79+
output, outputFormat = GdalUtils.ogrConnectionStringAndFormat(outFile, context)
14080

141-
if outFormat == 'SQLite' and os.path.isfile(output):
142-
os.remove(output)
81+
if outputFormat == 'SQLite' and os.path.isfile(output):
82+
raise QgsProcessinException(self.tr('Output file "{}" already exists.'.format(output)))
14383

14484
arguments = []
145-
arguments.append('-f')
146-
arguments.append(outFormat)
85+
if outputFormat:
86+
arguments.append('-f {}'.format(outputFormat))
14787

148-
if options is not None and len(options.strip()) > 0:
88+
if options:
14989
arguments.append(options)
15090

15191
arguments.append(output)
15292
arguments.append(ogrLayer)
153-
arguments.append(GdalUtils.ogrLayerName(inLayer))
154-
155-
commands = []
156-
if isWindows():
157-
commands = ['cmd.exe', '/C ', 'ogr2ogr.exe',
158-
GdalUtils.escapeAndJoin(arguments)]
159-
else:
160-
commands = ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]
93+
arguments.append(layerName)
16194

162-
return commands
163-
164-
def commandName(self):
165-
return "ogr2ogr"
95+
return ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]

0 commit comments

Comments
 (0)
Please sign in to comment.