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

2008-12-09 Thread Qingfeng (Gene) Guan
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 ==--
___
gdal-dev mailing list
gdal-dev@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/gdal-dev

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

2008-12-09 Thread Qingfeng (Gene) Guan
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 ===---
___
gdal-dev mailing list
gdal-dev@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/gdal-dev


[gdal-dev] a question about GDALDataset::RasterIO

2008-10-23 Thread Qingfeng (Gene) Guan




Hi, 
  I am trying to use the GDALDataset::RasterIO method to read all the
bands into a chunk of memory, and be able to access a pixel's values in
all bands by using a [] operator on the memory. 
  I got the sizes of the data types in all the bands, sum up to get the
total data type size, then define a new type: 
  
  typedef char NewType[totalSize]; /* For exmaple, for a
Byte-type RGB raster, the totalSize is 3 */
  pData = (NewType *)CPLMalloc(totalSize*nRows*nCols);
  poDataset->RasterIO(GF_Read, 0, 0, nCols, nRows,
          pData, nCols, nRows,
  vDataTypes[0], /* using the data type
of band 1 */
          nBands, aBandMap,
          totalSize,    /* The byte offset for
one pixel is the totalSize, e.g., 3 for a RGB raster */
          totalSize*nCols,  /* The byte offset for
one line */
          totalSize/nBands  /* The byte offset for
one band is the size of the data type of one band, e.g, 1 for a RGB
raster */
 );
  
  NewType pixVal;
  memcpy(pixVal,
 pData[0], /* This should return the pixel's values in
all bands, e.g., a 3-byte array containing the RGB values */
 totalSize);

  However, this approach somehow didn't work. Anyone has an idea
how to do this? Is it because the NewType isn't correct?
  Thanks.
-- 

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



___
gdal-dev mailing list
gdal-dev@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/gdal-dev