[gdal-dev] Remapping nodata

Blumentrath, Stefan Stefan.Blumentrath at nina.no
Thu Jan 7 02:59:01 PST 2016


Hei Even,

Thanks for clarification.
I misunderstood the intention, and for import to PostGIS -a_nodata does work around issue #828...
At least in my case I can import (raster2pgsql) "your_raster.vrt" from the command below, while I cannot import "your_raster.tif"...

Cheers
Stefan

-----Original Message-----
From: Even Rouault [mailto:even.rouault at spatialys.com] 
Sent: 7. januar 2016 11:09
To: Blumentrath, Stefan <Stefan.Blumentrath at nina.no>
Cc: gdal-dev at lists.osgeo.org
Subject: Re: [gdal-dev] Remapping nodata

Le jeudi 07 janvier 2016 10:52:24, Blumentrath, Stefan a écrit :
> Hi,
> 
> This is also an issue for PostGIS:
> https://trac.osgeo.org/postgis/ticket/828
> 
> You could try a VRT:
> gdal_translate -of VRT -a_nodata 99999 your_raster.tif your_raster.vrt 
> which is pretty efficient (fast and does not duplicate your data), 
> esp. if you have a bigger raster and do not need to reproject...
> 

gdal_translate -of VRT -a_nodata will not remap values, but just change the declared nodata value.

You can though manually post-edit the VRT to make the SimpleSource a ComplexSource and add a NODATA node, as below:

<VRTDataset rasterXSize="2" rasterYSize="2">
  <GeoTransform>  4.4072000000000000e+05,  6.0000000000000000e+01, 0.0000000000000000e+00,  3.7513200000000000e+06,  0.0000000000000000e+00, -6.0000000000000000e+01</GeoTransform>
  <Metadata>
    <MDI key="AREA_OR_POINT">Area</MDI>
  </Metadata>
  <VRTRasterBand dataType="Float32" band="1">
    <NoDataValue>99999</NoDataValue>
    <ColorInterp>Gray</ColorInterp>
    <ComplexSource>
      <SourceFilename relativeToVRT="1">in.asc</SourceFilename>
      <SourceBand>1</SourceBand>
      <SourceProperties RasterXSize="2" RasterYSize="2" DataType="Float32" 
BlockXSize="2" BlockYSize="1" />
      <SrcRect xOff="0" yOff="0" xSize="2" ySize="2" />
      <DstRect xOff="0" yOff="0" xSize="2" ySize="2" />
      <NODATA>nan</NODATA>
    </ComplexSource>
  </VRTRasterBand>
</VRTDataset>


Which can also be accomplished with gdalbuildvrt  and the -vrtnodata switch without any manual editing:

$ gdalbuildvrt out2.vrt in.asc -vrtnodata 0

$ cat out2.vrt
<VRTDataset rasterXSize="2" rasterYSize="2">
  <GeoTransform>  4.4072000000000000e+05,  6.0000000000000000e+01, 0.0000000000000000e+00,  3.7513200000000000e+06,  0.0000000000000000e+00, -6.0000000000000000e+01</GeoTransform>
  <VRTRasterBand dataType="Float32" band="1">
    <NoDataValue>0</NoDataValue>
    <ColorInterp>Gray</ColorInterp>
    <ComplexSource>
      <SourceFilename relativeToVRT="1">in.asc</SourceFilename>
      <SourceBand>1</SourceBand>
      <SourceProperties RasterXSize="2" RasterYSize="2" DataType="Float32" 
BlockXSize="2" BlockYSize="1" />
      <SrcRect xOff="0" yOff="0" xSize="2" ySize="2" />
      <DstRect xOff="0" yOff="0" xSize="2" ySize="2" />
      <NODATA>nan</NODATA>
    </ComplexSource>
  </VRTRasterBand>
</VRTDataset>

$ gdal_translate out2.vrt /vsistdout/ -of aaigrid
ncols        2
nrows        2
xllcorner    440720.000000000000
yllcorner    3751200.000000000000
cellsize     60.000000000000
NODATA_value  0
 0.0 123
 115 132


> Cheers
> Stefan
> 
> -----Original Message-----
> From: gdal-dev [mailto:gdal-dev-bounces at lists.osgeo.org] On Behalf Of 
> Even Rouault Sent: 7. januar 2016 10:42
> To: gdal-dev at lists.osgeo.org
> Subject: Re: [gdal-dev] Remapping nodata
> 
> Le jeudi 07 janvier 2016 08:54:17, Julien Michel a écrit :
> > Well, maybe this is a little off topic for the gdal mailing list, 
> > but I recently had to deal with the same case, and wrote a small 
> > application in Orfeo ToolBox (>5.2) to do exactly that: map NaN to a 
> > real no data value [1]. So if you need this really quick, and can 
> > afford bringin another (quite big) tool in your workflow, here is a 
> > workaround.
> > 
> > In the long term, this would be a nice addition to gdal_translate 
> > capabilities.
> 
> This can actually be done with gdalwarp.
> 
> $ cat in.asc
> ncols        2
> nrows        2
> xllcorner    440720
> yllcorner    3751200
> cellsize     60
> NODATA_value nan
>  nan 123.0
>  115 132
> 
> $ gdalwarp in.asc out.tif  -dstnodata 0 -overwrite
> 
> (if using trunk please update to the latest revision as I just 
> discovered and fixed a bug for this very particular case (float32 and 
> 0 as target nodata). 2.0 works fine)
> 
> $ gdal_translate out.tif /vsistdout/ -of aaigrid
> ncols        2
> nrows        2
> xllcorner    440720.000000000000
> yllcorner    3751200.000000000000
> cellsize     60.000000000000
> NODATA_value  0
>  0.0 123
>  115 132
> 
> (you likely need a recent enough GDAL version so that NaN is correctly 
> dealt with)
> 
> > Regards,
> > 
> > Julien
> > 
> > [1]
> > https://www.orfeo-toolbox.org/CookBook/CookBooksu61.html#x85-1900004
> > .1.7
> > 
> > Le 06/01/2016 23:59, Brad Hards a écrit :
> > > I'm working on the geopackage elevation extension experiment, and 
> > > trying to produce some GeoTIFF tiles that get inserted into sqlite.
> > > 
> > > I need to remap the existing nodata (nan) to some other value that 
> > > I can insert into a sqlite table as a number (say zero).
> > > 
> > > I had a long explanation of what I'm doing here, but it is 
> > > actually explained in the docs: "Note that, if the input dataset 
> > > has a nodata value, this does not cause pixel values that are 
> > > equal to that nodata value to be changed to the value specified with this option. "
> > > 
> > > Is there an easy way to do that conversion?
> > > 
> > > Brad
> > > _______________________________________________
> > > gdal-dev mailing list
> > > gdal-dev at lists.osgeo.org
> > > http://lists.osgeo.org/mailman/listinfo/gdal-dev

--
Spatialys - Geospatial professional services http://www.spatialys.com


More information about the gdal-dev mailing list