Skip to content

Commit 66e4895

Browse files
committed
Merge pull request #1503 from alexbruy/processing-ogr
Processing ogr
2 parents 6097e58 + c33007f commit 66e4895

12 files changed

+91
-2425
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
FILE(GLOB PY_FILES *.py)
22

3-
ADD_SUBDIRECTORY(pyogr)
4-
5-
PLUGIN_INSTALL(processing ./algs/gdal ${PY_FILES})
3+
PLUGIN_INSTALL(processing ./algs/gdal ${PY_FILES})

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

-15
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

@@ -68,16 +66,3 @@ def ogrConnectionString(self, uri):
6866
else:
6967
ogrstr = str(layer.source())
7068
return ogrstr
71-
72-
def drivers(self):
73-
list = []
74-
if ogrAvailable:
75-
for iDriver in range(ogr.GetDriverCount()):
76-
list.append('%s' % ogr.GetDriver(iDriver).GetName())
77-
return list
78-
79-
def failure(self, pszDataSource):
80-
out = 'FAILURE: Unable to open datasource %s with the following \
81-
drivers.' % pszDataSource
82-
out = out + string.join(map(lambda d: '->' + d, self.drivers()), '\n')
83-
return out

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

+42-137
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,57 @@ 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

121-
def commandLineName(self):
122-
return "gdalogr:ogr2ogr"
115+
def processAlgorithm(self, progress):
116+
inLayer = self.getParameterValue(self.INPUT_LAYER)
117+
ogrLayer = self.ogrConnectionString(inLayer)
123118

119+
output = self.getOutputFromName(self.OUTPUT_LAYER)
120+
outFile = output.value
124121

125-
def processAlgorithm(self, progress):
126-
if not gdalAvailable:
127-
raise GeoAlgorithmExecutionException(
128-
'GDAL bindings not installed.')
122+
formatIdx = self.getParameterValue(self.FORMAT)
123+
outFormat = FORMATS[formatIdx]
124+
ext = EXTS[formatIdx]
125+
if not outFile.endswith(ext):
126+
outFile += ext
127+
output.value = outFile
129128

130-
input = self.getParameterValue(self.INPUT_LAYER)
131-
ogrLayer = self.ogrConnectionString(input)
129+
output = self.ogrConnectionString(outFile)
130+
options = unicode(self.getParameterValue(self.OPTIONS))
132131

133-
output = self.getOutputFromName(self.OUTPUT_LAYER)
134-
outfile = output.value
132+
if outFormat == 'SQLite' and os.path.isfile(output):
133+
os.remove(output)
135134

136-
formatIdx = self.getParameterValue(self.DEST_FORMAT)
135+
arguments = []
136+
arguments.append('-f')
137+
arguments.append(outFormat)
138+
if len(options) > 0:
139+
arguments.append(options)
137140

138-
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
141+
arguments.append(output)
142+
arguments.append(ogrLayer)
143+
144+
commands = []
145+
if isWindows():
146+
commands = ['cmd.exe', '/C ', 'ogr2ogr.exe',
147+
GdalUtils.escapeAndJoin(arguments)]
204148
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
149+
commands = ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]
150+
151+
GdalUtils.runGdal(commands, progress)
152+

0 commit comments

Comments
 (0)