Skip to content

Commit a9216c7

Browse files
committed
[processing] refactor OGR algorithms to use commandline tools, not
bindings
1 parent 0df2b01 commit a9216c7

File tree

6 files changed

+91
-350
lines changed

6 files changed

+91
-350
lines changed

python/plugins/processing/algs/gdal/GdalUtils.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,10 @@ def getFormatShortNameFromFilename(filename):
138138
def escapeAndJoin(strList):
139139
joined = ''
140140
for s in strList:
141-
if s[0]!='-' and ' ' in s:
141+
if s[0] != '-' and ' ' in s:
142142
escaped = '"' + s.replace('\\', '\\\\').replace('"', '\\"') \
143143
+ '"'
144144
else:
145145
escaped = s
146146
joined += escaped + ' '
147-
return joined.strip()
147+
return joined.strip()

python/plugins/processing/algs/gdal/OgrAlgorithm.py

-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@
4444

4545
class OgrAlgorithm(GdalAlgorithm):
4646

47-
DB = 'DB'
48-
4947
def ogrConnectionString(self, uri):
5048
ogrstr = None
5149

python/plugins/processing/algs/gdal/information.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ def processAlgorithm(self, progress):
6464
progress)
6565
output = self.getOutputValue(information.OUTPUT)
6666
f = open(output, 'w')
67+
f.write('<pre>')
6768
for s in GdalUtils.getConsoleOutput()[1:]:
68-
f.write('<p>' + str(s) + '</p>')
69+
f.write(unicode(s))
70+
f.write('</pre>')
6971
f.close()

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

+43-135
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,22 @@
2525

2626
__revision__ = '$Format:%H$'
2727

28-
29-
try:
30-
from osgeo import gdal, ogr, osr
31-
gdalAvailable = True
32-
except:
33-
gdalAvailable = False
28+
import os
3429

3530
from PyQt4.QtCore import *
3631
from PyQt4.QtGui import *
32+
3733
from qgis.core import *
38-
from processing.core.GeoAlgorithmExecutionException import \
39-
GeoAlgorithmExecutionException
34+
4035
from processing.parameters.ParameterVector import ParameterVector
4136
from processing.parameters.ParameterString import ParameterString
4237
from processing.parameters.ParameterSelection import ParameterSelection
4338
from processing.outputs.OutputVector import OutputVector
4439

45-
from OgrAlgorithm import OgrAlgorithm
46-
from pyogr.ogr2ogr import *
40+
from processing.tools.system import *
4741

48-
GeomOperation = Enum(['NONE', 'SEGMENTIZE', 'SIMPLIFY_PRESERVE_TOPOLOGY'])
42+
from processing.algs.gdal.OgrAlgorithm import OgrAlgorithm
43+
from processing.algs.gdal.GdalUtils import GdalUtils
4944

