Skip to content
Permalink
Browse files

[processing][gdal] Fix some more nodata=0 handling

  • Loading branch information
nyalldawson committed May 5, 2018
1 parent b328819 commit f48aaeaf33a4a1ef6c8015bba1163bc2315e3ae5
@@ -111,7 +111,10 @@ def commandName(self):
def getConsoleCommands(self, parameters, context, feedback, executing=True):
inLayer = self.parameterAsRasterLayer(parameters, self.INPUT, context)
bbox = self.parameterAsExtent(parameters, self.EXTENT, context, inLayer.crs())
nodata = self.parameterAsDouble(parameters, self.NODATA, context)
if self.NODATA in parameters and parameters[self.NODATA] is not None:
nodata = self.parameterAsDouble(parameters, self.NODATA, context)
else:
nodata = None
options = self.parameterAsString(parameters, self.OPTIONS, context)
out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)

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

if nodata:
if nodata is not None:
arguments.append('-a_nodata {}'.format(nodata))

arguments.append('-ot')
@@ -127,7 +127,10 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):

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

nodata = self.parameterAsDouble(parameters, self.NODATA, context)
if self.NODATA in parameters and parameters[self.NODATA] is not None:
nodata = self.parameterAsDouble(parameters, self.NODATA, context)
else:
nodata = None
options = self.parameterAsString(parameters, self.OPTIONS, context)
out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)

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

if nodata:
if nodata is not None:
arguments.append('-dstnodata {}'.format(nodata))

if options:
@@ -127,7 +127,10 @@ def commandName(self):
def getConsoleCommands(self, parameters, context, feedback, executing=True):
inLayer = self.parameterAsRasterLayer(parameters, self.INPUT, context)
fieldName = self.parameterAsString(parameters, self.FIELD_NAME, context)
nodata = self.parameterAsDouble(parameters, self.NODATA, context)
if self.NODATA in parameters and parameters[self.NODATA] is not None:
nodata = self.parameterAsDouble(parameters, self.NODATA, context)
else:
nodata = None
offset = self.parameterAsDouble(parameters, self.OFFSET, context)

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

if nodata:
if nodata is not None:
arguments.append('-snodata {}'.format(nodata))

if offset:
@@ -28,6 +28,9 @@
import AlgorithmsTestBase
from processing.algs.gdal.OgrToPostGis import OgrToPostGis
from processing.algs.gdal.GdalUtils import GdalUtils
from processing.algs.gdal.ClipRasterByExtent import ClipRasterByExtent
from processing.algs.gdal.ClipRasterByMask import ClipRasterByMask
from processing.algs.gdal.contour import contour
from processing.algs.gdal.translate import translate
from qgis.core import (QgsProcessingContext,
QgsProcessingFeedback,
@@ -37,6 +40,7 @@
QgsGeometry,
QgsPointXY,
QgsProject,
QgsRectangle,
QgsProcessingUtils,
QgsProcessingFeatureSourceDefinition)
import nose2
@@ -192,6 +196,15 @@ def _copyFile(dst):
'port=5493 sslmode=disable key=\'edge_id\' srid=0 type=LineString table="city_data"."edge" (geom) sql=')
self.assertEqual(name, 'city_data.edge')

def testOgrConnectionStringAndFormat(self):
context = QgsProcessingContext()
output, outputFormat = GdalUtils.ogrConnectionStringAndFormat('d:/test/test.shp', context)
self.assertEqual(output, '"d:/test/test.shp"')
self.assertEqual(outputFormat, '"ESRI Shapefile"')
output, outputFormat = GdalUtils.ogrConnectionStringAndFormat('d:/test/test.mif', context)
self.assertEqual(output, '"d:/test/test.mif"')
self.assertEqual(outputFormat, '"MapInfo File"')

def testGdalTranslate(self):
context = QgsProcessingContext()
feedback = QgsProcessingFeedback()
@@ -258,6 +271,125 @@ def testGdalTranslate(self):
source + ' ' +
'd:/temp/check.tif'])

def testClipRasterByExtent(self):
context = QgsProcessingContext()
feedback = QgsProcessingFeedback()
source = os.path.join(testDataPath, 'dem.tif')
alg = ClipRasterByExtent()
alg.initAlgorithm()
extent = QgsRectangle(1, 2, 3, 4)

