[gdal-dev] How to CreateCopy a dataset from an empty virtual dataset (no source dataset is linked)?

Frank Warmerdam warmerdam at pobox.com
Tue Dec 9 18:09:19 EST 2008

Qingfeng (Gene) Guan wrote:
> I am trying to write a program which is able to create a raster dataset 
> of any supported format. To do so, I first created a virtual dataset in 
> memory using the Create() function, then set the projection and add the 
> bands to the virtual dataset (no links to source datasets), and then 
> used the CreateCopy() function to create a non-virtual dataset from the 
> virtual dataset I just created. However, the CreateCopy() always 
> returned a NULL pointer. I also tried creating a temporary vrt file 
> using the Create() function before using CreateCopy(). Still, the 
> CreateCopy() returned a NULL pointer.
> Has anyone done this before or have any idea how to do this? Do I have 
> to link a source dataset to the virtual dataset before using 
> CreateCopy() to create a non-virtual dataset from the virtual dataset? I 
> just want to create an empty dataset in which all the pixels are set to 
> some initial value (for example, zero).
> I am using GDAL 1.5.3. <http://1.5.3./> The following is the code I wrote:
> ...
> GDALDriver *poDriver = (GDALDriver *) GDALGetDriverByName( "VRT" );
> GDALDataset *poVRTDS;
> poVRTDS = poVRTDriver->Create("", xSize, ySize,
>                             3, GDT_Byte,
>                             NULL);


It turns out that VRTSourcedRasterBand::IRasterIO() returns CE_Failure if
there are no sources for the band.  I believe this is an inappropriate
behavior, and unnecessarily interferes in situations like yours.

I would appreciate your filing a trac ticket on the issue, and I'll look
into applying a fix.  Tentatively I'm considering this change
in gdal/frmts/vrt/vrtsourcedrasterband.cpp:

Index: vrtsourcedrasterband.cpp
--- vrtsourcedrasterband.cpp    (revision 15924)
+++ vrtsourcedrasterband.cpp    (working copy)
@@ -122,7 +122,7 @@

      int         iSource;
-    CPLErr      eErr = CE_Failure;
+    CPLErr      eErr = CE_None;

      if( eRWFlag == GF_Write )
@@ -171,7 +171,7 @@
  /* -------------------------------------------------------------------- */
  /*      Overlay each source in turn over top this.                      */
  /* -------------------------------------------------------------------- */
-    for( iSource = 0; iSource < nSources; iSource++ )
+    for( iSource = 0; eErr == CE_None && iSource < nSources; iSource++ )
          eErr =
              papoSources[iSource]->RasterIO( nXOff, nYOff, nXSize, nYSize,

You might want to try it yourself.

Best regards,
I set the clouds in motion - turn up   | Frank Warmerdam, warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | Geospatial Programmer for Rent

More information about the gdal-dev mailing list