I'm new to GIS and GDAL. My question probably is very basic, but I couldn't find answer. May be I don't understand GDAL ideology.

I need to create raster images from scratch, for example, JPEG or PNG. Their drivers don't support Create function - only CreateCopy. What is the common technique of new files creation in this case?

In principle, I can try to create Tiff because its driver suports Create(). Next, I can use CreateCopy() for PNG or JPEG using this Tiff. But such method looks indirect and unnatural for me. Also I suppose that this procedure can be too memory hungry if rasters are large.

I dealt with some image libraries before, they usually provide direct and simple way of bitmaps creation. Could somebody show me right direction for GDAL?


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:

driver = gdal.GetDriverByName( 'MEM' )
driver2 = gdal.GetDriverByName( 'PNG' )
ds = driver.Create( '', 255, 255, 1, gdal.GDT_Int32)
ds2 = driver.CreateCopy('/tmp/out.png', ds, 0)

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

  • Thank you. I've tested: it works and doesn't require extra memory. – Alexei Feb 11 '15 at 11:03
  • I've met some problems with PNG and JPEG creation and continued in another theme: gis.stackexchange.com/questions/134000/… . (Sorry for this duplication.) – Alexei Feb 12 '15 at 8:32
  • This won't work anyway if I try to flush cache of ds2 – manatttta Nov 30 '17 at 14:10

I'm using GDAL Version 1.11 in C++, that said my answer may not be relevant depending on the version you are using.

The the Create() method is not supported by the PNG driver (I'm not familiar with the JPEG driver), so you must first create a GDALDataset with a different driver the use a the CreateCopy method from a PNG driver to get your PNG image. Also, the PNG driver only supports GDALDataType GDT_UInt16 and GDT_Byte so the source dataset you are copying must by one of those types.

Here's a little example code. . .

GDALDriver *pDriverTiff, *pDriverPng;
GDALDataset *pSourceDS, *pPngDS;

//call GDALGetDriverManager to setup the two different drivers

//create (or open) the source dataset
pSourceDS = pDriverTiff->Create(datasetPath, 300, 300, 1, GDT_Byte, NULL);

//use the png driver to copy the source dataset
pPngDS = pDriverPNG->CreateCopy(pngPath, pSourceDS, FALSE, NULL, NULL, NULL);
  • The gdal.org/frmt_various.html doc says for PNG that "All pixel types other than 16bit unsigned will be written as eight bit" so you can copy from other types, but they will get cast to GDT_Byte. – Dave X Jan 6 '17 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.