[GIS] Keep NoData values when using gdal_merge

gdalgdal-mergejavascriptnodata

I'm using gdal_merge to join several BioClim rasters, which I will then feed into a Java program that "snaps" points to the nearest BioClim raster tile of non-NoData value. However, when I run gdal_merge, it assigns the NoData value of the BioClim file (NoData Value=-3.39999999999999996e+38) to each of the tiles as if it's a proper value (I think).

Running gdal_info on the merged raster shows that value as the minimum, with no indication of a NoData value; whereas running gdal_info on one of the pre-merged raster tiles shows it assigned to the NoData values.

I think this is creating an IndexOutOfBoundsException error when I then run the Java program… preventing me from snapping the points successfully.

I have two questions:

1) How do I use gdal_merge such that the NoData classification of cells are preserved in the merged output?

2) Could something else be causing the IndexOutOfBoundsException? I'm a novice with Java programs.

Best Answer

In order to maintain NoData cells in the output you seem to need to use both the "-n" option (specifying the NoData value for the input) and the "-a_nodata" option (specifying the NoData value for the output). For example:

gdal_merge.py -n -9999 -a_nodata -9999 -o merged.tif in*.tif

My version of gdal_merge does not overwrite existing files (it just touches them), so I have to remove "merged.tif" before running the command.

Related Question