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

...
GDALDriver *poDriver = (GDALDriver *) GDALGetDriverByName( "VRT" );
GDALDataset *poVRTDS;
poVRTDS = poVRTDriver->Create("", xSize, ySize,
                              3, GDT_Byte,
                              NULL);
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",
                              poVRTDS,
                              FALSE, NULL,
                              NULL, NULL);
  if(poDesDS== NULL) {
    cerr << "Error: unable to create GDAL dataset" << endl;
    GDALClose(poVRTDS);
    return false;
  }
  GDALClose(poVRTDS);
...

-- 
---=== Qingfeng (Gene) Guan ===---


More information about the gdal-dev mailing list