# with no NODATA value
self.assertEqual(
alg.getConsoleCommands({'INPUT': source,
'EXTENT': extent,
'OUTPUT': 'd:/temp/check.jpg'}, context, feedback),
['gdal_translate',
'-projwin 0.0 0.0 0.0 0.0 -ot Float32 -of JPEG ' +
source + ' ' +
'd:/temp/check.jpg'])
# with NODATA value
self.assertEqual(
alg.getConsoleCommands({'INPUT': source,
'EXTENT': extent,
'NODATA': 9999,
'OUTPUT': 'd:/temp/check.jpg'}, context, feedback),
['gdal_translate',
'-projwin 0.0 0.0 0.0 0.0 -a_nodata 9999.0 -ot Float32 -of JPEG ' +
source + ' ' +
'd:/temp/check.jpg'])
# with "0" NODATA value
self.assertEqual(
alg.getConsoleCommands({'INPUT': source,
'EXTENT': extent,
'NODATA': 0,
'OUTPUT': 'd:/temp/check.jpg'}, context, feedback),
['gdal_translate',
'-projwin 0.0 0.0 0.0 0.0 -a_nodata 0.0 -ot Float32 -of JPEG ' +
source + ' ' +
'd:/temp/check.jpg'])

def testClipRasterByMask(self):
context = QgsProcessingContext()
feedback = QgsProcessingFeedback()
source = os.path.join(testDataPath, 'dem.tif')
mask = os.path.join(testDataPath, 'polys.gml')
alg = ClipRasterByMask()
alg.initAlgorithm()

# with no NODATA value
self.assertEqual(
alg.getConsoleCommands({'INPUT': source,
'MASK': mask,
'OUTPUT': 'd:/temp/check.jpg'}, context, feedback),
['gdalwarp',
'-ot Float32 -of JPEG -cutline ' +
mask + ' -crop_to_cutline ' + source + ' ' +
'd:/temp/check.jpg'])
# with NODATA value
self.assertEqual(
alg.getConsoleCommands({'INPUT': source,
'MASK': mask,
'NODATA': 9999,
'OUTPUT': 'd:/temp/check.jpg'}, context, feedback),
['gdalwarp',
'-ot Float32 -of JPEG -cutline ' +
mask + ' -crop_to_cutline -dstnodata 9999.0 ' + source + ' ' +
'd:/temp/check.jpg'])
# with "0" NODATA value
self.assertEqual(
alg.getConsoleCommands({'INPUT': source,
'MASK': mask,
'NODATA': 0,
'OUTPUT': 'd:/temp/check.jpg'}, context, feedback),
['gdalwarp',
'-ot Float32 -of JPEG -cutline ' +
mask + ' -crop_to_cutline -dstnodata 0.0 ' + source + ' ' +
'd:/temp/check.jpg'])

def testContour(self):
context = QgsProcessingContext()
feedback = QgsProcessingFeedback()
source = os.path.join(testDataPath, 'dem.tif')
alg = contour()
alg.initAlgorithm()

# with no NODATA value
self.assertEqual(
alg.getConsoleCommands({'INPUT': source,
'BAND': 1,
'FIELD_NAME': 'elev',
'INTERVAL': 5,
'OUTPUT': 'd:/temp/check.shp'}, context, feedback),
['gdal_contour',
'-b 1 -a elev -i 5.0 -f "ESRI Shapefile" ' +
source + ' ' +
'"d:/temp/check.shp"'])
# with NODATA value
self.assertEqual(
alg.getConsoleCommands({'INPUT': source,
'BAND': 1,
'FIELD_NAME': 'elev',
'INTERVAL': 5,
'NODATA': 9999,
'OUTPUT': 'd:/temp/check.shp'}, context, feedback),
['gdal_contour',
'-b 1 -a elev -i 5.0 -snodata 9999.0 -f "ESRI Shapefile" ' +
source + ' ' +
'"d:/temp/check.shp"'])
# with "0" NODATA value
self.assertEqual(
alg.getConsoleCommands({'INPUT': source,
'BAND': 1,
'FIELD_NAME': 'elev',
'INTERVAL': 5,
'NODATA': 0,
'OUTPUT': 'd:/temp/check.gpkg'}, context, feedback),
['gdal_contour',
'-b 1 -a elev -i 5.0 -snodata 0.0 -f "GPKG" ' +
source + ' ' +
'"d:/temp/check.gpkg"'])


class TestGdalOgrToPostGis(unittest.TestCase):

@@ -61,7 +61,6 @@ tests:
MASK:
name: custom/polygon_mask.gml
type: vector
NODATA: 0.0
OPTIONS: ''
results:
OUTPUT:

0 comments on commit f48aaea

Please sign in to comment.
You can’t perform that action at this time.