4

I'm trying to change the values of nodata from my source file to a set value for my output file, but all I am getting is an error. This is my code:

    from osgeo import gdal
    from subprocess import call

    input_raster = r"c:\blah\someinputtiff.tif" #This file I want to clip

    output_raster = r"c:\blah\someoutputtiff.tif" #The resulting clip file

    input_shape = r"c:\blah\someshapefile.shp" #The file I want to use to clip the.tif

    call(['C:\Program Files (x86)\GDAL\gdalwarp.exe','-
    dstNodata"value[-9999]"', '-cutline' , input_shape , '-crop_to_cutline' ,  
    input_raster , output_raster, '-overwrite'])

This produces the following error:

    FAILURE: Unknown option name '-dstNodata"value[-9999]"'

I have tried multiple permutations of '-dstNodata' followed by a value but no luck.

5

Since GDAL 2.1 it's possible to use GDAL/OGR utilities as library functions. So this is an alternative way to accomplish this task:

from osgeo import gdal

input_raster = r"c:\blah\someinputtiff.tif" #This file I want to clip
output_raster = r"c:\blah\someoutputtiff.tif" #The resulting clip file
input_shape = r"c:\blah\someshapefile.shp" #The file I want to use to clip the.tif

ds = gdal.Warp(output_raster, input_raster, format = 'GTiff', cutlineDSName = input_shape, cutlineLayer = 'someshapefile', dstNodata = -9999)
ds = None
| improve this answer | |
  • Similarly to the question, in my case the lack of 'dstNodata' parameter usage set all the nodata values to 0, which had an affect in histograms etc. By using -9999, now it shows 'no data' in QGIS. But I don't know the exact values, since I can't create a human readable ASCII Grid' format :/ – pnz May 15 at 22:21
1

From gdalwarp documentation page:

-dstnodata value [value...]:

Set nodata values for output bands (different values can be supplied for each band). If more than one value is supplied all values should be quoted to keep them together as a single operating system argument. New files will be initialized to this value and if possible the nodata value will be recorded in the output file. Use a value of None to ensure that nodata is not defined (GDAL>=1.11). If this argument is not used then nodata values will be copied from the source dataset (GDAL>=1.11).

Examples from gdalwarp wiki page ignoring a single value for one or more bands and different values per band:

# for this image we want to ignore black (0)
gdalwarp -srcnodata 0 -dstnodata 0 orig-ignore-black.tif black-nodata.tif

# and now we want to ignore white (0)
gdalwarp -srcnodata 255 -dstnodata 255 orig-ignore-white.tif white-nodata.tif

# and finally ignore a particular blue-grey (RGB 125 125 150)
gdalwarp -srcnodata "125 125 150" -dstnodata "125 125 150" orig-ignore-grey.tif grey-nodata.tif

# now we can mosaic them all and not worry about nodata parameters
gdalwarp -dstnodata 0 black-nodata.tif grey-nodata.tif white-nodata.tif final-mosaic.tif

Based in this information, I think that your error raises for two reasons, value definition ("value[-9999]") and the space between the option name and the option value (-dstNodata"value[-9999]").

So, my recommendation is to change to:

call(['C:\Program Files (x86)\GDAL\gdalwarp.exe','-
    dstnodata -9999', '-cutline' , input_shape , '-crop_to_cutline' ,  
    input_raster , output_raster, '-overwrite'])
| improve this answer | |
  • Thanks for your reply, unfortunately this didn't work and the error simply changed to: FAILURE: Unknown option name '-dstNodata -9999' – Yjelza Nov 15 '17 at 11:45
  • I made the same mistake than yours.... The option is -dstnodata, not -dstNodata – aldo_tapia Nov 15 '17 at 11:46
  • FAILURE: Unknown option name '-dstnodata -9999' Still no luck, I have no idea why it's refusing to accept this as an option... – Yjelza Nov 15 '17 at 11:51
  • @Yjelza, also are you sure you're using gdal>=1.11? from python try calling >>> gdal.__version__ – dr_times Nov 15 '17 at 12:01
  • @dr_times I'm definitely using Gdal 2.2.2 – Yjelza Nov 15 '17 at 12:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.