gdal_grid has parameters that let you designate the extent coordinates of your output raster.
gdal_grid -txe [xcoord1] [xcoord2] -tye [ycoord1] [ycoord2]
where the lower left coordinate would be (xcoord1,ycoord1) and the upper right would be (xcoord2,ycoord2)
GDAL will interpolate your data according to your output extents
I've caught my error. May be it will be helpful for somebody.
CreateCopy() makes read-only Dataset for PNG and JPG, so RasterIO() doesn't write anything into dataset's bands. We have to call RasterIO for the memory Dataset. I've made tests for PNG, JPEG and TIFF, it works.
Create() call for "MEM" dataset doesn't allocate memory for the raster. Memory allocation takes place in RasterIO() calls.
We can activate driver and call CreateCopy() for destination format at the end, when image was completely formed in the memory dataset.
The final code can look like...
union rgba_t {
struct { unsigned char g, b, r, a; };
unsigned c;
};
int cx = 500, cy = 500;
rgba_t buf = new rgba_t[cx * cy];
memset(buf, 0xff, cx * cy * sizeof(rgba_t));
GDALAllRegister();
GDALDataset *pDs, *pDsMem;
char **opt = NULL;
GDALDriver *pDriver, *pDriverMem;
int nBands = 3;
char *strDrv = "GTiff",
*fpath = "c:\\tmp\\o.tiff";
pDriverMem = GetGDALDriverManager()->GetDriverByName("MEM");
pDsMem = pDriverMem->Create("", cx, cy, nBands, GDT_Byte, opt);
GDALRasterBand *pBandR = pDsMem->GetRasterBand(1),
*pBandG = pDsMem->GetRasterBand(2),
*pBandB = pDsMem->GetRasterBand(3);
int szScan = cx * sizeof(rgba_t);
CPLErr err;
err = pBandR->RasterIO(GF_Write, 0, 0, cx, cy, (BYTE *)buf + 2, cx, cy, GDT_Byte, 4, szScan);
err = pBandG->RasterIO(GF_Write, 0, 0, cx, cy, (BYTE *)buf + 1, cx, cy, GDT_Byte, 4, szScan);
err = pBandB->RasterIO(GF_Write, 0, 0, cx, cy, (BYTE *)buf + 0, cx, cy, GDT_Byte, 4, szScan);
pDriver = GetGDALDriverManager()->GetDriverByName(strDrv);
pDs = pDriver->CreateCopy(fpath, pDsMem, FALSE, opt, NULL, NULL);
GDALClose(pDsMem);
GDALClose(pDs);
delete[] buf;
Best Answer
As user30184 said, in Python, the process would be creating a memory raster of the same dimensions (layers and layer extension), and executing the CreateCopy after that:
Then, just work with ds2
This should be translated to C++, of course. You can take a look at the Using CreateCopy() section in the tutorial: http://www.gdal.org/gdal_tutorial.html