I have a gdal2tiles code for getting tiff file. In my code I am getting true tiff format but it makes my dype uint8. I want int16 for dtype. how can I overcome it? I know I have to add gdal.GDT_Int16 somewhere but I don't know where... the relevant parts of the my code:

default_tileformat = 'png'

tempdriver = gdal.GetDriverByName( 'MEM' )
tiledriver = gdal.GetDriverByName( default_tileformat )


def writetile( filename, data, dxsize, dysize, bands):
Write raster 'data' (of the size 'dataxsize' x 'dataysize') read from
'dataset' into the tile 'filename' with size 'tilesize' pixels.
Later this should be replaced by new <TMS Tile Raster Driver> from GDAL.

# Create needed directories for output
dirs, file = os.path.split(filename)
if not os.path.isdir(dirs):

# so we have to create temporary file in memmory...

#TODO: Add transparency to files with one band only too (grayscale).
if bands == 3 and tileformat == 'png':
    tmp = tempdriver.Create('', tilesize, tilesize, bands=4)
    alpha = tmp.GetRasterBand(4)
    alphaarray = (zeros((dysize, dxsize)) + 255).astype('l')
    alpha.WriteArray( alphaarray, 0, tilesize-dysize )
    tmp = tempdriver.Create('', tilesize, tilesize, bands=bands)

# (write data from the bottom left corner)
tmp.WriteRaster( 0, tilesize-dysize, dxsize, dysize, data, band_list=range(1, bands+1))

# ... and then copy it into the final tile with given filename
tiledriver.CreateCopy(filename, tmp, strict=0, options=gdal_driver_options)

return 0

and another part:

 # Load raster from read window.
 data = dataset.ReadRaster(rx, ry, rxsize, rysize, dxsize, dysize)
 # Write that raster to the tile.
 writetile( filename, data, dxsize, dysize, bands)

Reading the GDAL API tutorial, under the section Using Create(), you'll see the following line:

dst_ds = driver.Create( dst_filename, 512, 512, 1, gdal.GDT_Byte )

I presume the default type is GDT_Byte which is why you are getting what you are getting.

  • thank you for replyment. before your response i have added the same thing you added. but not "GDT_Byte". to GDT_Int16. only create module is enough for this? am i add this for write modeule too... – Aragon Feb 1 '12 at 17:36
  • When you create a driver it is used for reading and writing, so once you set the datatype, that's it for that driver. There's nothing stopping you creating two drivers for two different datatypes and use one to read and one to write. – MerseyViking Feb 2 '12 at 10:24

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.