|
25 | 25 |
|
26 | 26 | __revision__ = '$Format:%H$'
|
27 | 27 |
|
28 |
| - |
29 |
| -try: |
30 |
| - from osgeo import gdal, ogr, osr |
31 |
| - gdalAvailable = True |
32 |
| -except: |
33 |
| - gdalAvailable = False |
| 28 | +import os |
34 | 29 |
|
35 | 30 | from PyQt4.QtCore import *
|
36 | 31 | from PyQt4.QtGui import *
|
| 32 | + |
37 | 33 | from qgis.core import *
|
38 |
| -from processing.core.GeoAlgorithmExecutionException import \ |
39 |
| - GeoAlgorithmExecutionException |
| 34 | + |
40 | 35 | from processing.parameters.ParameterVector import ParameterVector
|
41 | 36 | from processing.parameters.ParameterString import ParameterString
|
42 | 37 | from processing.parameters.ParameterSelection import ParameterSelection
|
43 | 38 | from processing.outputs.OutputVector import OutputVector
|
44 | 39 |
|
45 |
| -from OgrAlgorithm import OgrAlgorithm |
46 |
| -from pyogr.ogr2ogr import * |
| 40 | +from processing.tools.system import * |
47 | 41 |
|
48 |
| -GeomOperation = Enum(['NONE', 'SEGMENTIZE', 'SIMPLIFY_PRESERVE_TOPOLOGY']) |
| 42 | +from processing.algs.gdal.OgrAlgorithm import OgrAlgorithm |
| 43 | +from processing.algs.gdal.GdalUtils import GdalUtils |
49 | 44 |
|
50 | 45 | FORMATS = [
|
51 | 46 | 'ESRI Shapefile',
|
@@ -101,147 +96,57 @@ class Ogr2Ogr(OgrAlgorithm):
|
101 | 96 |
|
102 | 97 | OUTPUT_LAYER = 'OUTPUT_LAYER'
|
103 | 98 | 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' |
107 | 101 |
|
108 | 102 | def defineCharacteristics(self):
|
109 | 103 | self.name = 'Convert format'
|
110 | 104 | self.group = '[OGR] Conversion'
|
111 | 105 |
|
112 | 106 | self.addParameter(ParameterVector(self.INPUT_LAYER, 'Input layer',
|
113 | 107 | [ParameterVector.VECTOR_TYPE_ANY], False))
|
114 |
| - self.addParameter(ParameterSelection(self.DEST_FORMAT, |
| 108 | + self.addParameter(ParameterSelection(self.FORMAT, |
115 | 109 | 'Destination Format', FORMATS))
|
116 |
| - self.addParameter(ParameterString(self.DEST_DSCO, 'Creation Options', |
117 |
| - '')) |
| 110 | + self.addParameter(ParameterString(self.OPTIONS, 'Creation Options', |
| 111 | + '', optional=True)) |
118 | 112 |
|
119 | 113 | self.addOutput(OutputVector(self.OUTPUT_LAYER, 'Output layer'))
|
120 | 114 |
|
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) |
123 | 118 |
|
| 119 | + output = self.getOutputFromName(self.OUTPUT_LAYER) |
| 120 | + outFile = output.value |
124 | 121 |
|
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 |
129 | 128 |
|
130 |
| - input = self.getParameterValue(self.INPUT_LAYER) |
131 |
| - ogrLayer = self.ogrConnectionString(input) |
| 129 | + output = self.ogrConnectionString(outFile) |
| 130 | + options = unicode(self.getParameterValue(self.OPTIONS)) |
132 | 131 |
|
133 |
| - output = self.getOutputFromName(self.OUTPUT_LAYER) |
134 |
| - outfile = output.value |
| 132 | + if outFormat == 'SQLite' and os.path.isfile(output): |
| 133 | + os.remove(output) |
135 | 134 |
|
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) |
137 | 140 |
|
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)] |
204 | 148 | 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