Skip to content

Commit f48aaea

Browse files
committedMay 5, 2018
[processing][gdal] Fix some more nodata=0 handling
1 parent b328819 commit f48aaea

File tree

5 files changed

+147
-7
lines changed

5 files changed

+147
-7
lines changed
 

‎python/plugins/processing/algs/gdal/ClipRasterByExtent.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,10 @@ def commandName(self):
111111
def getConsoleCommands(self, parameters, context, feedback, executing=True):
112112
inLayer = self.parameterAsRasterLayer(parameters, self.INPUT, context)
113113
bbox = self.parameterAsExtent(parameters, self.EXTENT, context, inLayer.crs())
114-
nodata = self.parameterAsDouble(parameters, self.NODATA, context)
114+
if self.NODATA in parameters and parameters[self.NODATA] is not None:
115+
nodata = self.parameterAsDouble(parameters, self.NODATA, context)
116+
else:
117+
nodata = None
115118
options = self.parameterAsString(parameters, self.OPTIONS, context)
116119
out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
117120

@@ -122,7 +125,7 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
122125
arguments.append(str(bbox.xMaximum()))
123126
arguments.append(str(bbox.yMinimum()))
124127

125-
if nodata:
128+
if nodata is not None:
126129
arguments.append('-a_nodata {}'.format(nodata))
127130

128131
arguments.append('-ot')

‎python/plugins/processing/algs/gdal/ClipRasterByMask.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,10 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
127127

128128
maskLayer, maskLayerName = self.getOgrCompatibleSource(self.MASK, parameters, context, feedback, executing)
129129

130-
nodata = self.parameterAsDouble(parameters, self.NODATA, context)
130+
if self.NODATA in parameters and parameters[self.NODATA] is not None:
131+
nodata = self.parameterAsDouble(parameters, self.NODATA, context)
132+
else:
133+
nodata = None
131134
options = self.parameterAsString(parameters, self.OPTIONS, context)
132135
out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
133136

@@ -153,7 +156,7 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
153156
if self.parameterAsBool(parameters, self.ALPHA_BAND, context):
154157
arguments.append('-dstalpha')
155158

156-
if nodata:
159+
if nodata is not None:
157160
arguments.append('-dstnodata {}'.format(nodata))
158161

159162
if options:

‎python/plugins/processing/algs/gdal/contour.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,10 @@ def commandName(self):
127127
def getConsoleCommands(self, parameters, context, feedback, executing=True):
128128
inLayer = self.parameterAsRasterLayer(parameters, self.INPUT, context)
129129
fieldName = self.parameterAsString(parameters, self.FIELD_NAME, context)
130-
nodata = self.parameterAsDouble(parameters, self.NODATA, context)
130+
if self.NODATA in parameters and parameters[self.NODATA] is not None:
131+
nodata = self.parameterAsDouble(parameters, self.NODATA, context)
132+
else:
133+
nodata = None
131134
offset = self.parameterAsDouble(parameters, self.OFFSET, context)
132135

133136
outFile = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
@@ -150,7 +153,7 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
150153
if self.parameterAsBool(parameters, self.IGNORE_NODATA, context):
151154
arguments.append('-inodata')
152155

153-
if nodata:
156+
if nodata is not None:
154157
arguments.append('-snodata {}'.format(nodata))
155158

156159
if offset:

‎python/plugins/processing/tests/GdalAlgorithmsTest.py

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
import AlgorithmsTestBase
2929
from processing.algs.gdal.OgrToPostGis import OgrToPostGis
3030
from processing.algs.gdal.GdalUtils import GdalUtils
31+
from processing.algs.gdal.ClipRasterByExtent import ClipRasterByExtent
32+
from processing.algs.gdal.ClipRasterByMask import ClipRasterByMask
33+
from processing.algs.gdal.contour import contour
3134
from processing.algs.gdal.translate import translate
3235
from qgis.core import (QgsProcessingContext,
3336
QgsProcessingFeedback,
@@ -37,6 +40,7 @@
3740
QgsGeometry,
3841
QgsPointXY,
3942
QgsProject,
43+
QgsRectangle,
4044
QgsProcessingUtils,
4145
QgsProcessingFeatureSourceDefinition)
4246
import nose2
@@ -192,6 +196,15 @@ def _copyFile(dst):
192196
'port=5493 sslmode=disable key=\'edge_id\' srid=0 type=LineString table="city_data"."edge" (geom) sql=')
193197
self.assertEqual(name, 'city_data.edge')
194198

199+
def testOgrConnectionStringAndFormat(self):
200+
context = QgsProcessingContext()
201+
output, outputFormat = GdalUtils.ogrConnectionStringAndFormat('d:/test/test.shp', context)
202+
self.assertEqual(output, '"d:/test/test.shp"')
203+
self.assertEqual(outputFormat, '"ESRI Shapefile"')
204+
output, outputFormat = GdalUtils.ogrConnectionStringAndFormat('d:/test/test.mif', context)
205+
self.assertEqual(output, '"d:/test/test.mif"')
206+
self.assertEqual(outputFormat, '"MapInfo File"')
207+
195208
def testGdalTranslate(self):
196209
context = QgsProcessingContext()
197210
feedback = QgsProcessingFeedback()
@@ -258,6 +271,125 @@ def testGdalTranslate(self):
258271
source + ' ' +
259272
'd:/temp/check.tif'])
260273

