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

Qingfeng (Gene) Guan guanqf at gmail.com
Tue Dec 9 16:43:22 EST 2008

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
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. The following is the code I wrote:

GDALDriver *poDriver = (GDALDriver *) GDALGetDriverByName( "VRT" );
GDALDataset *poVRTDS;
poVRTDS = poVRTDriver->Create("", xSize, ySize,
                            3, GDT_Byte,
if(poVRTDS == NULL) {
 cerr << "Error: unable to create virtual GDAL dataset " \
     << endl;
 return false;

if(poVRTDS->SetProjection(pProjRef) == CE_Failure ||
 poVRTDS->SetGeoTransform(adfGeoTransform) == CE_Failure) {
 cerr << "Error: unable to set projection for the virtual dataset" \
     << endl;
 return false;

GDALDriver *poDriver = (GDALDriver *) GDALGetDriverByName( "Gtiff" );
GDALDataset *poDesDS;
poDesDS= poDriver->CreateCopy("myGTIFF.tif",
                            FALSE, NULL,
                            NULL, NULL);
 if(poDesDS== NULL) {
  cerr << "Error: unable to create GDAL dataset" << endl;
  return false;


--== Qingfeng (Gene) Guan ==--
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/gdal-dev/attachments/20081209/eeaf68c1/attachment-0001.html

More information about the gdal-dev mailing list