28
28
import AlgorithmsTestBase
29
29
from processing .algs .gdal .OgrToPostGis import OgrToPostGis
30
30
from processing .algs .gdal .GdalUtils import GdalUtils
31
+ from processing .algs .gdal .translate import translate
31
32
from qgis .core import (QgsProcessingContext ,
32
33
QgsProcessingFeedback ,
33
34
QgsApplication ,
@@ -91,7 +92,8 @@ def testGetOgrCompatibleSourceFromMemoryLayer(self):
91
92
parameters = {'INPUT' : 'testmem' }
92
93
feedback = QgsProcessingFeedback ()
93
94
# check that memory layer is automatically saved out to shape when required by GDAL algorithms
94
- ogr_data_path , ogr_layer_name = alg .getOgrCompatibleSource ('INPUT' , parameters , context , feedback , executing = True )
95
+ ogr_data_path , ogr_layer_name = alg .getOgrCompatibleSource ('INPUT' , parameters , context , feedback ,
96
+ executing = True )
95
97
self .assertTrue (ogr_data_path )
96
98
self .assertTrue (ogr_data_path .endswith ('.shp' ))
97
99
self .assertTrue (os .path .exists (ogr_data_path ))
@@ -180,9 +182,76 @@ def _copyFile(dst):
180
182
self .assertEqual (name , 't' )
181
183
182
184
# PostgreSQL provider
183
- name = GdalUtils .ogrLayerName ('port=5493 sslmode=disable key=\' edge_id\' srid=0 type=LineString table="city_data"."edge" (geom) sql=' )
185
+ name = GdalUtils .ogrLayerName (
186
+ 'port=5493 sslmode=disable key=\' edge_id\' srid=0 type=LineString table="city_data"."edge" (geom) sql=' )
184
187
self .assertEqual (name , 'city_data.edge' )
185
188
189
+ def testGdalTranslate (self ):
190
+ context = QgsProcessingContext ()
191
+ feedback = QgsProcessingFeedback ()
192
+ source = os .path .join (testDataPath , 'dem.tif' )
193
+ translate_alg = translate ()
194
+ translate_alg .initAlgorithm ()
195
+
196
+ # with no NODATA value
197
+ self .assertEqual (
198
+ translate_alg .getConsoleCommands ({'INPUT' : source ,
199
+ 'OUTPUT' : 'd:/temp/check.jpg' }, context , feedback ),
200
+ ['gdal_translate' ,
201
+ '-ot Float32 -of JPEG ' +
202
+ source + ' ' +
203
+ 'd:/temp/check.jpg' ])
204
+ # with NODATA value
205
+ self .assertEqual (
206
+ translate_alg .getConsoleCommands ({'INPUT' : source ,
207
+ 'NODATA' : 9999 ,
208
+ 'OUTPUT' : 'd:/temp/check.jpg' }, context , feedback ),
209
+ ['gdal_translate' ,
210
+ '-a_nodata 9999.0 ' +
211
+ '-ot Float32 -of JPEG ' +
212
+ source + ' ' +
213
+ 'd:/temp/check.jpg' ])
214
+ # with "0" NODATA value
215
+ self .assertEqual (
216
+ translate_alg .getConsoleCommands ({'INPUT' : source ,
217
+ 'NODATA' : 0 ,
218
+ 'OUTPUT' : 'd:/temp/check.jpg' }, context , feedback ),
219
+ ['gdal_translate' ,
220
+ '-a_nodata 0.0 ' +
221
+ '-ot Float32 -of JPEG ' +
222
+ source + ' ' +
223
+ 'd:/temp/check.jpg' ])
224
+ # with target srs
225
+ self .assertEqual (
226
+ translate_alg .getConsoleCommands ({'INPUT' : source ,
227
+ 'TARGET_CRS' : 'EPSG:3111' ,
228
+ 'OUTPUT' : 'd:/temp/check.jpg' }, context , feedback ),
229
+ ['gdal_translate' ,
230
+ '-a_srs EPSG:3111 ' +
231
+ '-ot Float32 -of JPEG ' +
232
+ source + ' ' +
233
+ 'd:/temp/check.jpg' ])
234
+ # with target srs
235
+ self .assertEqual (
236
+ translate_alg .getConsoleCommands ({'INPUT' : source ,
237
+ 'TARGET_CRS' : 'EPSG:3111' ,
238
+ 'OUTPUT' : 'd:/temp/check.jpg' }, context , feedback ),
239
+ ['gdal_translate' ,
240
+ '-a_srs EPSG:3111 ' +
241
+ '-ot Float32 -of JPEG ' +
242
+ source + ' ' +
243
+ 'd:/temp/check.jpg' ])
244
+ # with copy subdatasets
245
+ self .assertEqual (
246
+ translate_alg .getConsoleCommands ({'INPUT' : source ,
247
+ 'COPY_SUBDATASETS' : True ,
248
+ 'OUTPUT' : 'd:/temp/check.tif' }, context , feedback ),
249
+ ['gdal_translate' ,
250
+ '-sds ' +
251
+ '-ot Float32 -of GTiff ' +
252
+ source + ' ' +
253
+ 'd:/temp/check.tif' ])
254
+
186
255
187
256
class TestGdalOgrToPostGis (unittest .TestCase ):
188
257
@@ -198,7 +267,6 @@ def tearDownClass(cls):
198
267
199
268
# See https://issues.qgis.org/issues/15706
200
269
def test_getConnectionString (self ):
201
-
202
270
obj = OgrToPostGis ()
203
271
obj .initAlgorithm ({})
204
272
0 commit comments