16
16
* *
17
17
***************************************************************************
18
18
"""
19
- from builtins import str
20
19
21
20
__author__ = 'Victor Olaya'
22
21
__date__ = 'November 2012'
28
27
29
28
import os
30
29
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 )
36
35
from processing .algs .gdal .GdalAlgorithm import GdalAlgorithm
37
36
from processing .algs .gdal .GdalUtils import GdalUtils
38
37
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'
100
42
OPTIONS = 'OPTIONS'
43
+ OUTPUT = 'OUTPUT'
101
44
102
45
def __init__ (self ):
103
46
super ().__init__ ()
104
47
105
48
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' )))
112
51
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' )))
114
61
115
62
def name (self ):
116
63
return 'convertformat'
@@ -121,45 +68,28 @@ def displayName(self):
121
68
def group (self ):
122
69
return self .tr ('Vector conversion' )
123
70
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'
130
73
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 )
137
78
138
- output = GdalUtils .ogrConnectionString (outFile , context )
139
- options = str (self .getParameterValue (self .OPTIONS ))
79
+ output , outputFormat = GdalUtils .ogrConnectionStringAndFormat (outFile , context )
140
80
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 )) )
143
83
144
84
arguments = []
145
- arguments . append ( '-f' )
146
- arguments .append (outFormat )
85
+ if outputFormat :
86
+ arguments .append ('-f {}' . format ( outputFormat ) )
147
87
148
- if options is not None and len ( options . strip ()) > 0 :
88
+ if options :
149
89
arguments .append (options )
150
90
151
91
arguments .append (output )
152
92
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 )
161
94
162
- return commands
163
-
164
- def commandName (self ):
165
- return "ogr2ogr"
95
+ return ['ogr2ogr' , GdalUtils .escapeAndJoin (arguments )]
0 commit comments