5045
FORMATS = [
5146
'ESRI Shapefile',
@@ -101,147 +96,60 @@ class Ogr2Ogr(OgrAlgorithm):
10196

10297
OUTPUT_LAYER = 'OUTPUT_LAYER'
10398
INPUT_LAYER = 'INPUT_LAYER'
104-
DEST_DS = 'DEST_DS'
105-
DEST_FORMAT = 'DEST_FORMAT'
106-
DEST_DSCO = 'DEST_DSCO'
99+
FORMAT = 'FORMAT'
100+
OPTIONS = 'OPTIONS'
107101

108102
def defineCharacteristics(self):
109103
self.name = 'Convert format'
110104
self.group = '[OGR] Conversion'
111105

112106
self.addParameter(ParameterVector(self.INPUT_LAYER, 'Input layer',
113107
[ParameterVector.VECTOR_TYPE_ANY], False))
114-
self.addParameter(ParameterSelection(self.DEST_FORMAT,
108+
self.addParameter(ParameterSelection(self.FORMAT,
115109
'Destination Format', FORMATS))
116-
self.addParameter(ParameterString(self.DEST_DSCO, 'Creation Options',
117-
''))
110+
self.addParameter(ParameterString(self.OPTIONS, 'Creation Options',
111+
'', optional=True))
118112

119113
self.addOutput(OutputVector(self.OUTPUT_LAYER, 'Output layer'))
120114

121115
def commandLineName(self):
122116
return "gdalogr:ogr2ogr"
123117

124-
125118
def processAlgorithm(self, progress):
126-
if not gdalAvailable:
127-
raise GeoAlgorithmExecutionException(
128-
'GDAL bindings not installed.')
129-
130-
input = self.getParameterValue(self.INPUT_LAYER)
131-
ogrLayer = self.ogrConnectionString(input)
119+
inLayer = self.getParameterValue(self.INPUT_LAYER)
120+
ogrLayer = self.ogrConnectionString(inLayer)
132121

133122
output = self.getOutputFromName(self.OUTPUT_LAYER)
134-
outfile = output.value
135-
136-
formatIdx = self.getParameterValue(self.DEST_FORMAT)
123+
outFile = output.value
137124

125+
formatIdx = self.getParameterValue(self.FORMAT)
126+
outFormat = FORMATS[formatIdx]
138127
ext = EXTS[formatIdx]
139-
if not outfile.endswith(ext):
140-
outfile = outfile + ext
141-
output.value = outfile
142-
143-
dst_ds = self.ogrConnectionString(outfile)
144-
dst_format = FORMATS[formatIdx]
145-
ogr_dsco = [self.getParameterValue(self.DEST_DSCO)]
146-
147-
poDS = ogr.Open(ogrLayer, False)
148-
if poDS is None:
149-
raise GeoAlgorithmExecutionException(self.failure(ogrLayer))
150-
151-
if dst_format == 'SQLite' and os.path.isfile(dst_ds):
152-
os.remove(dst_ds)
153-
driver = ogr.GetDriverByName(str(dst_format))
154-
poDstDS = driver.CreateDataSource(dst_ds, options=ogr_dsco)
155-
if poDstDS is None:
156-
raise GeoAlgorithmExecutionException('Error creating %s' % dst_ds)
157-
return
158-
self.ogrtransform(poDS, poDstDS, bOverwrite=True)
159-
160-
def ogrtransform(
161-
self,
162-
poSrcDS,
163-
poDstDS,
164-
papszLayers=[],
165-
papszLCO=[],
166-
bTransform=False,
167-
bAppend=False,
168-
bUpdate=False,
169-
bOverwrite=False,
170-
poOutputSRS=None,
171-
poSourceSRS=None,
172-
pszNewLayerName=None,
173-
pszWHERE=None,
174-
papszSelFields=None,
175-
eGType=-2,
176-
eGeomOp=GeomOperation.NONE,
177-
dfGeomOpParam=0,
178-
papszFieldTypesToString=[],
179-
pfnProgress=None,
180-
pProgressData=None,
181-
nCountLayerFeatures=0,
182-
poClipSrc=None,
183-
poClipDst=None,
184-
bExplodeCollections=False,
185-
pszZField=None,
186-
):
187-
188-
# Process each data source layer
189-
if len(papszLayers) == 0:
190-
nLayerCount = poSrcDS.GetLayerCount()
191-
papoLayers = [None for i in range(nLayerCount)]
192-
iLayer = 0
193-
194-
for iLayer in range(nLayerCount):
195-
poLayer = poSrcDS.GetLayer(iLayer)
196-
197-
if poLayer is None:
198-
raise GeoAlgorithmExecutionException(
199-
"FAILURE: Couldn't fetch advertised layer %d!"
200-
% iLayer)
201-
202-
papoLayers[iLayer] = poLayer
203-
iLayer = iLayer + 1
128+
if not outFile.endswith(ext):
129+
outFile += ext
130+
output.value = outFile
131+
132+
output = self.ogrConnectionString(outFile)
133+
options = unicode(self.getParameterValue(self.OPTIONS))
134+
135+
if outFormat == 'SQLite' and os.path.isfile(output):
136+
os.remove(output)
137+
138+
arguments = []
139+
arguments.append('-f')
140+
arguments.append(outFormat)
141+
if len(options) > 0:
142+
arguments.append(options)
143+
144+
arguments.append(output)
145+
arguments.append(ogrLayer)
146+
147+
commands = []
148+
if isWindows():
149+
commands = ['cmd.exe', '/C ', 'ogr2ogr.exe',
150+
GdalUtils.escapeAndJoin(arguments)]
204151
else:
205-
# Process specified data source layers
206-
nLayerCount = len(papszLayers)
207-
papoLayers = [None for i in range(nLayerCount)]
208-
iLayer = 0
209-
210-
for layername in papszLayers:
211-
poLayer = poSrcDS.GetLayerByName(layername)
212-
213-
if poLayer is None:
214-
raise GeoAlgorithmExecutionException(
215-
"FAILURE: Couldn't fetch advertised layer %s!"
216-
% layername)
217-
218-
papoLayers[iLayer] = poLayer
219-
iLayer = iLayer + 1
220-
221-
for poSrcLayer in papoLayers:
222-
ok = TranslateLayer(
223-
poSrcDS,
224-
poSrcLayer,
225-
poDstDS,
226-
papszLCO,
227-
pszNewLayerName,
228-
bTransform,
229-
poOutputSRS,
230-
poSourceSRS,
231-
papszSelFields,
232-
bAppend,
233-
eGType,
234-
bOverwrite,
235-
eGeomOp,
236-
dfGeomOpParam,
237-
papszFieldTypesToString,
238-
nCountLayerFeatures,
239-
poClipSrc,
240-
poClipDst,
241-
bExplodeCollections,
242-
pszZField,
243-
pszWHERE,
244-
pfnProgress,
245-
pProgressData,
246-
)
247-
return True
152+
commands = ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]
153+
154+
GdalUtils.runGdal(commands, progress)
155+

0 commit comments

Comments
 (0)