274+
def testClipRasterByExtent(self):
275+
context = QgsProcessingContext()
276+
feedback = QgsProcessingFeedback()
277+
source = os.path.join(testDataPath, 'dem.tif')
278+
alg = ClipRasterByExtent()
279+
alg.initAlgorithm()
280+
extent = QgsRectangle(1, 2, 3, 4)
281+
282+
# with no NODATA value
283+
self.assertEqual(
284+
alg.getConsoleCommands({'INPUT': source,
285+
'EXTENT': extent,
286+
'OUTPUT': 'd:/temp/check.jpg'}, context, feedback),
287+
['gdal_translate',
288+
'-projwin 0.0 0.0 0.0 0.0 -ot Float32 -of JPEG ' +
289+
source + ' ' +
290+
'd:/temp/check.jpg'])
291+
# with NODATA value
292+
self.assertEqual(
293+
alg.getConsoleCommands({'INPUT': source,
294+
'EXTENT': extent,
295+
'NODATA': 9999,
296+
'OUTPUT': 'd:/temp/check.jpg'}, context, feedback),
297+
['gdal_translate',
298+
'-projwin 0.0 0.0 0.0 0.0 -a_nodata 9999.0 -ot Float32 -of JPEG ' +
299+
source + ' ' +
300+
'd:/temp/check.jpg'])
301+
# with "0" NODATA value
302+
self.assertEqual(
303+
alg.getConsoleCommands({'INPUT': source,
304+
'EXTENT': extent,
305+
'NODATA': 0,
306+
'OUTPUT': 'd:/temp/check.jpg'}, context, feedback),
307+
['gdal_translate',
308+
'-projwin 0.0 0.0 0.0 0.0 -a_nodata 0.0 -ot Float32 -of JPEG ' +
309+
source + ' ' +
310+
'd:/temp/check.jpg'])
311+
312+
def testClipRasterByMask(self):
313+
context = QgsProcessingContext()
314+
feedback = QgsProcessingFeedback()
315+
source = os.path.join(testDataPath, 'dem.tif')
316+
mask = os.path.join(testDataPath, 'polys.gml')
317+
alg = ClipRasterByMask()
318+
alg.initAlgorithm()
319+
320+
# with no NODATA value
321+
self.assertEqual(
322+
alg.getConsoleCommands({'INPUT': source,
323+
'MASK': mask,
324+
'OUTPUT': 'd:/temp/check.jpg'}, context, feedback),
325+
['gdalwarp',
326+
'-ot Float32 -of JPEG -cutline ' +
327+
mask + ' -crop_to_cutline ' + source + ' ' +
328+
'd:/temp/check.jpg'])
329+
# with NODATA value
330+
self.assertEqual(
331+
alg.getConsoleCommands({'INPUT': source,
332+
'MASK': mask,
333+
'NODATA': 9999,
334+
'OUTPUT': 'd:/temp/check.jpg'}, context, feedback),
335+
['gdalwarp',
336+
'-ot Float32 -of JPEG -cutline ' +
337+
mask + ' -crop_to_cutline -dstnodata 9999.0 ' + source + ' ' +
338+
'd:/temp/check.jpg'])
339+
# with "0" NODATA value
340+
self.assertEqual(
341+
alg.getConsoleCommands({'INPUT': source,
342+
'MASK': mask,
343+
'NODATA': 0,
344+
'OUTPUT': 'd:/temp/check.jpg'}, context, feedback),
345+
['gdalwarp',
346+
'-ot Float32 -of JPEG -cutline ' +
347+
mask + ' -crop_to_cutline -dstnodata 0.0 ' + source + ' ' +
348+
'd:/temp/check.jpg'])
349+
350+
def testContour(self):
351+
context = QgsProcessingContext()
352+
feedback = QgsProcessingFeedback()
353+
source = os.path.join(testDataPath, 'dem.tif')
354+
alg = contour()
355+
alg.initAlgorithm()
356+
357+
# with no NODATA value
358+
self.assertEqual(
359+
alg.getConsoleCommands({'INPUT': source,
360+
'BAND': 1,
361+
'FIELD_NAME': 'elev',
362+
'INTERVAL': 5,
363+
'OUTPUT': 'd:/temp/check.shp'}, context, feedback),
364+
['gdal_contour',
365+
'-b 1 -a elev -i 5.0 -f "ESRI Shapefile" ' +
366+
source + ' ' +
367+
'"d:/temp/check.shp"'])
368+
# with NODATA value
369+
self.assertEqual(
370+
alg.getConsoleCommands({'INPUT': source,
371+
'BAND': 1,
372+
'FIELD_NAME': 'elev',
373+
'INTERVAL': 5,
374+
'NODATA': 9999,
375+
'OUTPUT': 'd:/temp/check.shp'}, context, feedback),
376+
['gdal_contour',
377+
'-b 1 -a elev -i 5.0 -snodata 9999.0 -f "ESRI Shapefile" ' +
378+
source + ' ' +
379+
'"d:/temp/check.shp"'])
380+
# with "0" NODATA value
381+
self.assertEqual(
382+
alg.getConsoleCommands({'INPUT': source,
383+
'BAND': 1,
384+
'FIELD_NAME': 'elev',
385+
'INTERVAL': 5,
386+
'NODATA': 0,
387+
'OUTPUT': 'd:/temp/check.gpkg'}, context, feedback),
388+
['gdal_contour',
389+
'-b 1 -a elev -i 5.0 -snodata 0.0 -f "GPKG" ' +
390+
source + ' ' +
391+
'"d:/temp/check.gpkg"'])
392+
261393

262394
class TestGdalOgrToPostGis(unittest.TestCase):
263395

‎python/plugins/processing/tests/testdata/gdal_algorithm_tests.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ tests:
6161
MASK:
6262
name: custom/polygon_mask.gml
6363
type: vector
64-
NODATA: 0.0
6564
OPTIONS: ''
6665
results:
6766
OUTPUT:

0 commit comments

Comments
 (0)
Please sign in to comment.